Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I'm deleting a file as such

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

The fileDeleted is true and when I check the DDMS the file is not there but if I click on the gallery it still shows the image that was just deleted. I have to restart the emulator to see the change.

Is there any way to see the changes without having to restart the emaulator? I'm using eclipse

like image 651
stack Avatar asked Aug 26 '12 13:08

stack


2 Answers

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);
like image 82
zapl Avatar answered Oct 10 '22 02:10

zapl


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

like image 30
nandeesh Avatar answered Oct 10 '22 03:10

nandeesh