How to refresh Gallery after deleting image from SDCard

前端 未结 4 797
鱼传尺愫
鱼传尺愫 2020-12-16 02:21

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

相关标签:
4条回答
  • 2020-12-16 02:26

    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();
        }
    
    }
    
    0 讨论(0)
  • 2020-12-16 02:37

    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();
    
            }
    
    0 讨论(0)
  • 2020-12-16 02:43

    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});
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-16 02:46

    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>
    
    0 讨论(0)
提交回复
热议问题