在使用给定位置的意图拍摄照片以ACTION_IMAGE_CAPTURE
在某些设备上保存照片(我的测试设备是带有 android 2.3 的索尼爱立信 Mt11i)后,默认相机应用程序将重复图像保存在相机照片文件夹中。
所以我的应用程序检查照片是否重复并将其删除。但是相机应用程序还保存了拍摄照片的缩略图,所以我也将其删除,但即使在我删除此缩略图之后,仍有关于此缩略图的信息,并且在设备的图库应用程序图像中仍然存在默认图像。如果我检查这张图片的详细信息,仍然有这张照片的信息(日期、位置)。这些是我处理重复照片的方法:
public static void handleTakenPicture(Context context, int lastId, File tempFile) {
/*
* Checking for duplicate images
* This is necessary because some camera implementation not only save where you want them to save but also in their default location.
*/
if (lastId == 0)
return;
final String[] projection = {MediaStore.Images.ImageColumns.DATA, MediaStore.Images.ImageColumns.DATE_TAKEN,
MediaStore.Images.ImageColumns.SIZE, MediaStore.Images.ImageColumns._ID};
final String imageOrderBy = MediaStore.Images.Media._ID + " DESC";
final String imageWhere = MediaStore.Images.Media._ID + ">?";
final String[] imageArguments = {Integer.toString(lastId)};
Cursor imageCursor = context.getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, projection, imageWhere, imageArguments,
imageOrderBy);
// List<File> cameraTakenMediaFiles = new ArrayList<File>();
File gFile = null;
if (imageCursor.getCount() > 0) {
// while( imageCursor.moveToNext() ) {
imageCursor.moveToFirst();
// int id =
// imageCursor.getInt(imageCursor.getColumnIndex(MediaStore.Images.Media._ID));
String path = imageCursor.getString(imageCursor.getColumnIndex(MediaStore.Images.Media.DATA));
gFile = new File(path);
// Long takenTimeStamp =
// imageCursor.getLong(imageCursor.getColumnIndex(MediaStore.Images.Media.DATE_TAKEN));
// Long size =
// imageCursor.getLong(imageCursor.getColumnIndex(MediaStore.Images.Media.SIZE));
// cameraTakenMediaFiles.add(new File(path));
//}
}
imageCursor.close();
//File imageFile = new File(imageFilePath);
if (!tempFile.exists() && gFile != null) {
// was not saved where I wanted, but the camera saved one in the media folder
// try to copy over the one saved by the camera and then delete
try {
Prefsy.copyFile(gFile, tempFile);
gFile.delete();
} catch (IOException e) {
e.printStackTrace();
}
}
if(gFile != null)
{
gFile.delete();
try { // DELETING THUMBNAIL
Cursor th = context.getContentResolver().query(MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, new String[] {MediaStore.Images.ImageColumns.DATA},
MediaStore.Images.Thumbnails.IMAGE_ID + " = ?", new String[] {"" + lastId}, MediaStore.Images.Thumbnails._ID + " DESC");
L(th.getCount()+"");
if (th.getCount() > 0)
{
th.moveToFirst();
L(th.getString(0));
L(new File(th.getString(0)).delete()+"");
L(context.getContentResolver().delete(MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI,
MediaStore.Images.Thumbnails.IMAGE_ID + " = ?", new String[] {"" + lastId})+"");
}
th.close();
} catch (Exception e) {
}
}
/*for( File cameraTakenFile : cameraTakenMediaFiles ) {
// delete the one duplicated
cameraTakenFile.delete();
}*/
}
public static int getLastImageId(Context context){
final String[] imageColumns = { MediaStore.Images.Media._ID };
final String imageOrderBy = MediaStore.Images.Media._ID+" DESC";
final String imageWhere = null;
final String[] imageArguments = null;
Cursor imageCursor = context.getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, imageColumns, imageWhere, imageArguments, imageOrderBy);
if(imageCursor.moveToFirst()){
int id = imageCursor.getInt(imageCursor.getColumnIndex(MediaStore.Images.Media._ID));
imageCursor.close();
return id;
}else{
return 0;
}
}
L() 是 Log 方法