Android加载大图片OOM异常解决

项目用到加载大图片,app老是出现OOM异常,总结了几点经验,供参考。

1、手动干涉dalvik的堆内存处理效率:

 

1     private final static float TARGET_HEAP_UTILIZATION = 0.75f;

2     //for same activity

3     public void onCreate()

4     {

5         …………

6         VMRuntime.getRuntime().setTargetHeapUtilization(TARGET_HEAP_UTILIZATION);

7         …………

8     }

 

2、手动指定Android堆大小:

 

1     private final static int CWJ_HEAP_SIZE = 6* 1024* 1024 ;

2     //for same activity

3     public void onCreate()

4     {

5         …………

6         VMRuntime.getRuntime().setMinimumHeapSize(CWJ_HEAP_SIZE); //设置最小heap内存为6MB大小。当然对于内存吃紧来说还可以通过手动干涉GC去处理 

7         …………

8     }

 

3、手动指定回收内存,指定gc:

1         if(bitmap!=null && !bitmap.isRecycled())

2         {

3             bitmap.recycle();

4             System.gc();

5         }

4、图片必须进行缩放,不然多半会出OOM:

 

 1     /**

 2      * @param url

 3      *            图片的url

 4      * @param sc

 5      *            ,显示的像素大小

 6      * @return 返回指定RUL的缩略图

 7      *

 8      * @author jevan 2012-7-3

 9      *

10      */

11     public static Bitmap loadImageFromUrl(String url, int sc)

12     {

13

14         URL m;

15         InputStream i = null;

16         BufferedInputStream bis = null;

17         ByteArrayOutputStream out = null;

18

19         if (url == null)

20             return null;

21         try

22         {

23             m = new URL(url);

24             i = (InputStream) m.getContent();

25             bis = new BufferedInputStream(i, 1024 * 4);

26             out = new ByteArrayOutputStream();

27             int len = 0;

28

29             while ((len = bis.read(isBuffer)) != -1)

30             {

31                 out.write(isBuffer, 0, len);

32             }

33             out.close();

34             bis.close();

35         } catch (MalformedURLException e1)

36         {

37             e1.printStackTrace();

38             return null;

39         } catch (IOException e)

40         {

41             e.printStackTrace();

42         }

43         if (out == null)

44             return null;

45         byte[] data = out.toByteArray();

46         BitmapFactory.Options options = new BitmapFactory.Options();

47         options.inJustDecodeBounds = true;

48         BitmapFactory.decodeByteArray(data, 0, data.length, options);

49         options.inJustDecodeBounds = false;

50         int be = (int) (options.outHeight / (float) sc);

51         if (be <= 0)

52         {

53             be = 1;

54         } else if (be > 3)

55         {

56             be = 3;

57         }

58         options.inSampleSize = be;

59         Bitmap bmp =null;

60         try

61         {

62             bmp = BitmapFactory.decodeByteArray(data, 0, data.length, options); //返回缩略图

63         } catch (OutOfMemoryError e)

64         {

65             // TODO: handle exception

66             MainActivity.print("Tile Loader (241) Out Of Memory Error " + e.getLocalizedMessage());

67        

68             System.gc();

69             bmp =null;

70         }

71         return bmp;

72     }

 

把上面几条全部用上,OOM的异常基本上能完全避免!!!

 

转载于:https://www.cnblogs.com/gsdimz/archive/2013/05/21/3089940.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值