本文介绍了触发旧版和新版android设备(低于和高于kitkat)的mediascanner的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

       }
    }

}

推荐答案

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

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

        }

    }

这篇关于触发旧版和新版android设备(低于和高于kitkat)的mediascanner的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-18 09:44