如何在缓存ContentProvider的位图? [英] How to Caching Bitmaps in a ContentProvider?

查看:104
本文介绍了如何在缓存ContentProvider的位图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你怎么COM prehend本说明

How do you comprehend this note

注意:ContentProvider的可能是存储更合适的地方
  缓存的图像,如果它们以更频繁地访问,例如
  图片库的应用程序。

Note: A ContentProvider might be a more appropriate place to store cached images if they are accessed more frequently, for example in an image gallery application.

本次培训文章中 https://developer.android.com/培训/显示的位图/缓存bitmap.html ?因为我不能让位图文件光标 ,我怎样才能通过的ContentProvider将位图缓存?

in this training article https://developer.android.com/training/displaying-bitmaps/cache-bitmap.html? Since I can't get Bitmap or File from Cursor, how can I cache Bitmaps via ContentProvider?

推荐答案

您实际上读取和使用ContentProvider的写入文件。

You actually can read and write Files using a ContentProvider.

要支持这个在自己的ContentProvider,你必须有你的支持的文件MIME类型在 getStreamTypes()的方法。检查了Android的MIME类型部分<一个href=\"https://developer.android.com/guide/topics/providers/content-provider-creating.html#FileMIMETypes\"相对=nofollow> ContentProvider的教程这里获取更多信息。

To support this in your own ContentProvider you'll have to include your supported File MIME types in the getStreamTypes() method. Check the MIME types section of the Android ContentProvider tutorial here for more info.

您还需要落实 中openFile(URI URI,字符串模式)方法 这是在那里你会实际选择基于URI提供给ContentResolver的文件目录和名称。下面是该方法的实现:

You will also need to implement the openFile(Uri uri, String mode) method which is where you'll actually choose the File directory and name based on the Uri provided to the ContentResolver. Here's a sample implementation of that method:

  @Override
  public ParcelFileDescriptor openFile(Uri uri, String mode) throws FileNotFoundException {
      File root = getContext().getFilesDir();
      File path = new File(root, uri.getEncodedPath());
      path.mkdirs();
      File file = new File(path, "file_"+uri.getLastPathSegment());

      int imode = 0;
      if (mode.contains("w")) {
        imode |= ParcelFileDescriptor.MODE_WRITE_ONLY;
        if (!file.exists()) {
          try {
            file.createNewFile();
          } catch (IOException e) {
            e.printStackTrace();
          }
        }
      }
      if (mode.contains("r"))
        imode |= ParcelFileDescriptor.MODE_READ_ONLY;
      if (mode.contains("+"))
        imode |= ParcelFileDescriptor.MODE_APPEND;

      return ParcelFileDescriptor.open(file, imode);
  }

您可以使用自己喜欢的任意逻辑这里选择你的文件目录。这code只使用一个应用程序的文件目录,但对于位图缓存的目的,可能应该使用的临时缓存目录。

You can use whatever logic you'd like here to choose your File directory. This code just uses the applications files directory, but for the purposes of Bitmap caching this should probably use a temp cache directory.

最后,您的code访问ContentProvider的文件数据应该是这个样子:

Finally, your code to access the ContentProvider file data should look something like this:

ContentResolver cr = getContext().getContentResolver();
InputStream inputStream = cr.openInputStream(uri);

另外,您会使用 ContentResolver.openOutputStream(URI)写您的文件数据的ContentProvider。

Alternatively you'd use ContentResolver.openOutputStream(uri) to write your file data to the ContentProvider.

的位图缓存教程将需要修改的公平位使用的ContentProvider作为磁盘缓存,但我相信这是该说明指的是。

The Bitmap caching tutorial would require a fair bit of modifications to use a ContentProvider as the Disk cache, but I do believe this is what that note was referring to.

这篇关于如何在缓存ContentProvider的位图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