I want to add taken photos to MediaStore so Gallery app can find them (without restarting device). App's min sdk is 9. Any help, blog or documentation appreciated.
4 Answers
On most devices, all you need to do is wait a little while and the new photos will be detected automatically.
If you want to preform an immediate refresh to the gallery, you need to use the MediaScanner class, It will refresh the gallery - remove deleted photos, add new ones and so on...
public void refreshGallery() {
Intent scanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
String newPhotoPath = "file:" + image.getAbsolutePath(); // image is the created file image
File file = new File(newPhotoPath);
Uri contentUri = Uri.fromFile(file);
scanIntent.setData(contentUri);
sendBroadcast(scanIntent);
}
Hope this helped!

- 8,414
- 5
- 50
- 91

- 1,829
- 1
- 12
- 22
-
Changing `ACTION_MEDIA_MOUNTED` with `ACTION_MEDIA_SCANNER_SCAN_FILE` solved problem. Edit your answer so I accept this as correct answer! – Ali Behzadian Nejad Feb 17 '14 at 07:22
-
does this work for older android-s? i have an android-4.2 tablet where scanning a single file doesn-t work while the same code on an other android-4.4 handset works. With the android-4.2 i can start scanning the whole sdcard but not a single file. is this an android-4.2 issue or just an issue for my a-4.2 tablet? – k3b Sep 23 '15 at 10:55
-
`ACTION_MEDIA_SCANNER_SCAN_FILE` is deprecated – Tommehh May 20 '20 at 08:50
sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://" + Environment.getExternalStorageDirectory())));
Insert this line of code after your 'save' code.
This will trigger a media scan and all media files in all folders (except with '.nomedia' files) will be updates & visible in gallery.
OR
// Tell the media scanner about the new file so that it is
// immediately available to the user.
MediaScannerConnection.scanFile(this,
new String[] { file.toString() }, null,
new MediaScannerConnection.OnScanCompletedListener() {
public void onScanCompleted(String path, Uri uri) {
Log.i("ExternalStorage", "Scanned " + path + ":");
Log.i("ExternalStorage", "-> uri=" + uri);
}
});
-
This is huge and time consuming procedure. I want just to add one file! – Ali Behzadian Nejad Feb 17 '14 at 07:20
-
I dont know how you can simply add one file. You will have to rescan the entire file system or wait till android system does it for you automatically. – Ab5 Feb 17 '14 at 07:22
-
I have added another method which I found on developers.android.com hope it helps! – Ab5 Feb 17 '14 at 07:26
ok its my code and it work for me it give all images which i can see in Android Gallery just call this function from this line
getallimages(Environment.getExternalStorageDirectory());
and my function is below
private void getallimages(File dir)
{
String[] STAR = { "*" };
final String orderBy = MediaStore.Images.Media.DEFAULT_SORT_ORDER;
Cursor imagecursor = cntx.getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, STAR, null, null, orderBy);
int image_column_index = imagecursor.getColumnIndex(MediaStore.Images.Media.DATA);
int count = imagecursor.getCount();
for (int i = 0; i < count; i++) {
imagecursor.moveToPosition(i);
int id = imagecursor.getInt(image_column_index);
ImageItem imageItem = new ImageItem();
if(new File(imagecursor.getString(imagecursor.getColumnIndex(MediaStore.Images.Media.DATA))).length()<=10485760)
{
imageItem.filePath = imagecursor.getString(imagecursor.getColumnIndex(MediaStore.Images.Media.DATA));
imageItem.id = id;
imageItem.selection = false; //newly added item will be selected by default
controller.images.add(imageItem);
}
}
}

- 5,135
- 2
- 24
- 49
You can ask the MediaScanner to scan a specific file, ie your image file on demand. This should produce less overhead than just asking the MediaScanner to scan everything for new files.