Android: Refreshing the Gallery after saving new images

BHendricks picture BHendricks · Sep 4, 2013 · Viewed 24.9k times · Source

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

Answer

Saran picture Saran · Nov 8, 2013

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);
  }
    });