Deleted image still shows in android gallery untill I restart the emaulator

前端 未结 3 1519
别跟我提以往
别跟我提以往 2021-01-13 14:06

I\'m deleting a file as such

File fileToDelete =  new File(\"filepath\");
Boolean fileDeleted =  fileToDelete.delete();

The fileDeleted is

相关标签:
3条回答
  • 2021-01-13 14:20

    I use the following code (which is also much like my code for creating a video which also tells the media system about file changes and correctly updates the Gallery):

    private void deleteVideo(String videoUrl)
    {
        File videoFile = new File(videoUrl);
        if (!videoFile.delete())
        {
            Log.e(TAG, "Failed to delete " + videoUrl);
        }
        else
        {
            MediaScannerConnection.scanFile(mContext,new String[] { videoUrl }, null, new MediaScannerConnection.OnScanCompletedListener() 
            {
                public void onScanCompleted(String path, Uri uri) 
                {
                    Log.i("ExternalStorage", "Scanned " + path + ":");
                    Log.i("ExternalStorage", "-> uri=" + uri);
                }
            });
        }
    }
    
    0 讨论(0)
  • 2021-01-13 14:32

    The gallery is using Android's media database to display the list of media. Deleting the file will not be reflected in the database until it scans the filesystem again. That is for example done after rebooting.

    You can either delete the file directly through the database or force it to scan the file or folder you just deleted.

    File fileToDelete =  new File("filepath");
    boolean fileDeleted =  fileToDelete.delete();
    
    // request scan    
    Intent scanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
    scanIntent.setData(Uri.fromFile(fileToDelete));
    sendBroadcast(scanIntent);
    
    0 讨论(0)
  • 2021-01-13 14:44

    It is to do with how Gallery shows the Image files. Image file's Thumbnails are cached in the MediaStore and all the details are present in the Mediastore contentProvider.

    Deleting the file will not update this database. But when you restart the emulator, Mediascanning is done by android. If MediaScanning can be triggered , gallery will stop showing the files

    0 讨论(0)
提交回复
热议问题