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/p/3089940.html