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

守給你的承諾、 提交于 2019-12-12 06:03:21

问题


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

       }
    }

}


回答1:


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

        }

    }


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!