Question:
I call the activity to get a photo with the following code:
Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File fileTemp;
File pathTemp;
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED))
pathTemp = new File(DB.pathData, TEMP_DIR);
else {
Toast.makeText(getApplicationContext(), R.string.error_foto, Toast.LENGTH_LONG);
return;
}
if (!pathTemp.exists()) pathTemp.mkdirs(); // если нет папки TEMP создаст ее
fileTemp = new File(pathTemp.getAbsolutePath(), "IMG_" +DB
.dateToString(Calendar.getInstance().getTime(), DB.DATE_FORMAT_FILE)
+ ".jpg");
mOutputFileTempUri = Uri.fromFile(fileTemp);
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, mOutputFileTempUri);
startActivityForResult(cameraIntent, TAKE_PICTURE);
I get the result:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (( requestCode == TAKE_PICTURE)&&(resultCode == RESULT_OK)){
if ( data != null) {
if (data.hasExtra("data")) {
Bitmap thingBitmap = data.getParcelableExtra("data");
// TODO Какие-то действия с миниатюрой
mFotoIV.setImageBitmap(thingBitmap);
}
} else {
// TODO Какие-то действия с полноценным изображением,
// сохраненным по адресу mOutputFileTempUriUri
mFotoIV.setImageURI(mOutputFileTempUri);
mSignature.setVisibility(View.INVISIBLE);
}
mFotoIV.setBackgroundColor(0); // обнуляем фон
} else mOutputFileTempUri = null;
} // onActivityResult
except in mOutputFileTempUri, a photo appears in DCIM, how to make it not appear there?
Answer:
I am using the following method. The saved file from the camera is transferred to it, and then the search is carried out by the recently taken photos. If the same file was found, made with a difference of less than 10 seconds, relative to the received file, then the duplicate is deleted and an Intent
sent with a notification about the deletion of this file.
private static void checkAndDeleteDuplicatePhotos(@Nullable File photoFile, @NotNull Context ctx){
if (photoFile == null) {
Log.w("photo file is null, returning...");
return;
}
String[] projection = new String[]{
MediaStore.Images.ImageColumns._ID,
MediaStore.Images.ImageColumns.DATA,
MediaStore.Images.ImageColumns.DATE_MODIFIED
};
Cursor cursor = ctx.getContentResolver()
.query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
projection,
null,
null,
MediaStore.Images.ImageColumns.DATE_MODIFIED + " DESC");
if (cursor == null) {
Log.w("cursor is null, returning");
return;
}
if (! cursor.moveToFirst()) {
Log.w("cursor is empty, returning");
cursor.close();
return;
}
do {
long date = cursor.getLong(cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATE_MODIFIED)) * 1000;
long diff = Math.abs(date - photoFile.lastModified());
Log.i("\nphotoFile.lastModified() = " + photoFile.lastModified() + "\ndate = " + date + "\nDiff = " + diff);
if (diff > 10000) break;
File file = new File(cursor.getString(cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA)));
BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
options.inSampleSize = 16; //to avoid OOM Error
Bitmap bm1 = BitmapFactory.decodeFile(file.getPath(), options);
Bitmap bm2 = BitmapFactory.decodeFile(photoFile.getPath(), options);
boolean same = bm1.sameAs(bm2);
Log.i("Comparing bitmaps... " + same);
if (same) {
Log.i("Deleting duplicate... " + file.delete());
try {
Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
mediaScanIntent.setData(Uri.fromFile(file));
ctx.sendBroadcast(mediaScanIntent);
} catch (Exception e) {
Log.w(e);
}
break;
}
} while (cursor.moveToNext());
cursor.close();
}