Sarun1001 Sarun1001 - 4 months ago 8
Android Question

Trigger mediascanner for older and new android devices (below and above kitkat)

How can I put Mediascanner code here? I need to show images on gallery. Tried so many solutions, but nothing worked.
An example with the given code will be helpful:

public void SaveImage(Bitmap bitmap)
{
final File myDir = new File(
Environment
.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),

pref.getGalleryName());


myDir.mkdirs();
// fix
myDir.setExecutable(true);
myDir.setReadable(true);
myDir.setWritable(true);

Random generator = new Random();
int n = 100000;
n = generator.nextInt(n);

final String fname = "Filename" + 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());

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

}
}


}

Answer

We can't directly call media scanner after version kitkat,it is only limited to system applications. For updating contents in gallery you need to directly update mediastore database of gallery with required file name. This is what i did to overcome the issue. This method is also efficient than Mediascanner,because Mediascanner method requires a lot of cpu resource. Mediascanner basically search for multimedia contents on our entire storage locations and that may slowdown the device performance

public void saveImageToSDCard(Bitmap bitmap)
    {
        final File myDir = new File(  
         Environment
         .getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),
         pref.getGalleryName());
        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();

        }

    }
Comments