Bitmap 的加载和 Cache

2017-06-04  本文已影响0人  kongjn

缓存策略是一个通用的思想,实际开发中经常需要用 Bitmap 做缓存。

12.1 Bitmap 的高效加载


BitmapFactory 类提供了四个加载图片的方法:

这四类方法最终是在 Android 的底层实现的,对应这 BitmapFactory 类的几个 native 方法。并且都支持使用 BitmapFactory.options 参数对一个图片进行采样缩放。主要是用到它的 inSampleSize 参数,及采样率。

一张储存格式为ARGB8888 的 1024*1024 像素图片,那么它占有的内存是 1024*1024*4 即 4MB。

12.2 Android 中的缓存策略


目前常用的一种缓存算法是 LRU(Last Recently Used),LRU 是近期少用算法,它的核心思想是当缓存满时,会优先淘汰那些近期最少使用的缓存对象,采用 LRU 算法的缓存有两种:

12.2.1 LruCache

LruCache 是一个泛型类,它的内部采用 LinkedHashMap 以强引用的方法存储外界的缓存对象,其提供了 get 和 put 方法。

public class LruCache<K, V> {
    private final LinkedHashMap<K, V> map;
...

典型的 LruCache 初始化过程:

// 缓存总容量 / 1024 转化 KB 单位
int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024);
    int cacheSize = maxMemory / 8;
    mMemoryCache = new LruCache<String, Bitmap>(cacheSize) {
        @Override
        protected int sizeOf(String key, Bitmap bitmap) {
             // bitmap.getByteCount() / 1024 获取 bitmap 的占用大小
            return bitmap.getByteCount() / 1024;
        }
    };

只需要提供缓存总容量大小并重写 sizeOf 方法即可。

12.2.2 DiskLruCache

DiskLruCache 通过将缓存对象写入文件系统从而实现缓存的效果。DiskLruCache 不是 Android 的一部分,需要另外下载:DiskLruCache.java 源码 (需要 科学上网),源码并不能直接在 Android 中使用,还需要修改。修改后的 DiskLruCache.java

1. DiskLruCache 的创建

public static DiskLruCache open(File directory, int appVersion, int valueCount, long maxSize)
            throws IOException {
private static final long DISK_CACHE_SIZE = 1024 * 1024 * 50; // 50M
File diskCacheDir = getDiskCacheDir(mContext, "bitmap");
        if (!diskCacheDir.exists()) {
            diskCacheDir.mkdirs();
        }
 mDiskLruCache = DiskLruCache.open(diskCacheDir, 1, 1, DISK_CACHE_SIZE);

2. DiskLruCache 的缓存添加

DiskLruCache 的缓存添加的操作是通过 Editor 完成的,Editor 表示一个缓存对象的编辑对象,根据 key 通过 edit() 来获取 Editor 对象

在 Android 中 url 不能直接作为 key,因为 url 中很有可能有特殊字符,一般采用 url 的 md5 值作为 key。
MD5 加密

由于 DiskLruCache.open 方法中设置一个节点(valueCount 为 1)只有一个数据,因此下面的 DISK_CACHE_INDEX 常量直接设置为 0 即可:

// hashKeyFormUrl 返回 MD5 算法结果
String key = hashKeyFormUrl(url);
        //获取 editor 对象
        DiskLruCache.Editor editor = mDiskLruCache.edit(key);
        if (editor != null) {
            // 创建文件输出流
            OutputStream outputStream = editor.newOutputStream(DISK_CACHE_INDEX);
            if (downloadUrlToStream(url, outputStream)) {
             // 提交写入操作
                editor.commit();
            } else {
             // 回退整个操作
                editor.abort();
            }
            mDiskLruCache.flush();
        }
/**
     * http 下载到磁盘缓存
     */
    public boolean downloadUrlToStream(String urlString,
                                       OutputStream outputStream) {
        HttpURLConnection urlConnection = null;
        BufferedOutputStream out = null;
        BufferedInputStream in = null;

        try {
            final URL url = new URL(urlString);
            urlConnection = (HttpURLConnection) url.openConnection();
            in = new BufferedInputStream(urlConnection.getInputStream(),
                    IO_BUFFER_SIZE);
            out = new BufferedOutputStream(outputStream, IO_BUFFER_SIZE);

            int b;
            while ((b = in.read()) != -1) {
                out.write(b);
            }
            return true;
        } catch (IOException e) {
            Log.e(TAG, "downloadBitmap failed." + e);
        } finally {
            if (urlConnection != null) {
                urlConnection.disconnect();
            }
            //关闭流
            MyUtils.close(out);
            MyUtils.close(in);
        }
        return false;
    }

3. DiskLruCache 的缓存查找

通过 key 得到 snapShot 对象,通过 snapShot 可以获得缓存的文件输入流,再转化 bitmap

String key = hashKeyFormUrl(url);
        DiskLruCache.Snapshot snapShot = mDiskLruCache.get(key);
        if (snapShot != null) {
            FileInputStream fileInputStream = (FileInputStream) snapShot.getInputStream(DISK_CACHE_INDEX);
            //该方法返回与此文件输入流有关的文件描述符对象
            FileDescriptor fileDescriptor = fileInputStream.getFD();
            //decodeSampledBitmapFromFileDescriptor 封装了 bitmap 缩放方法。
            bitmap = mImageResizer.decodeSampledBitmapFromFileDescriptor(fileDescriptor,
                    reqWidth, reqHeight);
            if (bitmap != null) {
                addBitmapToMemoryCache(key, bitmap);
            }
        }

FileInputStream 是一种有序的文件流,两次 decodeStream 调用会影响了文件流的位置属性,导致第二次 decodeStream 时得到 null。为了解决这个问题,可以通过文件流来得到它所对应的文件描述符,然后通过 BitmapFactory.decodeFileDescriptor 方法来加载一张缩放后的图片。

public Bitmap decodeSampledBitmapFromFileDescriptor(FileDescriptor fd, int reqWidth, int reqHeight) {
        // First decode with inJustDecodeBounds=true to check dimensions
        final BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeFileDescriptor(fd, null, options);

        // Calculate inSampleSize
        options.inSampleSize = calculateInSampleSize(options, reqWidth,
                reqHeight);

        // Decode bitmap with inSampleSize set
        options.inJustDecodeBounds = false;
        return BitmapFactory.decodeFileDescriptor(fd, null, options);
    }

12.2.3 ImageLoader 的实现

一般来说,一个优秀的 ImageLoader 应该具备如下功能:

本章源码 (https://github.com/singwhatiwanna/android-art-res/tree/master/Chapter_12)

上一篇下一篇

猜你喜欢

热点阅读