android图片缓存相关

来源:互联网 发布:无线传输数据 编辑:程序博客网 时间:2024/05/22 13:01

我们编写的应用程序都是有一定内存限制的,程序占用了过高的内存就容易出现OOM(OutOfMemory)异常。

在大多数情况下图片都会大于我们程序所需要的大小。比如说系统图片库里展示的图片大都是用手机摄像头拍出来的,这些图片的分辨率会比我们手机屏幕的分辨率高得多。

因此在展示高分辨率图片的时候,最好先将图片进行压缩。压缩后的图片大小应该和用来展示它的控件大小相近,在一个很小的ImageView上显示一张超大的图片不会带来任何视觉上的好处,但却会占用我们相当多宝贵的内存。

系统BitmapFactory这个类提供了Options来操作图片:

inJustDecodeBounds属性设置为true就可以让解析方法禁止为bitmap分配内存,返回值也不再是一个Bitmap对象,而是null。虽然Bitmap是null了,但是BitmapFactory.Options的outWidth、outHeight都会被赋值。这个技巧让我们可以在加载图片之前就获取到图片的长宽值从而根据情况对图片进行压缩。比如我们有一张2048*1536像素的图片,将inSampleSize的值设置为4,就可以把这张图片压缩成512*384像素。原本加载这张图片需要占用13M的内存,压缩后就只需要占用0.75M了

  1. public static Bitmap decodeSampledBitmapFromResource(Resources res, int resId,  
  2.         int reqWidth, int reqHeight) {  
  3.     // 第一次解析将inJustDecodeBounds设置为true,来获取图片大小  
  4.     final BitmapFactory.Options options = new BitmapFactory.Options();  
  5.     options.inJustDecodeBounds = true;  
  6.     BitmapFactory.decodeResource(res, resId, options);  
  7.     // 计算inSampleSize值  
  8.     options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);  
  9.     // 使用获取到的inSampleSize值再次解析图片  
  10.     options.inJustDecodeBounds = false;  
  11.     return BitmapFactory.decodeResource(res, resId, options);  
  12. }  

  1. public static int calculateInSampleSize(BitmapFactory.Options options,  
  2.         int reqWidth, int reqHeight) {  
  3.     // 源图片的高度和宽度  
  4.     final int height = options.outHeight;  
  5.     final int width = options.outWidth;  
  6.     int inSampleSize = 1;  
  7.     if (height > reqHeight || width > reqWidth) {  
  8.         // 计算出实际宽高和目标宽高的比率  
  9.         final int heightRatio = Math.round((float) height / (float) reqHeight);  
  10.         final int widthRatio = Math.round((float) width / (float) reqWidth);  
  11.         // 选择宽和高中最小的比率作为inSampleSize的值,这样可以保证最终图片的宽和高  
  12.         // 一定都会大于等于目标的宽和高。  
  13.         inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;  
  14.     }  
  15.     return inSampleSize;  
  16. }  

下面的代码非常简单地将任意一张图片压缩成100*100的缩略图,并在ImageView上展示。

[java] view plain copy
  1. mImageView.setImageBitmap(  
  2.     decodeSampledBitmapFromResource(getResources(), R.id.myimage, 100100));  
以上方法是加载单个图片我们选择的通用方式,但是当你需要在界面上加载一大堆图片的时候,情况就变得复杂起来。在很多情况下,(比如使用ListView, GridView 或者 ViewPager 这样的组件),屏幕上显示的图片可以通过滑动屏幕等事件不断地增加,最终导致OOM。

为了保证内存的使用始终维持在一个合理的范围,通常会把被移除屏幕的图片进行回收处理。此时垃圾回收器也会认为你不再持有这些图片的引用,从而对这些图片进行GC操作。用这种思路来解决问题是非常好的,可是为了能让程序快速运行,在界面上迅速地加载图片,你又必须要考虑到某些图片被回收之后,用户又将它重新滑入屏幕这种情况。这时重新去加载一遍刚刚加载过的图片无疑是性能的瓶颈,你需要想办法去避免这个情况的发生。这个时候,使用内存缓存技术可以很好的解决这个问题,它可以让组件快速地重新加载和处理图片。

内存缓存技术对那些大量占用应用程序宝贵内存的图片提供了快速访问的方法。其中最核心的类是LruCache (此类在android-support-v4的包中提供) 。这个类非常适合用来缓存图片,它的主要算法原理是把最近使用的对象用强引用存储在 LinkedHashMap 中,并且把最近最少使用的对象在缓存值达到预设定值之前从内存中移除。

