Home » Android » android – How to refresh Gallery after deleting image from SDCard

android – How to refresh Gallery after deleting image from SDCard

Posted by: admin May 14, 2020 Leave a comment


When deleting the images on Android’s SD Card, sometime the images are correctly removed but in the gallery still remain a preview of the removed image. When tapping on it, it is loaded as a black image. To resolve it, you need to run MediaScanner. But this code won’t work and still the preview of review image remain in the Gallery.

Anyone knows how to resolve this.

Uri contentUri = Uri.fromFile(file);
Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE,contentUri); 
How to&Answers:

You should delete it from mediaStore

public static void deleteFileFromMediaStore(final ContentResolver contentResolver, final File file) {
    String canonicalPath;
    try {
        canonicalPath = file.getCanonicalPath();
    } catch (IOException e) {
        canonicalPath = file.getAbsolutePath();
    final Uri uri = MediaStore.Files.getContentUri("external");
    final int result = contentResolver.delete(uri,
            MediaStore.Files.FileColumns.DATA + "=?", new String[] {canonicalPath});
    if (result == 0) {
        final String absolutePath = file.getAbsolutePath();
        if (!absolutePath.equals(canonicalPath)) {
                    MediaStore.Files.FileColumns.DATA + "=?", new String[]{absolutePath});



sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://"+ Environment.getExternalStorageDirectory())));

is restricted for only system apps from 4.4

here is the another solution..pass the path of the image you have deleted or added and if the image is deleted image pass true or if added image to gallery then pass false.

 * Scanning the file in the Gallery database
 * @param path
 * @param isDelete
private void scanFile(String path, final boolean isDelete) {
    try {
        MediaScannerConnection.scanFile(context, new String[] { path },
                null, new MediaScannerConnection.OnScanCompletedListener() {
                    public void onScanCompleted(String path, Uri uri) {
                        if (isDelete) {
                            if (uri != null) {
                                        null, null);
    } catch (Exception e) {



For each file while deleted use this


this worked for me

try {
                    MediaStore.Images.Media.DATA + "='"
                            + new File(fileUri).getPath() + "'", null);
        } catch (Exception e) {



Try this:

sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://"+ Environment.getExternalStorageDirectory())));

Add this to your manifest:

            <action android:name="android.intent.action.MEDIA_MOUNTED" />
            <data android:scheme="file" />