How to refresh Gallery after deleting image from S

2020-02-26 09:54发布

问题:

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); 
sendBroadcast(mediaScanIntent);

回答1:

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});
        }
    }
}


回答2:

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();
    }

}


回答3:

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();

        }


回答4:

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>