Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to refresh Android's MediaStore upon photo deletion

Question: how to make the media store to refresh its entry of a DELETED file?

After deleting a photo in code from the external storage, I still see a slot for the deleted photo in the gallery - blank photo.

It seems that the gallery reflects the media store and the deleted photo is found in the media store until the phone is restarted or generally - until the media is rescanned.

Trying to scan the deleted file did not help scanning deleted files (works just for new or existing files): MediaScannerConnection.scanFile(Application.get(), new String[]{file.getPath()}, null, null) (I tried scanning the parent folder as well).

Also tried ACTION_MEDIA_SCANNER_SCAN_FILE to no avail. Example: Application.get().sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(file)))

Sending a broadcast receiver to rescan the entire external storage (thus refreshing the media store)did the trick: Application.get().sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.fromFile(Environment.getExternalStorageDirectory())))

BUT, it seems that Android, as of 4.4, throws a security exception when trying to manually send the ACTION_MEDIA_MOUNTED system broadcast. See @CommonsWare's post: http://commonsware.com/blog/2013/11/06/android-4p4-permission-regressions.html

So, I'm stuck with no solution for refreshing the media store upon file(/photo/video/etc.) deletion.

like image 375
AlikElzin-kilaka Avatar asked Nov 13 '13 13:11

AlikElzin-kilaka


1 Answers

I found the following that works for me in 4.4 on a Nexus 10.

// request scan
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
startActivityForResult(intent, SELECT_PICTURE);
Intent scanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
scanIntent.setData(Uri.fromFile(refreshFile));
sendBroadcast(scanIntent);

"refreshFile" is the file I deleted that I get from my String "fPath" and then convert it to a file.

String filePath = fPath;        
File refreshFile = new File(filePath);
like image 51
Sobo Avatar answered Sep 22 '22 17:09

Sobo