在Android应用里,最耗费内存的就是图片资源。并且在Android系统中。读取位图Bitmap时,分给虚拟机中的图片的堆栈大小仅仅有8M。假设超出了。就会出现OutOfMemory异常

E/AndroidRuntime(  697): java.lang.OutOfMemoryError

E/AndroidRuntime(  697):     at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)

E/AndroidRuntime(  697):     at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:500)

E/AndroidRuntime(  697):     at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:353)

E/AndroidRuntime(  697):     at android.graphics.BitmapFactory.decodeResource(BitmapFactory.java:376)

E/AndroidRuntime(  697):     at android.graphics.BitmapFactory.decodeResource(BitmapFactory.java:406)

E/AndroidRuntime(  697):     at com.example.imagetoshow2.ImageAdapter.createReflectedImages(ImageAdapter.java:66)

E/AndroidRuntime(  697):     at com.example.imagetoshow2.ImageAdapter.getView(ImageAdapter.java:54)

E/AndroidRuntime(  697):     at android.widget.AbsSpinner.onMeasure(AbsSpinner.java:193)



解决的方法:

1.及时回收内存

 if(bitmap != null && !bitmap.isRecycled()){
// 回收而且置为null
bitmap.recycle();
bitmap = null;
}
System.gc();

在适当的地方使用上述代码,将临时不需使用的的回收掉。当然system.gc不应该频繁调用,否则会使系统效率减少。

2.使用BitmapFactory.Options对图片进行压缩

 BitmapFactory.Options opts = new BitmapFactory.Options();
opts.inSampleSize = n; bitmap = BitmapFactory.decodeStream(fis, null, opts);

使用inSampleSize设置放缩比例,默认值为0,设置一个大于0的数便可对图片进行压缩。

 BitmapFactory.Options opts = new BitmapFactory.Options();

    // 设置inJustDecodeBounds为true

    opts.inJustDecodeBounds = true;

    // 使用decodeFile方法得到图片的宽和高

    BitmapFactory.decodeFile(path, opts);

使inJustDecodeBounds为true后,再使用decodeFile()等方法,并不会真正的分配空间,即解码出来的Bitmap为null。仅仅会计算出options.outWidth和options.outHeight值。在下次使用BitmapFactory的decodeFile()等方法实例化Bitmap对象前。将opts.inJustDecodeBound设置回false就能够得到图片了。

3.代码优化

为了避免应用在分配Bitmap内存的时候出现OutOfMemory异常停止执行。通常。在实例化Bitmap的代码中,对OutOfMemory异常进行捕获

<span style="font-size:18px;"> <span style="font-size:18px;">Bitmap bitmap = null;
try {
// 实例化Bitmap
bitmap = BitmapFactory.decodeFile(path);
} catch (OutOfMemoryError e) {
//
}</span></span>

然后在Catch部分做一些内存回收操作。或者是使用缓存图片等...

总是良好的编程风格和优质的代码结构是程序猿的无上追求....

Android Bitmap OutOfMemory 解决的方法的更多相关文章

  1. 用Ant手动打包android程序,android分包机制解决65536方法过多异常

    Android利用ant手动打包 首先我们要给自己的IDE eclispe配置ant,默认的eclipse是集成了ant构建工具的,但是google提供的Android集成开发工具ADT,里面封装了E ...

  2. [Phonegap+Sencha Touch] 移动开发26 Android下的sencha touch程序,转屏时,Ext.Viewport不能触发orientationchange事件的解决的方法

    Sencha touch 2.4.2 已经解决问题了. 比方你为Ext.Viewport的orientationchange事件加入了一个监听方法: Ext.Viewport.on('orientat ...

  3. Android Eclipseproject开发中的常见调试问题(二)android.os.NetworkOnMainThreadException 异常的解决的方法

    android.os.NetworkOnMainThreadException 异常的解决的方法. 刚开是把HttpURLConnectionnection 打开连接这种方法放在UI线程里了,可能不是 ...

  4. Android Studio关于USB device not found的解决的方法

    Android Studio关于USB device not found的解决的方法 我们使用Android Studio进行Android开发时.当我们使用真机进行调试时.非常可能会出现USB de ...

  5. 我的Android进阶之旅------&gt;Android字符串资源中的单引號问题error: Apostrophe not preceded by 的解决的方法

    刚刚在string字符串资源文件里,写了一个单引號.报错了,错误代码例如以下 error: Apostrophe not preceded by \ (in OuyangPeng's blog ) 资 ...

  6. android ANR产生原因和解决的方法

    ANR (Application Not Responding) ANR定义:在Android上,假设你的应用程序有一段时间响应不够灵敏,系统会向用户显示一个对话框.这个对话框称作应用程序无响应(AN ...

  7. android 项目R文件丢失解决的方法

    R文件丢失的原因有非常多,这里提供几种解决的方法: 1.  选中项目,点击 Project - Clean , 清理一下项目. 2. 选中项目,右键 选择 Android Tools  - Fix P ...

  8. Ubuntu使用adb连接android手机失败unknown的解决的方法

    Ubuntu使用adb连接android手机失败unknown的解决的方法   Ubuntu下通过USB数据线连接G11手机后,adb可能无法识别到设备.依照一下步骤能够解决此问题. 1.在termi ...

  9. 兔子--Android Support v4包丢失的解决的方法

    在开发中,Android Support v4包丢失的解决的方法: Project->properties->Java Build Path->Libraries->Add E ...

随机推荐

  1. EXE文件结构和读取方法

    一.EXE文件概念 EXE File英文全名executable file .译作可运行文件,可移植可运行 (PE) 文件格式的文件,它能够载入到内存中.并由操作系统载入程序运行,是可在操作系统存储空 ...

  2. uva-11234 Expressions

    Arithmetic expressions are usually written with the operators in between the two operands (which is ...

  3. SQLServer 网络协议(一)

    SQLserver现在主要的3种协议:Shared Memory.TCP/IP 和 Named Pipe SharedMemory: Shared Memory最快最简单的协议,使用SharedMem ...

  4. 数据结构 - trie

    #include <cstring> #include <iostream> #include <map> #include <cstdio> usin ...

  5. 检验身份证的正确性(C语言版本)

    /* check id_card * write by sndnvaps<sndnvaps@gmail.com> * ai -> a1 , a2, a3, a4, a5, a6... ...

  6. 【翻译自mos文章】回收 asm磁盘空间的方法

    回收 asm磁盘空间的方法 參考原文: How To Reclaim Asm Disk Space? (Doc ID 351866.1) 适用于: Oracle Database - Enterpri ...

  7. cocos2d-x于android在call to OpenGL ES API with no current context

    一.问题: 正在使用JNI离Java(Android)侧 打回来C++(Cocos2d-x)该函数返回消息.Cocos2d-x花掉了 看看 Eclipse的Log中.显示 有 call to Open ...

  8. A WPF/MVVM Countdown Timer

    Introduction This article describes the construction of a countdown timer application written in C# ...

  9. 【Web探索之旅】第二部分第四课:数据库

    内容简介 1.第二部分第四课:数据库 2.第二部分第五课预告:响应式网站 第二部分第四课:数据库 说到“数据库”,顾名思义,是“数据的仓库”的意思. 所以数据库的一大作用就是储存数据咯. 为什么Web ...

  10. poj 3662 Telephone Lines spfa算法灵活运用

    意甲冠军: 到n节点无向图,它要求从一个线1至n路径.你可以让他们在k无条,的最大值.如今要求花费的最小值. 思路: 这道题能够首先想到二分枚举路径上的最大值,我认为用spfa更简洁一些.spfa的本 ...