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

前端 未结 3 1518
别跟我提以往
别跟我提以往 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: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);
    

提交回复
热议问题