While working with android file system, I could notice an issue that I can't really explain to myself. Changing an image by using File.renameTo()
or File.delete()
(no matter if the image has any Exif
data) results in the following (the methods mentioned above return true
):
File.renameTo()
: the Gallery still shows the old name by going to details AND the method posted below also returns an empty thumbnail (sure, because the path points to the file which was supposed to be deleted)File.delete()
: the image is gone from the file system (none of many tested file explorers shows it anymore andnew File(pathToDeletedImage).exists()
returnsfalse
) but the Gallery still shows that image incl. all details
The question: do I need to somehow notify the Gallery about the changes I just performed to an image file?
The method I use to get a thumbnail:
private Bitmap getThumbnail(ContentResolver cr, String path)
throws Exception {
Cursor ca = cr.query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
new String[] { MediaStore.MediaColumns._ID },
MediaStore.MediaColumns.DATA + "=?", new String[] { path },
null);
if (ca != null && ca.moveToFirst()) {
int id = ca.getInt(ca.getColumnIndex(MediaStore.MediaColumns._ID));
ca.close();
return MediaStore.Images.Thumbnails.getThumbnail(cr, id,
MediaStore.Images.Thumbnails.MICRO_KIND, null);
}
ca.close();
return null;
}
UPDATE
Okay, I figured out that we need to synchronize any file changes with the file system. My guess is that operations like File.delete()
and File.renameTo()
work on the Linux-level, but there's also Android above. I ended up resetting my Nexus 5 after programmatically deleting the DCIM
directory, the Gallery just refused to recreate and also refused to use it if I recreated it manually, so all camera images were just blank. Clearing cache and rebooting didn't help. If it happened on a user's device I guess my developer rating would have dropped to a negative value.
I was only able to figure out how to correctly delete images, the below code works and the changes are synced (does not work for rename however):
private void broadcastScanFile(File f) {
Intent intentNotifyImgDeleted = new Intent();
intentNotifyImgDeleted.setType("image/*");
intentNotifyImgDeleted.setAction(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
intentNotifyImgDeleted.setData(Uri.fromFile(f));
sendBroadcast(intentNotifyImgDeleted);
}
The question is now: how do I delete/rename any file/directory so the changes are correctly synced with the file system?
P.S. yes I know how to use search, haven't found any suitable solution so far.