Android

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( 
 Environment.getExternalStoragePublicDirectory 
 (Environment.DIRECTORY_PICTURES),
 pref.getGalleryName());  // change this line based on your requirement
 myDir.mkdirs();
 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())
 file.delete();
 try {
 FileOutputStream out = new FileOutputStream(file);
 bitmap.compress(Bitmap.CompressFormat.JPEG, 90, 
out);
 out.flush();
 out.close();
 Toast.makeText(
 _context,
 _context.getString(R.string.toast_saved).replace 
 ("#",
 "\"" + pref.getGalleryName() + "\""),
 Toast.LENGTH_SHORT).show();
 Log.d(TAG, "Image saved to: " + file.getAbsolutePath());
 
// follow from here onwards --------------------------------
 
 ContentValues values = new ContentValues();
 values.put 
 (MediaStore.Images.Media.DATA,file.getAbsolutePath());
 values.put 
 (MediaStore.Images.Media.MIME_TYPE,"image/jpeg");
 _context.getContentResolver().insert 
 (MediaStore.Images.Media.EXTERNAL_CONTENT_URI,values);

 } catch (Exception e) {
 e.printStackTrace();
 Toast.makeText(_context,
 _context.getString(R.string.toast_saved_failed),
 Toast.LENGTH_LONG).show();
   
  }
 }

 

 

Also i post about this problem on Stackoverflow

http://stackoverflow.com/questions/32171993/trigger-mediascanner-for-older-and-new-android-devices-below-and-above-kitkat

 

For more reference about Mediastore class visit this link
https://developer.android.com/reference/android/provider/MediaStore.html