android 加载大图片时报OOM的解决方案(源码)
来源:互联网 发布:e宠商城淘宝店 编辑:程序博客网 时间:2024/05/17 22:52
在Android中:
1.一个进程的内存可以由2个部门组成:java 施用内存 ,C 施用内存 ,这两个内存的和必需小于16M,不然就会出现各人熟悉的OOM,这个就是熬头种OOM的情况。
2.一朝内存分配给Java后,以后这块内存纵然开释后,也只能给Java的施用,这个估计跟java虚拟机里把内存分成好几块进行缓存的原因有关,反正C就别想用到这块的内存了,所以要是Java突然占用了一个大块内存,纵然很快开释了:
C能施用的内存 = 16M - Java某一瞬间占在校大学生创业点子用的最大内存。
而Bitmap的生成是路程经过过程malloc进行内存分配的,占用的是C的内存。
Code :
- /**
- * 加载大图片工具类:解决android加载大图片时报OOM异常
- * 解决原理:先设置缩放选项,再读取缩放的图片数据到内存,规避了内存引起的OOM
- * @author: 张进
- * @time:2011/7/28
- */
- public class BitmapUtil {
- public static final int UNCONSTRAINED = -1;
- /*
- * 获得设置信息
- */
- public static Options getOptions(String path){
- Options options = new Options();
- options.inJustDecodeBounds = true;//只描边,不读取数据
- BitmapFactory.decodeFile(path, options);
- return options;
- }
- /**
- * 获得图像
- * @param path
- * @param options
- * @return
- * @throws FileNotFoundException
- */
- public static Bitmap getBitmapByPath(String path, Options options , int screenWidth , int screenHeight)throws FileNotFoundException{
- File file = new File(path);
- if(!file.exists()){
- throw new FileNotFoundException();
- }
- FileInputStream in = null;
- in = new FileInputStream(file);
- if(options != null){
- Rect r = getScreenRegion(screenWidth,screenHeight);
- int w = r.width();
- int h = r.height();
- int maxSize = w > h ? w : h;
- int inSimpleSize = computeSampleSize(options, maxSize, w * h);
- options.inSampleSize = inSimpleSize; //设置缩放比例
- options.inJustDecodeBounds = false;
- }
- Bitmap b = BitmapFactory.decodeStream(in, null, options);
- try {
- in.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- return b;
- }
- private static Rect getScreenRegion(int width , int height) {
- return new Rect(0,0,width,height);
- }
- /**
- * 获取需要进行缩放的比例,即options.inSampleSize
- * @param options
- * @param minSideLength
- * @param maxNumOfPixels
- * @return
- */
- public static int computeSampleSize(BitmapFactory.Options options,
- int minSideLength, int maxNumOfPixels) {
- int initialSize = computeInitialSampleSize(options, minSideLength,
- maxNumOfPixels);
- int roundedSize;
- if (initialSize <= 8) {
- roundedSize = 1;
- while (roundedSize < initialSize) {
- roundedSize <<= 1;
- }
- } else {
- roundedSize = (initialSize + 7) / 8 * 8;
- }
- return roundedSize;
- }
- private static int computeInitialSampleSize(BitmapFactory.Options options,
- int minSideLength, int maxNumOfPixels) {
- double w = options.outWidth;
- double h = options.outHeight;
- int lowerBound = (maxNumOfPixels == UNCONSTRAINED) ? 1 :
- (int) Math.ceil(Math.sqrt(w * h / maxNumOfPixels));
- int upperBound = (minSideLength == UNCONSTRAINED) ? 128 :
- (int) Math.min(Math.floor(w / minSideLength),
- Math.floor(h / minSideLength));
- if (upperBound < lowerBound) {
- // return the larger one when there is no overlapping zone.
- return lowerBound;
- }
- if ((maxNumOfPixels == UNCONSTRAINED) &&
- (minSideLength == UNCONSTRAINED)) {
- return 1;
- } else if (minSideLength == UNCONSTRAINED) {
- return lowerBound;
- } else {
- return upperBound;
- }
- }
- }
工具类的使用:
- String path = "/sdcard/test2.jpg";
- try {
- Bitmap bitmap = BitmapUtil.getBitmapByPath(path, BitmapUtil.getOptions(path), screenWidth, screenHeight);
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- }
- android 加载大图片时报OOM的解决方案(源码)
- android 加载大图片时报OOM的解决方案(源码)
- android 加载大图片时报OOM的解决方案(源码)
- Android 加载大图片时报OOM的解决方案(源码)
- android 加载大图片时报OOM的解决方案
- android加载大图片时oom问题处理
- Android开发中 页面加载超大图片时出现OOM
- Android加载大图片时的OOM异常及解决(图片的二次采样)
- 如何解决加载大图片时内存溢出的问题 OOM
- Android加载大图、多图OOM解决方案
- 加载大图片时,出现的OOM异常,以及处理OOM异常的方法
- Android在加载大图片时,易出现内存溢出的情况 解决方案一
- Android在加载大图片时,易出现内存溢出的情况 解决方案
- Android开发中 页面加载一张超大图片时出现OOM
- 有效解决Android加载大图片时内存溢出的问题
- Android有效解决加载大图片时内存溢出的问题
- Android有效解决加载大图片时内存溢出的问题
- 有效解决Android加载大图片时内存溢出的问题
- oracle简单触发器 实例
- PHP判断编码是否为utf-8 函数
- C语言编程优化运行速度
- 服务端日志记录log4j
- 将windows共享挂载到linux文件系统下
- android 加载大图片时报OOM的解决方案(源码)
- loadrunner 关联函数的高级应用
- Algorithms - 排序算法之二:快速排序
- PHP统计中英文字符个数的函数
- java多线程基本方法(3)
- PHP获取当前文件路径,上层目录路径
- java 输入/输出 IO流(4)
- 谈密码的管理方法
- 浅析 java5 新特性(5)