在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;
}
本文介绍了一种在Android中加载大尺寸图片而不触发内存溢出的方法。通过调整图片的采样率并根据屏幕尺寸来缩小图片,从而避免了常见的OOM错误。文章提供了具体的实现代码,包括如何设置BitmapFactory.Options来实现图片的高效加载。
795

被折叠的 条评论
为什么被折叠?



