原因:自己公司项目有IM功,基于万物皆克抄原则,还原微信你可发送的图片功能
image.png要求获取的图片:
1:相机图片
2:截图图片
3:下载图片
4:三方截图,三方相机照片
思路:
1:获取相机文件夹下图片以及照片
1:获取相机路径(魅族特殊)
String CAMERA_IMAGE_BUCKET_NAME = "";
if (DeviceUtils.isMeizu()) {
//魅族拍照图片直接放在DCIM中
CAMERA_IMAGE_BUCKET_NAME =
Environment.getExternalStorageDirectory().toString() +"/DCIM";
} else {
CAMERA_IMAGE_BUCKET_NAME = Environment.getExternalStorageDirectory().getPath() + "/DCIM/Camera";
}
2:获取截图路径
String path = Environment.getExternalStorageDirectory().toString() + "/DCIM/Screenshots";
File file = new File(path);
if (!file.exists()) {
path = Environment.getExternalStorageDirectory().toString() + "/Pictures/Screenshots";
}
2:通过游标获取手机资源文件的图片(选用)
实现通过测试选用了第二种方案(获取数据比较全)
实现:游标获取手机最新一张图片更新(限制30秒内)
public static Pair<Long, String> getLatestMediaPhoto(Context context) {
//查询路径和修改时间
String[] projection = {MediaStore.Images.Media.DATA,MediaStore.Images.Media.DATE_MODIFIED};
long currentTime = System.currentTimeMillis()/ 1000 - Constants.SCREEN_SHOT_OFFET_TIME_S;
//检查camera文件夹,查询并排序
Pair<Long, String> mediaPair = null;
Cursor cursor = context.getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
projection,
MediaStore.Images.Media.DATE_ADDED + " >= ?",
new String[]{currentTime + ""},
MediaStore.Images.Media.DATE_ADDED + " DESC");
if (cursor.moveToFirst()) {
String path= cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA));
if (path!=null&& FileUtils.isFileExists(path)&&FileUtils.getFileLength(path)<= Constant.IMAGE_MAX_SIZE){
mediaPair = new Pair(cursor.getLong(cursor.getColumnIndex(MediaStore.Images.Media.DATE_MODIFIED)),path);
}
}
//对比
if (mediaPair != null ) {
return mediaPair;
}
return null;
}
网友评论