在过去,我们经常会使用一种非常流行的内存缓存技术的实现,即软引用或弱引用 (SoftReference or WeakReference)。但是现在已经不再推荐使用这种方式了,因为从 Android 2.3 (API Level 9)开始,垃圾回收器会更倾向于回收持有软引用或弱引用的对象,这让软引用和弱引用变得不再可靠。另外,Android 3.0 (API Level 11)中,图片的数据会存储在本地的内存当中,因而无法用一种可预见的方式将其释放,这就有潜在的风险造成应用程序的内存溢出并崩溃。

并没有一个指定的缓存大小可以满足所有的应用程序,这是由你决定的。你应该去分析程序内存的使用情况,然后制定出一个合适的解决方案。一个太小的缓存空间,有可能造成图片频繁地被释放和重新加载,这并没有好处。而一个太大的缓存空间,则有可能还是会引起 java.lang.OutOfMemory 的异常。

下面是一个使用 LruCache 来缓存图片的例子:

[java] view plain copy
  1. private LruCache<String, Bitmap> mMemoryCache;  
  2.   
  3. @Override  
  4. protected void onCreate(Bundle savedInstanceState) {  
  5.     // 获取到可用内存的最大值,使用内存超出这个值会引起OutOfMemory异常。  
  6.     // LruCache通过构造函数传入缓存值,以KB为单位。  
  7.     int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024);  
  8.     // 使用最大可用内存值的1/8作为缓存的大小。  
  9.     int cacheSize = maxMemory / 8;  
  10.     mMemoryCache = new LruCache<String, Bitmap>(cacheSize) {  
  11.         @Override  
  12.         protected int sizeOf(String key, Bitmap bitmap) {  
  13.             // 重写此方法来衡量每张图片的大小,默认返回图片数量。  
  14.             return bitmap.getByteCount() / 1024;  
  15.         }  
  16.     };  
  17. }  
  18.   
  19. public void addBitmapToMemoryCache(String key, Bitmap bitmap) {  
  20.     if (getBitmapFromMemCache(key) == null) {  
  21.         mMemoryCache.put(key, bitmap);  
  22.     }  
  23. }  
  24.   
  25. public Bitmap getBitmapFromMemCache(String key) {  
  26.     return mMemoryCache.get(key);  
  27. }  
在这个例子当中,使用了系统分配给应用程序的八分之一内存来作为缓存大小。在中高配置的手机当中,这大概会有4兆(32/8)的缓存空间。一个全屏幕的 GridView 使用4张 800x480分辨率的图片来填充,则大概会占用1.5兆的空间(800*480*4)。因此,这个缓存大小可以存储2.5页的图片。
当向 ImageView 中加载一张图片时,首先会在 LruCache 的缓存中进行检查。如果找到了相应的键值,则会立刻更新ImageView ,否则开启一个后台线程来加载这张图片。
[java] view plain copy
  1. public void loadBitmap(int resId, ImageView imageView) {  
  2.     final String imageKey = String.valueOf(resId);  
  3.     final Bitmap bitmap = getBitmapFromMemCache(imageKey);  
  4.     if (bitmap != null) {  
  5.         imageView.setImageBitmap(bitmap);  
  6.     } else {  
  7.         imageView.setImageResource(R.drawable.image_placeholder);  
  8.         BitmapWorkerTask task = new BitmapWorkerTask(imageView);  
  9.         task.execute(resId);  
  10.     }  
  11. }  
BitmapWorkerTask 还要把新加载的图片的键值对放到缓存中。
[java] view plain copy
  1. class BitmapWorkerTask extends AsyncTask<Integer, Void, Bitmap> {  
  2.     // 在后台加载图片。  
  3.     @Override  
  4.     protected Bitmap doInBackground(Integer... params) {  
  5.         final Bitmap bitmap = decodeSampledBitmapFromResource(  
  6.                 getResources(), params[0], 100100);  
  7.         addBitmapToMemoryCache(String.valueOf(params[0]), bitmap);  
  8.         return bitmap;  
  9.     }  
  10. }  
掌握了以上两种方法,不管是要在程序中加载超大图片,还是要加载大量图片,都不用担心OOM的问题了! 但LruCache只是管理了内存中图片的存储与释放,如果图片从内存中被移除的话,那么又需要从网络上重新加载一次图片,这显然非常耗时。对此,Google又提供了一套硬盘缓存的解决方案:DiskLruCache(非Google官方编写,但获得官方认证)。

网易新闻中的数据都是从网络上获取的,包括了很多的新闻内容和新闻图片,如下图所示:




