您的位置:首页 > 移动开发 > Android开发

android 加载大图片时报OOM的解决方案(源码)

2013-01-22 15:56 393 查看
在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();
}

方法二:

// 按图片大小(字节大小)缩放图片   网友处理方式
public static Bitmap fitSizeImg(String path) {
if(path == null || path.length()<1 ) return null;
File file = new File(path);
Bitmap resizeBmp = null;
BitmapFactory.Options opts = new BitmapFactory.Options();
// 数字越大读出的图片占用的heap越小 不然总是溢出
if (file.length() < 20480) {       // 0-20k
opts.inSampleSize = 1;
} else if (file.length() < 51200) { // 20-50k
opts.inSampleSize = 2;
} else if (file.length() < 307200) { // 50-300k
opts.inSampleSize = 4;
} else if (file.length() < 819200) { // 300-800k
opts.inSampleSize = 6;
} else if (file.length() < 1048576) { // 800-1024k
opts.inSampleSize = 8;
} else {
opts.inSampleSize = 10;
}
resizeBmp = BitmapFactory.decodeFile(file.getPath(), opts);
return resizeBmp;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