android 一次性加载多个图片容易出现OutOfMemoryError ,自己设计了个类,参考部分网上的代码


import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;


import android.graphics.Bitmap;
import android.graphics.BitmapFactory;


public class BigImage {
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 == -1) ? 1 : (int) Math.ceil(Math.sqrt(w * h / maxNumOfPixels));
int upperBound = (minSideLength == -1) ? 128 : (int) Math.min(Math.floor(w / minSideLength), Math.floor(h / minSideLength));


if (upperBound < lowerBound) {
return lowerBound;
}


if ((maxNumOfPixels == -1) && (minSideLength == -1)) {
return 1;
} else if (minSideLength == -1) {
return lowerBound;
} else {
return upperBound;
}
}


public static Bitmap decodeBigBmp(String path) {
BitmapFactory.Options opts = new BitmapFactory.Options();
opts.inJustDecodeBounds = true;// 设置成了true,不占用内存,只获取bitmap宽高
BitmapFactory.decodeFile(path, opts);
opts.inSampleSize = computeSampleSize(opts, -1, 1024 * 800);


opts.inJustDecodeBounds = false;// 这里一定要将其设置回false,因为之前我们将其设置成了true
opts.inPurgeable = true;
opts.inInputShareable = true;
opts.inDither = false;
opts.inPurgeable = true;
opts.inTempStorage = new byte[16 * 1024];
FileInputStream is = null;
Bitmap bmp = null;
try {
is = new FileInputStream(path);
bmp = BitmapFactory.decodeFileDescriptor(is.getFD(), null, opts);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return bmp;
}


public static OutputStream decodeBitmap(String path) {


BitmapFactory.Options opts = new BitmapFactory.Options();
opts.inJustDecodeBounds = true;// 设置成了true,不占用内存,只获取bitmap宽高
BitmapFactory.decodeFile(path, opts);
opts.inSampleSize = computeSampleSize(opts, -1, 1024 * 800);


opts.inJustDecodeBounds = false;// 这里一定要将其设置回false,因为之前我们将其设置成了true
opts.inPurgeable = true;
opts.inInputShareable = true;
opts.inDither = false;
opts.inPurgeable = true;
opts.inTempStorage = new byte[16 * 1024];
FileInputStream is = null;
Bitmap bmp = null;
InputStream ins = null;
ByteArrayOutputStream baos = null;
try {
is = new FileInputStream(path);
bmp = BitmapFactory.decodeFileDescriptor(is.getFD(), null, opts);
double scale = getScaling(opts.outWidth * opts.outHeight, 1024 * 600);
Bitmap bmp2 = Bitmap.createScaledBitmap(bmp, (int) (opts.outWidth * scale), (int) (opts.outHeight * scale), true);
bmp.recycle();
baos = new ByteArrayOutputStream();
bmp2.compress(Bitmap.CompressFormat.JPEG, 100, baos);
bmp2.recycle();
return baos;
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
is.close();
ins.close();
baos.close();
} catch (IOException e) {
e.printStackTrace();
}
System.gc();
}
return baos;
}


private static double getScaling(int src, int des) {
/**
* 目标尺寸÷原尺寸 sqrt开方,得出宽高百分比
*/
double scale = Math.sqrt((double) des / (double) src);
return scale;
}
}

更多相关文章

  1. Android各种屏幕尺寸
  2. Android:缩小图片尺寸,并添加水印
  3. android的内存管理方式
  4. Android 操作系统的内存回收机制
  5. Android 性能优化之内存泄漏检测以及内存优化(中)
  6. Android 内存泄漏调试
  7. Android内存机制分析2——分析APP内存使用情况

随机推荐

  1. Failed to fetch URL http://dl-ssl.goog
  2. Android(安卓)build system分析
  3. android:padding和android:margin的区别
  4. 软件工程 大四团队 android j2ee 求实习
  5. VMware虚拟机安装安卓(Android)x86系统图文
  6. android直接用v4l2采集图片数据
  7. 使用android:autoSizeMinTextSize需要api
  8. Android(安卓)FrameWork 底层开发
  9. Android中串口通讯的问题
  10. 在网址中跳转APP(进入XXXAPP内查看)