How to refresh Gallery after deleting image from SDCard
Asked Answered
M

5

10

When deleting the images on Android’s SD Card, sometimes the images are correctly removed but in the gallery still remains a preview of the removed image. When tapping on it, it is loaded as a black image. To resolve it I need to run MediaScanner. But this code doesn't work and still, the preview of review image remains 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); 
sendBroadcast(mediaScanIntent);
Mildredmildrid answered 24/3, 2014 at 9:59 Comment(0)
C
18

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)) {
            contentResolver.delete(uri,
                    MediaStore.Files.FileColumns.DATA + "=?", new String[]{absolutePath});
        }
    }
}
Cluster answered 24/3, 2014 at 10:24 Comment(5)
The application's ContentResolver (retrieved as context.getContentResolver())Cluster
Your code works for android 4.3, But in android 4.4 it still show removed image preview in the galleryMildredmildrid
This seems to work like a charm when I tested it on 4.1 and 5.1 since I have only these two devices.. not sure about 4.4.Groyne
This code is not working in Marshmallow devices. Any idea?Palaestra
@Palaestra For me M is working. Make a new question on stack.Peipeiffer
B
10

Although

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) {
                                context.getContentResolver().delete(uri,
                                        null, null);
                            }
                        }
                    }
                });
    } catch (Exception e) {
        e.printStackTrace();
    }

}
Belgian answered 24/3, 2014 at 10:9 Comment(14)
I tried your code but still it show the preview of removed image in the gallery. Any clue why its not working. I am testing the app in android 4.3 & aboveMildredmildrid
@Mildredmildrid this is the only way now.. works fine for me.pass the true if the image is deleted..Belgian
Ok I will let u know.Mildredmildrid
This is how i used your code, Here it always return null value for the Uri. Can u check what actually wrong in my code. I am testing on Note2 (Android version 4.3) pastebin.com/iikBbaP8Mildredmildrid
@Mildredmildrid seems to be okay..for my sake once try getPath instead of getAbsolutePathBelgian
I tried using getPath(), but not working, in which version of android device u r testingMildredmildrid
I am also testing on kitkat and JB. So whats the issue why its returning null in the URIMildredmildrid
@Mildredmildrid uri is null but image is removed from the gallery..there is no black thumb alsoBelgian
But in my case it still show in the gallery. Can u send me the sample example in my mail [email protected].Mildredmildrid
After lot of trial & error. finally fix the issue. Thanks a lot man. Can I get your mail ID, So that we can discuss our problems.Mildredmildrid
@Mildredmildrid how did you fix it ?Rollins
Exactly the same issue I am facing. I use mediaScanner to refresh the gallery. After scanning process completes the preview of the image is deleted. Please help. @MildredmildridNorman
@Mildredmildrid can u pls tell me how u resolve your issue i m also facing same issueAdiaphorous
On Android 6.0.1, after a file delete and subsequent scan, onScanCompleted() triggers with uri = null. So obviously this code doesn't work.Permanganate
S
2

For each file while deleted use this

KITKAT

this worked for me

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

        }
Snifter answered 16/7, 2015 at 12:5 Comment(0)
P
0

Try this:

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

Add this to your manifest:

<intent-filter>
            <action android:name="android.intent.action.MEDIA_MOUNTED" />
            <data android:scheme="file" /> 
        </intent-filter>
Prohibitionist answered 24/3, 2014 at 10:2 Comment(2)
This code doesn't work in android 4.4. It crash and show the following logcat Permission Denial:Not allowed to send broadcast android.intent.action.MEDIA_MOUNTEDMildredmildrid
Nope it still crash in android 4.4Mildredmildrid
G
0

I have been looking for C# Xamarin Code and found this for Xamarin-Android

You can pass multiple deleted Files addresses/path to ScanFile(Context,string[]) so the Mediascanner gets inform and update the gallery

 Android.Media.MediaScannerConnection.ScanFile(Android.App.Application.Context, new string[] { deletedImageFilePath}, null, null); 
Genia answered 1/1, 2021 at 19:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.