How to update mediastore on android devices instead of triggering mediascanner ?

How to update mediastore on android devices instead of triggering mediascanner ?

How to update mediastore on android devices instead of triggering mediascanner ?

Image or video not found on gallery after downloading. i think most of the Android developers faced this problem. After downloading a multimedia content using an app every user expect to saw it on there gallery.

But it won’t visible in gallery  until gallery database get updated with the filename and location. Database of  gallery is called mediastore.

Till Android 4.4 (KitKat) it is an easy task, you have to just invoke the mediascanner class and then device search through whole storage medium or given specific path to refresh the media store database. And the whole media contents gets updated. But from Lollipop (5.0) google limit mediascanner invoking access to only system apps. You can’t just explicitly call mediascanner through a ‘sendBroadcast(intent)’ method   from the code.

I think the reason is simple.

Invoking Mediascanner is a method needs a lot of CPU resource so its an expensive function because device has to scan the whole storage to update database. Imagine a 32 GB memory card with a folder full of images and videos, each time someone calls media scanner and the device get hang for a few amount of time. it should create a bad user experience

And also someone can purposefully create an application to harm users by repeatedly calling mediascanner.

Any way how to overcome this problem ?.

There comes the mediastore, we can directly update media store with our file name and location

This method is faster and efficent than mediascanner invoking method.

This is a sample code for  updating mediastore for an image file

public void saveImageToSDCard(Bitmap bitmap)
 final File myDir = new File( 
 pref.getGalleryName());  // change this line based on your requirement
 Random generator = new Random();
 int n = 100000;
 n = generator.nextInt(n);
 final String fname = "File" + n + ".jpg";
 File file = new File(myDir, fname);
 if (file.exists())
 try {
 FileOutputStream out = new FileOutputStream(file);
 bitmap.compress(Bitmap.CompressFormat.JPEG, 90, 
 "\"" + pref.getGalleryName() + "\""),
 Log.d(TAG, "Image saved to: " + file.getAbsolutePath());
// follow from here onwards --------------------------------
 ContentValues values = new ContentValues();

 } catch (Exception e) {



Also i post about this problem on Stackoverflow


For more reference about Mediastore class visit this link