3

I'm trying to build a Gallery-like app which will perform the following functions on External storage:

  • List all the folder that has images inside it
  • List all the images that available to the public (will not probe the files inside Android/data)

So far I can list all the images, and folders that have images. However, I later find that these images and folders are not the newest ones: they are not getting refreshed after I put some pictures (say I simply do some screenshots or download some pictures).

And new pictures are not appear even after I call MediaScannerConnection.scanFile.

Except the MediaScannerConnection, I've also tried the expensive Intent.ACTION_MEDIA_SCANNER_SCAN_FILE method with the specified folders, and no luck.

I've browsing on StackOverflow as well as various of sources with people discussing this issue, but none of them can offer a working method.

Sources I've checked/tried:

Code:

How I list all the images:

    private String[] projectionThumbnail = new String[]{MediaStore.Images.Thumbnails._ID, MediaStore.Images.Thumbnails.IMAGE_ID, MediaStore.Images.Thumbnails.DATA};
    private Uri uriThumbnail = MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI;

    private String[] projectionFull = new String[]{MediaStore.Images.Media._ID, MediaStore.Images.Media.BUCKET_DISPLAY_NAME, MediaStore.Images.Media.DATA};
    private Uri uriFull = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;


@Override
    public Loader<Cursor> onCreateLoader(int id, Bundle args) {
        switch (id) {
            case LOADER_ALL_IMAGES:
                return new CursorLoader(getActivity(), uriThumbnail, projectionThumbnail, null, null, MediaStore.Images.Thumbnails._ID + " DESC");
            default:
                return null;
        }
    }

    @Override
    public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
        switch (loader.getId()) {
            case LOADER_ALL_IMAGES:
                adapter.changeCursor(cursor);
                break;
}
    }
    @Override
    public void onLoaderReset(Loader<Cursor> loader) {
        adapter.changeCursor(null);
    }

// And here's how I init the CursorLoader
LoaderManager lm = getActivity().getLoaderManager();
lm.initLoader(LOADER_ALL_IMAGES, null, this);

Here's how I start scanning the images, I intend to scan the whole External storage, but for now, I'll just scan DCIM, PICTURES and DOWNLOADS just for experiment

    File dcim = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);
    File pictures = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
    File downloads = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);

    MediaScannerConnection.scanFile(this,
            new String[]{dcim.getAbsolutePath(), pictures.getAbsolutePath(), downloads.getAbsolutePath()}, new String[]{"image/*", "image/*", "image/*"},
            new MediaScannerConnection.OnScanCompletedListener() {
                public void onScanCompleted(String path, Uri uri) {
                    Log.i("ExternalStorage", "Scanned " + path + ":");
                    Log.i("ExternalStorage", "-> uri=" + uri);
                }
            });

Question:

I did my test on Stock 5.0.2 and 4.4, same result on both of them.

  1. Am I doing anything wrong?
  2. Why does the MediaScanner not working?

Note:

Interestingly, if I open Instagram and open "select gallery" from there. The gallery content is getting refreshed. So it means Instagram is doing the "gallery refreshing" very well so that my app can also receive the newest gallery content, how do they do that?

Community
  • 1
  • 1
dumbfingers
  • 7,001
  • 5
  • 54
  • 80
  • I just ran into the same need... did you manage to solve it? – Fidel Montesino Jun 29 '16 at 13:15
  • @FidelMontesino Hi did you ever solve this issue? I'm having same trouble, I update an mp3 ID3 tag successfully, and all the meta data is updated in MediaStore, apart from the thumbnail (embedded into mp3 frame 'APIC'. I can get it to work by rebooting, suggesting the MediaStore does some house keeping on boot, but don't know how to manually trigger the thumbnail generation. – Mark Oct 14 '16 at 00:37
  • @MarkKeen there is a way to solve this (might be too heavy, but it is the best way to do it): you'll need to manually find each picture and its thumbnail and link them together. Because somehow when you change the gallery's data, say, deleted an image, its thumbnail stays there become an orphan. – dumbfingers Oct 14 '16 at 09:27
  • @dumbfingers Hi thanks for your comment - I having been coming to a similar conclusion. I tried a couple MP3 album art grabbers off of playstore, they seem to work by only grabbing MISSING album art, and creating their own folders and updating mediastore ALBUM_ART. However I'd like to keep it all clean and only use the `Android/data/com.andorid.providers.media/albumthumbs` (where mediastore stores album thumbs) directory. I suppose basically I'll be manually doing what MediaScanner is failing to do when (re)scanning an MP3 file with new/updated embedded artwork. – Mark Oct 14 '16 at 10:01
  • @MarkKeen yeah probably that's the best(and reliable) way to go for now. It's a shame that Android failed to solve this problem even after 5-6 years. :( – dumbfingers Oct 14 '16 at 10:46

0 Answers0