为旧的和新的 android 设备触发 mediascanner(低于和高于 kitkat)

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

如何将 Mediascanner 代码放在这里?我需要在图库中显示图像。尝试了很多解决方案,但没有任何效果。 使用给定代码的示例将有所帮助:

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

       }
    }

}

kitkat版本后不能直接调用media scanner,仅限于系统应用。 要更新图库中的内容,您需要使用所需的文件名直接更新图库的媒体存储数据库。 这就是我为克服这个问题所做的。 此方法也比 Mediascanner 高效,因为 Mediascanner 方法需要大量 cpu 资源。 Mediascanner 主要是在我们的整个存储位置上搜索多媒体内容,这可能会降低设备性能

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

        }

    }