这些内容和图片在从网络上获取到之后都会存入到本地缓存中,因此即使手机在没有网络的情况下依然能够加载出以前浏览过的新闻。而使用的缓存技术不用多说,自然是DiskLruCache了.


在缓存位置有一个名为journal的文件,如下图所示:


上面那些文件名很长的文件就是一张张缓存的图片,每个文件都对应着一张图片,而journal文件是DiskLruCache的一个日志文件,程序对每张图片的操作记录都存放在这个文件中,基本上看到journal这个文件就标志着该程序使用DiskLruCache技术了。


其实,在真正的项目实战当中如果仅仅是使用硬盘缓存的话,程序是有明显短板的。而如果只使用内存缓存的话,程序当然也会有很大的缺陷。因此,一个优秀的程序必然会将内存缓存和硬盘缓存结合到一起使用。

[java] view plain copy
  1. public class PhotoWallAdapter extends ArrayAdapter<String> {  
  2.   
  3.     /** 
  4.      * 记录所有正在下载或等待下载的任务。 
  5.      */  
  6.     private Set<BitmapWorkerTask> taskCollection;  
  7.   
  8.     /** 
  9.      * 图片缓存技术的核心类,用于缓存所有下载好的图片,在程序内存达到设定值时会将最少最近使用的图片移除掉。 
  10.      */  
  11.     private LruCache<String, Bitmap> mMemoryCache;  
  12.   
  13.     /** 
  14.      * 图片硬盘缓存核心类。 
  15.      */  
  16.     private DiskLruCache mDiskLruCache;  
  17.   
  18.     /** 
  19.      * GridView的实例 
  20.      */  
  21.     private GridView mPhotoWall;  
  22.   
  23.     /** 
  24.      * 记录每个子项的高度。 
  25.      */  
  26.     private int mItemHeight = 0;  
  27.   
  28.     public PhotoWallAdapter(Context context, int textViewResourceId, String[] objects,  
  29.             GridView photoWall) {  
  30.         super(context, textViewResourceId, objects);  
  31.         mPhotoWall = photoWall;  
  32.         taskCollection = new HashSet<BitmapWorkerTask>();  
  33.         // 获取应用程序最大可用内存  
  34.         int maxMemory = (int) Runtime.getRuntime().maxMemory();  
  35.         int cacheSize = maxMemory / 8;  
  36.         // 设置图片缓存大小为程序最大可用内存的1/8  
  37.         mMemoryCache = new LruCache<String, Bitmap>(cacheSize) {  
  38.             @Override  
  39.             protected int sizeOf(String key, Bitmap bitmap) {  
  40.                 return bitmap.getByteCount();  
  41.             }  
  42.         };  
  43.         try {  
  44.             // 获取图片缓存路径  
  45.             File cacheDir = getDiskCacheDir(context, "thumb");  
  46.             if (!cacheDir.exists()) {  
  47.                 cacheDir.mkdirs();  
  48.             }  
  49.             // 创建DiskLruCache实例,初始化缓存数据  
  50.             mDiskLruCache = DiskLruCache  
  51.                     .open(cacheDir, getAppVersion(context), 110 * 1024 * 1024);  
  52.         } catch (IOException e) {  
  53.             e.printStackTrace();  
  54.         }  
  55.     }  
  56.   
  57.     @Override  
  58.     public View getView(int position, View convertView, ViewGroup parent) {  
  59.         final String url = getItem(position);  
  60.         View view;  
  61.         if (convertView == null) {  
  62.             view = LayoutInflater.from(getContext()).inflate(R.layout.photo_layout, null);  
  63.         } else {  
  64.             view = convertView;  
  65.         }  
  66.         final ImageView imageView = (ImageView) view.findViewById(R.id.photo);  
  67.         if (imageView.getLayoutParams().height != mItemHeight) {  
  68.             imageView.getLayoutParams().height = mItemHeight;  
  69.         }  
  70.         // 给ImageView设置一个Tag,保证异步加载图片时不会乱序  
  71.         imageView.setTag(url);  
  72.         imageView.setImageResource(R.drawable.empty_photo);  
  73.         loadBitmaps(imageView, url);  
  74.         return view;  
  75.     }  
  76.   
  77.     /** 
  78.      * 将一张图片存储到LruCache中。 
  79.      *  
  80.      * @param key 
  81.      *            LruCache的键,这里传入图片的URL地址。 
  82.      * @param bitmap 
  83.      *            LruCache的键,这里传入从网络上下载的Bitmap对象。 
  84.      */  
  85.     public void addBitmapToMemoryCache(String key, Bitmap bitmap) {  
  86.         if (getBitmapFromMemoryCache(key) == null) {  
  87.             mMemoryCache.put(key, bitmap);  
  88.         }  
  89.     }  
  90.   
  91.     /** 
  92.      * 从LruCache中获取一张图片,如果不存在就返回null。 
  93.      *  
  94.      * @param key 
  95.      *            LruCache的键,这里传入图片的URL地址。 
  96.      * @return 对应传入键的Bitmap对象,或者null。 
  97.      */  
  98.     public Bitmap getBitmapFromMemoryCache(String key) {  
  99.         return mMemoryCache.get(key);  
  100.     }  
  101.   
  102.     /** 
  103.      * 加载Bitmap对象。此方法会在LruCache中检查所有屏幕中可见的ImageView的Bitmap对象, 
  104.      * 如果发现任何一个ImageView的Bitmap对象不在缓存中,就会开启异步线程去下载图片。 
  105.      */  
  106.     public void loadBitmaps(ImageView imageView, String imageUrl) {  
  107.         try {  
  108.             Bitmap bitmap = getBitmapFromMemoryCache(imageUrl);  
  109.             if (bitmap == null) {  
  110.                 BitmapWorkerTask task = new BitmapWorkerTask();  
  111.                 taskCollection.add(task);  
  112.                 task.execute(imageUrl);  
  113.             } else {  
  114.                 if (imageView != null && bitmap != null) {  
  115.                     imageView.setImageBitmap(bitmap);  
  116.                 }  
  117.             }  
  118.         } catch (Exception e) {  
  119.             e.printStackTrace();  
  120.         }  
  121.     }  
  122.   
  123.     /** 
  124.      * 取消所有正在下载或等待下载的任务。 
  125.      */  
  126.     public void cancelAllTasks() {  
  127.         if (taskCollection != null) {  
  128.             for (BitmapWorkerTask task : taskCollection) {  
  129.                 task.cancel(false);  
  130.             }  
  131.         }  
  132.     }  
  133.   
  134.     /** 
  135.      * 根据传入的uniqueName获取硬盘缓存的路径地址。 
  136.      */  
  137.     public File getDiskCacheDir(Context context, String uniqueName) {  
  138.         String cachePath;  
  139.         if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())  
  140.                 || !Environment.isExternalStorageRemovable()) {  
  141.             cachePath = context.getExternalCacheDir().getPath();  
  142.         } else {  
  143.             cachePath = context.getCacheDir().getPath();  
  144.         }  
  145.         return new File(cachePath + File.separator + uniqueName);  
  146.     }   
  147.   
  148.     /** 
  149.      * 使用MD5算法对传入的key进行加密并返回。 
  150.      */  
  151.     public String hashKeyForDisk(String key) {  
  152.         String cacheKey;  
  153.         try {  
  154.             final MessageDigest mDigest = MessageDigest.getInstance("MD5");  
  155.             mDigest.update(key.getBytes());  
  156.             cacheKey = bytesToHexString(mDigest.digest());  
  157.         } catch (NoSuchAlgorithmException e) {  
  158.             cacheKey = String.valueOf(key.hashCode());  
  159.         }  
  160.         return cacheKey;  
  161.     }  
  162.       
  163.     /** 
  164.      * 将缓存记录同步到journal文件中。 
  165.      */  
  166.     public void fluchCache() {  
  167.         if (mDiskLruCache != null) {  
  168.             try {  
  169.                 mDiskLruCache.flush();  
  170.             } catch (IOException e) {  
  171.                 e.printStackTrace();  
  172.             }  
  173.         }  
  174.     }  
  175.   
  176.     private String bytesToHexString(byte[] bytes) {  
  177.         StringBuilder sb = new StringBuilder();  
  178.         for (int i = 0; i < bytes.length; i++) {  
  179.             String hex = Integer.toHexString(0xFF & bytes[i]);  
  180.             if (hex.length() == 1) {  
  181.                 sb.append('0');  
  182.             }  
  183.             sb.append(hex);  
  184.         }  
  185.         return sb.toString();  
  186.     }  
  187.   
  188.     /** 
  189.      * 异步下载图片的任务。 
  190.      *  
  191.      */  
  192.     class BitmapWorkerTask extends AsyncTask<String, Void, Bitmap> {  
  193.   
  194.         /** 
  195.          * 图片的URL地址 
  196.          */  
  197.         private String imageUrl;  
  198.   
  199.         @Override  
  200.         protected Bitmap doInBackground(String... params) {  
  201.             imageUrl = params[0];  
  202.             FileDescriptor fileDescriptor = null;  
  203.             FileInputStream fileInputStream = null;  
  204.             Snapshot snapShot = null;  
  205.             try {  
  206.                 // 生成图片URL对应的key  
  207.                 final String key = hashKeyForDisk(imageUrl);  
  208.                 // 查找key对应的缓存  
  209.                 snapShot = mDiskLruCache.get(key);  
  210.                 if (snapShot == null) {  
  211.                     // 如果没有找到对应的缓存,则准备从网络上请求数据,并写入缓存  
  212.                     DiskLruCache.Editor editor = mDiskLruCache.edit(key);  
  213.                     if (editor != null) {  
  214.                         OutputStream outputStream = editor.newOutputStream(0);  
  215.                         if (downloadUrlToStream(imageUrl, outputStream)) {  
  216.                             editor.commit();  
  217.                         } else {  
  218.                             editor.abort();  
  219.                         }  
  220.                     }  
  221.                     // 缓存被写入后,再次查找key对应的缓存  
  222.                     snapShot = mDiskLruCache.get(key);  
  223.                 }  
  224.                 if (snapShot != null) {  
  225.                     fileInputStream = (FileInputStream) snapShot.getInputStream(0);  
  226.                     fileDescriptor = fileInputStream.getFD();  
  227.                 }  
  228.                 // 将缓存数据解析成Bitmap对象  
  229.                 Bitmap bitmap = null;  
  230.                 if (fileDescriptor != null) {  
  231.                     bitmap = BitmapFactory.decodeFileDescriptor(fileDescriptor);  
  232.                 }  
  233.                 if (bitmap != null) {  
  234.                     // 将Bitmap对象添加到内存缓存当中  
  235.                     addBitmapToMemoryCache(params[0], bitmap);  
  236.                 }  
  237.                 return bitmap;  
  238.             } catch (IOException e) {  
  239.                 e.printStackTrace();  
  240.             } finally {  
  241.                 if (fileDescriptor == null && fileInputStream != null) {  
  242.                     try {  
  243.                         fileInputStream.close();  
  244.                     } catch (IOException e) {  
  245.                     }  
  246.                 }  
  247.             }  
  248.             return null;  
  249.         }  
  250.   
  251.         @Override  
  252.         protected void onPostExecute(Bitmap bitmap) {  
  253.             super.onPostExecute(bitmap);  
  254.             // 根据Tag找到相应的ImageView控件,将下载好的图片显示出来。  
  255.             ImageView imageView = (ImageView) mPhotoWall.findViewWithTag(imageUrl);  
  256.             if (imageView != null && bitmap != null) {  
  257.                 imageView.setImageBitmap(bitmap);  
  258.             }  
  259.             taskCollection.remove(this);  
  260.         }  
  261.   
  262.         /** 
  263.          * 建立HTTP请求,并获取Bitmap对象。 
  264.          *  
  265.          * @param imageUrl 
  266.          *            图片的URL地址 
  267.          * @return 解析后的Bitmap对象 
  268.          */  
  269.         private boolean downloadUrlToStream(String urlString, OutputStream outputStream) {  
  270.             HttpURLConnection urlConnection = null;  
  271.             BufferedOutputStream out = null;  
  272.             BufferedInputStream in = null;  
  273.             try {  
  274.                 final URL url = new URL(urlString);  
  275.                 urlConnection = (HttpURLConnection) url.openConnection();  
  276.                 in = new BufferedInputStream(urlConnection.getInputStream(), 8 * 1024);  
  277.                 out = new BufferedOutputStream(outputStream, 8 * 1024);  
  278.                 int b;  
  279.                 while ((b = in.read()) != -1) {  
  280.                     out.write(b);  
  281.                 }  
  282.                 return true;  
  283.             } catch (final IOException e) {  
  284.                 e.printStackTrace();  
  285.             } finally {  
  286.                 if (urlConnection != null) {  
  287.                     urlConnection.disconnect();  
  288.                 }  
  289.                 try {  
  290.                     if (out != null) {  
  291.                         out.close();  
  292.                     }  
  293.                     if (in != null) {  
  294.                         in.close();  
  295.                     }  
  296.                 } catch (final IOException e) {  
  297.                     e.printStackTrace();  
  298.                 }  
  299.             }  
  300.             return false;  
  301.         }  
  302.   
  303.     }  
  304.   
  305. }  



第一次从网络上请求图片的时候有点慢,但之后加载图片就会非常快了,滑动起来也很流畅。


原创粉丝点击