触发新旧Android设备的mediascanner(kitkat下方和上方)

时间:2015-08-23 22:01:34

标签: android android-mediascanner

如何在此处输入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();

       }
    }

}

1 个答案:

答案 0 :(得分:0)

我们不能在版本kitkat之后直接调用媒体扫描程序,它仅限于系统应用程序。 要更新库中的内容,您需要直接更新具有所需文件名的库的媒体库数据库。 这就是我为克服这个问题所做的。 此方法也比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();

        }

    }