Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: Refreshing the Gallery after saving new images

So in my application I at one point save a bunch of images to a temporary folder, and I want them to show up immediately in the Gallery. Off of a reboot, they do, but otherwise they don't.

I've tried using the sendBroadcast method:

 sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, 
     Uri.parse("file://" + Environment.getExternalStorageDirectory())));

But I get a permission error:

E/AndroidRuntime( 2628): java.lang.SecurityException: Permission Denial: not allowed to send broadcast android.intent.action.MEDIA_MOUNTED from pid=2628, uid=10068

Could I be missing a permission in my AndroidManifest, or is this just no longer supported? Thanks

like image 748
BHendricks Avatar asked Sep 04 '13 21:09

BHendricks


People also ask

How do I refresh my Android Gallery?

Try going to Settings>SD card & phone storage>Unmount Sd card>Mount SD card. That should trigger the media scanner, and your gallery should refresh.

Why my recent photos are not showing in gallery?

If your photos are visible in My Files but are not in the Gallery app, these files may be set as hidden. This prevents Gallery and other apps from scanning for media. To solve this, you can change the option for showing hidden files.

How do I refresh my gallery app?

Try to clear the data from "Media storage" and if you use an SD card, then also clear data from "external storage". Both are found in Apps -> show system apps and look for them. Restart the phone. It shuld be cleared up.

How do I move pictures from my files to my gallery?

On your Android phone, open Gallery . Press and hold the photo or video you want to move. Move to folder. Select the SD card folder you want to move your photos or videos to.


2 Answers

Code provided by Petrus in another answer works for me on Kitkat (4.4):

MediaScannerConnection.scanFile(this, new String[] { Environment.getExternalStorageDirectory().toString() }, null, new MediaScannerConnection.OnScanCompletedListener() {
/*
 *   (non-Javadoc)
 * @see android.media.MediaScannerConnection.OnScanCompletedListener#onScanCompleted(java.lang.String, android.net.Uri)
 */
public void onScanCompleted(String path, Uri uri) 
  {
      Log.i("ExternalStorage", "Scanned " + path + ":");
      Log.i("ExternalStorage", "-> uri=" + uri);
  }
    });
like image 102
Saran Avatar answered Sep 24 '22 05:09

Saran


I tried Environment.getExternalStorageDirectory().toString(), but it didn't find my custom folder.

I just wanted to share my experience to solving this issue.

At the end, I had MediaScannerConnection configured to scan one file at a time and now missing folder that was holding those images showed up. Every time I download each image, I called MediaScannerConnection and file path as Uri instead of folder itself.

Also, many people asked why sendBroadcast stop working on kitkat and the answer is that sendBroadcast was abused and called too many times and causing system to drain battery or slowdown, so they removed the direct call to prevent abuse which makes sense. I was hoping to use above solution to the folder that hold all images, but it didn't work on the folder. I was hoping that finding folder would expose rest of the files within the custom folder, but it didn't in my case. I am hoping to find better answer in the future...

Here's snippet of my code.

MediaScannerConnection.scanFile(ApplicationContext.context, new String[] { imageFile.getPath() }, null,
              new MediaScannerConnection.OnScanCompletedListener() {
                @Override
                public void onScanCompleted(String path, Uri uri) {
                  Log.i(TAG, "Scanned " + path);
                }
              });
like image 20
user2578151 Avatar answered Sep 22 '22 05:09

user2578151