5

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 Exifdata) 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 and new File(pathToDeletedImage).exists() returns false) 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.

Droidman
  • 11,485
  • 17
  • 93
  • 141
  • If you are renaming you'll need to broadcast two uri's I would think? (the old and the new). Or a URI a level above both (the containing directory). Watch out if using the container - if the directory contains very many files the media-scanner may take a while to finish. – Stevie Apr 17 '14 at 12:02
  • Thanks a lot for the answer – hybrid Sep 25 '15 at 12:17

1 Answers1

1

try with this :) just add this after your work will have been finished :) it will help u pass the file path which u edit

sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(new File(filePath))));
Bhanu Sharma
  • 5,135
  • 2
  • 24
  • 49
  • tried this with `rename()`, renaming a camera photo causes the Gallery to not show it anymore (as if it was deleted), also no thumbnail. So still looking for a solution – Droidman Apr 08 '14 at 10:58
  • try with this http://stackoverflow.com/questions/12529454/image-disappears-in-android-gallery-when-renamed – Bhanu Sharma Apr 08 '14 at 11:01
  • 1
    first i try with my code but unfortunately it not work for him that's y i give other one ok and if u know then give ans dont give this type of comment anywhere – Bhanu Sharma Apr 08 '14 at 11:14