美文网首页
Bitmap 的加载和 Cache

Bitmap 的加载和 Cache

作者: kongjn | 来源:发表于2017-06-04 17:17 被阅读0次

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

    12.1 Bitmap 的高效加载


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

    • decodeFile: 支持从文件系统中加载一个 Bitmap 对象,间接调用 decodeStream
    • decodeResource:支持从资源中加载一个 Bitmap 对象,间接调用 decodeStream
    • decodeStream:支持从输入流中加载一个 Bitmap 对象
    • decodeByteArray:支持从字节数组中加载一个 Bitmap 对象

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

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

    • inSampleSize 为1:占有的内存是 1024*1024*4 即 4MB。
    • inSampleSize 为2:占有的内存是 512*512*4 即 1MB。(1/4)
    • inSampleSize 为4:占有的内存是 256*256*4 即 0.25MB。(1/16)

    12.2 Android 中的缓存策略


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

    • LruCache:内存缓存

    • DiskLruCache:磁盘缓存

    12.2.1 LruCache

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

    public class LruCache<K, V> {
        private final LinkedHashMap<K, V> map;
    ...
    
    • 强引用:直接对对象引用;

    • 软引用:当一个对象只有软引用存在时,系统内存不足时就会被 gc 回收;

    • 弱引用:当一个对象只有弱引用存在时,此对象会随时被 gc 回收;

    典型的 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 方法即可。

    • 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 {
    
    • directory:磁盘缓存的存储路径。

    • SD 上的缓存目录:/sdcard/Android/data/package_name/cache 目录,当应用被卸载后会被删除。

    • 其他目录:应用卸载后依然存在,包括 SD 卡上的指定目录和应用 data 中的其他目录。

    • appVersion:应用版本号,一般设为 1 即可。(作用不大)

    • valueCount:单个节点对用的版本号,一般设为 1 即可。

    • maxSize:缓存的总大小,超出这个设定值后,DiskLruCache 会清除一些缓存。

    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 对象

    • edit():DiskLruCache 不允许同时编辑一个缓存对象,如果这个缓存正在被编辑,那么 edit 会返回 null。

    在 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)

    相关文章

      网友评论

          本文标题:Bitmap 的加载和 Cache

          本文链接:https://www.haomeiwen.com/subject/oobaxxtx.html