diff --git a/app/src/org/commcare/activities/components/ImageCaptureProcessing.java b/app/src/org/commcare/activities/components/ImageCaptureProcessing.java index 12a811c09d..dfadf37a02 100644 --- a/app/src/org/commcare/activities/components/ImageCaptureProcessing.java +++ b/app/src/org/commcare/activities/components/ImageCaptureProcessing.java @@ -1,19 +1,19 @@ package org.commcare.activities.components; +import android.content.ContentResolver; +import android.content.ContentUris; import android.content.ContentValues; import android.content.Intent; +import android.database.Cursor; import android.net.Uri; +import android.provider.MediaStore; import android.provider.MediaStore.Images.Media; import android.widget.Toast; import org.commcare.activities.FormEntryActivity; -import org.commcare.google.services.analytics.AnalyticsParamValue; -import org.commcare.google.services.analytics.FirebaseAnalyticsUtil; -import org.commcare.util.LogTypes; import org.commcare.utils.FileUtil; import org.commcare.utils.UriToFilePath; import org.commcare.views.widgets.ImageWidget; -import org.javarosa.core.services.Logger; import org.javarosa.core.services.locale.Localization; import java.io.File; @@ -55,10 +55,12 @@ private static File moveAndScaleImage(File originalImage, boolean shouldScale, // If we didn't create a scaled image and save it to the final path, then relocate the // original image from the temp filepath to our final path File finalFile = new File(finalFilePath); + if (!originalImage.renameTo(finalFile)) { throw new IOException("Failed to rename " + originalImage.getAbsolutePath() + " to " + finalFile.getAbsolutePath()); } else { + deleteFileFromMediaStore(formEntryActivity.getContentResolver(), originalImage); return finalFile; } } else { @@ -74,11 +76,34 @@ private static File moveAndScaleImage(File originalImage, boolean shouldScale, throw new IOException("Failed to rename " + originalImage.getAbsolutePath() + " to " + rawImageFile.getAbsolutePath()); } else { + deleteFileFromMediaStore(formEntryActivity.getContentResolver(), originalImage); return rawImageFile; } } } + public static void deleteFileFromMediaStore(final ContentResolver contentResolver, final File file) { + // Set up the projection (we only need the ID) + String[] projection = {MediaStore.Images.Media._ID}; + + // Match on the file path + String selection = MediaStore.Images.Media.DATA + " = ?"; + String[] selectionArgs = new String[]{file.getAbsolutePath()}; + + // Query for the ID of the media matching the file path + Uri queryUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI; + Cursor c = contentResolver.query(queryUri, projection, selection, selectionArgs, null); + if (c.moveToFirst()) { + // We found the ID. Deleting the item via the content provider will also remove the file + long id = c.getLong(c.getColumnIndexOrThrow(MediaStore.Images.Media._ID)); + Uri deleteUri = ContentUris.withAppendedId(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, id); + contentResolver.delete(deleteUri, null, null); + } else { + // File not found in media store DB + } + c.close(); + } + /** * Processes the return from an image capture intent, launched by either an ImageWidget or * SignatureWidget