public class BitmapUtils {
public static Bitmap getBitmapFromBytes(byte[] bytes) {
InputStream inputStream = new ByteArrayInputStream(bytes);
// 为位图设置100K的缓存
BitmapFactory.Options options = new BitmapFactory.Options();
options.inTempStorage = new byte[100 * 1024];
options.inPreferredConfig = Bitmap.Config.RGB_565;
// 设置图片可以被回收,创建Bitmap用于存储Pixel的内存空间在系统内存不足时可以被回收
options.inPurgeable = true;
int len = bytes.length;
if (len < 1024 * 100) {
options.inSampleSize = 1;
} else if (len < 1024 * 1024) {
options.inSampleSize = 2;
} else {
//options.inSampleSize = 4;
double times = Math.floor(((double)len) / (1024 * 1024));
options.inSampleSize = (int)times * 2;
}
options.inSampleSize = 4;
options.inInputShareable = true;
Bitmap bitmap = BitmapFactory.decodeStream(inputStream, null, options);
return bitmap;
}
public static Bitmap getBitmapFromStream(InputStream inputStream) {
if (inputStream == null) {
return null;
}
// 为位图设置100K的缓存
BitmapFactory.Options options = new BitmapFactory.Options();
options.inTempStorage = new byte[100 * 1024];
options.inPreferredConfig = Bitmap.Config.RGB_565;
// 设置图片可以被回收,创建Bitmap用于存储Pixel的内存空间在系统内存不足时可以被回收
options.inPurgeable = true;
int len = -1;
try {
len = inputStream.available();
} catch (Exception e) {
}
if (len != -1) {
if (len < 1024 * 100) {
options.inSampleSize = 1;
} else if (len < 1024 * 1024) {
options.inSampleSize = 2;
} else {
options.inSampleSize = 4;
double times = Math.floor(((double)len) / (1024 * 1024));
options.inSampleSize = (int)times * 2;
}
options.inSampleSize = 4;
options.inInputShareable = true;
Bitmap bitmap = BitmapFactory.decodeStream(inputStream, null, options);
return bitmap;
}
return null;
}
}
Bitmap的压缩处理(防止内存溢出)
最新推荐文章于 2024-04-09 20:00:52 发布