避免出现bitmap内存限制OUT OF MEMORY的一种方法

本文介绍了一种有效避免Android Gallery控件在处理大量图片时出现OOM(Out Of Memory)问题的方法。通过实现图片缓存机制及适时释放不再使用的图片资源,确保应用程序运行稳定。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在编写Android程序的时候,我们总是难免会碰到OOM(OUT OF MEMORY)的错误,那么这个错误究竟是怎么来的呢,可以先看一下这篇文章ANDROID BITMAP内存限制OOM,OUT OF MEMORY

这里,我使用Gallery来举例,在模拟器中,不会出现OOM错误,但是,一旦把程序运行到真机里,图片文件一多,必然会出现OOM,我们通过做一些额外的处理来避免。

1.创建一个图片缓存对象HashMap<Integer,Bitmap> dataCache,integer对应Adapter中的位置position,我们只用缓存处在显示中的图片,对于之外的位置,如果dataCache中有对应的图片,我们需要进行回收内存。在这个例子中,Adapter对象的getView方法首先判断该位置是否有缓存的bitmap,如果没有,则解码图片(bitmapDecoder.getPhotoItem,BitmapDecoder类见后面)并返回bitmap对象,设置dataCache在该位置上的bitmap缓存以便之后使用;若是该位置存在缓存,则直接取出来使用,避免了再一次调用底层的解码图像需要的内存开销。有时为了提高Gallery的更新速度,我们还可以预存储一些位置上的bitmap,比如存储显示区域位置外向上3个向下3个位置的bitmap,这样上或下滚动Gallery时可以加快getView的获取。

Java代码 收藏代码
  1. publicViewgetView(intposition,ViewconvertView,ViewGroupparent){
  2. if(convertView==null){
  3. LayoutInflaterinflater=LayoutInflater.from(context);
  4. convertView=inflater.inflate(R.layout.photo_item,null);
  5. holder=newViewHolder();
  6. holder.photo=(ImageView)convertView.findViewById(R.id.photo_item_image);
  7. holder.photoTitle=(TextView)convertView.findViewById(R.id.photo_item_title);
  8. holder.photoDate=(TextView)convertView.findViewById(R.id.photo_item_date);
  9. convertView.setTag(holder);
  10. }else{
  11. holder=(ViewHolder)convertView.getTag();
  12. }
  13. cursor.moveToPosition(position);
  14. Bitmapcurrent=dateCache.get(position);
  15. if(current!=null){//如果缓存中已解码该图片,则直接返回缓存中的图片
  16. holder.photo.setImageBitmap(current);
  17. }else{
  18. current=bitmapDecoder.getPhotoItem(cursor.getString(1),2);
  19. holder.photo.setImageBitmap(current);
  20. dateCache.put(position,current);
  21. }
  22. holder.photoTitle.setText(cursor.getString(2));
  23. holder.photoDate.setText(cursor.getString(4));
  24. returnconvertView;
  25. }

BitmapDecoder.class

Java代码 收藏代码
  1. packagecom.wuyi.bestjoy;
  2. importjava.io.FileNotFoundException;
  3. importjava.io.FileOutputStream;
  4. importandroid.content.Context;
  5. importandroid.graphics.Bitmap;
  6. importandroid.graphics.BitmapFactory;
  7. importandroid.graphics.Matrix;
  8. publicclassBitmapDecoder{
  9. privatestaticfinalStringTAG="BitmapDecoder";
  10. privateContextcontext;
  11. publicBitmapDecoder(Contextcontext){
  12. this.context=context;
  13. }
  14. publicBitmapgetPhotoItem(Stringfilepath,intsize){
  15. BitmapFactory.Optionsoptions=newBitmapFactory.Options();
  16. options.inSampleSize=size;
  17. Bitmapbitmap=BitmapFactory.decodeFile(filepath,options);
  18. bitmap=Bitmap.createScaledBitmap(bitmap,180,251,true);//预先缩放,避免实时缩放,可以提高更新率
  19. returnbitmap;
  20. }
  21. }

2.由于Gallery控件的特点,总有一个item处于当前选择状态,我们利用此时进行dataCache中额外不用的bitmap的清理,来释放内存。

Java代码 收藏代码
  1. @Override
  2. publicvoidonItemSelected(AdapterView<?>parent,Viewview,intposition,longid){
  3. releaseBitmap();
  4. Log.v(TAG,"selectid:"+id);
  5. }
  6. privatevoidreleaseBitmap(){
  7. //在这,我们分别预存储了第一个和最后一个可见位置之外的3个位置的bitmap
  8. //即dataCache中始终只缓存了(M=6+Gallery当前可见view的个数)M个bitmap
  9. intstart=mGallery.getFirstVisiblePosition()-3;
  10. intend=mGallery.getLastVisiblePosition()+3;
  11. Log.v(TAG,"start:"+start);
  12. Log.v(TAG,"end:"+end);
  13. //释放position<start之外的bitmap资源
  14. BitmapdelBitmap;
  15. for(intdel=0;del<start;del++){
  16. delBitmap=dateCache.get(del);
  17. if(delBitmap!=null){
  18. //如果非空则表示有缓存的bitmap,需要清理
  19. Log.v(TAG,"releaseposition:"+del);
  20. //从缓存中移除该del->bitmap的映射
  21. dateCache.remove(del);
  22. delBitmap.recycle();
  23. }
  24. }
  25. freeBitmapFromIndex(end);
  26. }
  27. /**
  28. *从某一位置开始释放bitmap资源
  29. *@paramindex
  30. */
  31. privatevoidfreeBitmapFromIndex(intend){
  32. //释放之外的bitmap资源
  33. BitmapdelBitmap;
  34. for(intdel=end+1;del<dateCache.size();del++){
  35. delBitmap=dateCache.get(del);
  36. if(delBitmap!=null){
  37. dateCache.remove(del);
  38. delBitmap.recycle();
  39. Log.v(TAG,"releaseposition:"+del);
  40. }
  41. }
  42. }

经过这些额外的操作,有效的避免了OOM的问题。


---

转自:http://ck19860613.iteye.com/blog/842732


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值