Skip to content

Commit

Permalink
Deleting deleted files from gallery
Browse files Browse the repository at this point in the history
  • Loading branch information
shubham1g5 committed Feb 13, 2018
1 parent 7ba69ff commit aeaeb01
Showing 1 changed file with 29 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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 {
Expand All @@ -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
Expand Down

0 comments on commit aeaeb01

Please sign in to comment.