public static Bitmap getBitmapFromPath(String imgPath, int reqWidth, int reqHeight) {
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(imgPath, options);
// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
//避免出现内存溢出的情况,进行相应的属性设置。
options.inPreferredConfig = Bitmap.Config.RGB_565;
options.inDither = true;
return BitmapFactory.decodeFile(imgPath, options);
}
public static int calculateInSampleSize( //参2和3为ImageView期待的图片大小
BitmapFactory.Options options, int reqWidth, int reqHeight) {
// 图片的实际大小
final int height = options.outHeight;
final int width = options.outWidth;
//默认值
int inSampleSize = 1;
//动态计算inSampleSize的值
if (height > reqHeight || width > reqWidth) {
final int halfHeight = height/2;
final int halfWidth = width/2;
while( (halfHeight/inSampleSize) >= reqHeight && (halfWidth/inSampleSize) >= reqWidth){
inSampleSize *= 2;
}
}
return inSampleSize;
}
Android Bitmap解决内存溢出问题
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
推荐阅读更多精彩内容
- 专题1:PHPExcel的疑难杂症各个击破 本文使用了一个轻量级的PHP的Excel操作库-PHP_XLSXWri...
- 第一次Hive报错:java.lang.OutOfMemoryError: Java heap space解决办法...
- 内存溢出问题是参加kaggle比赛或者做大数据量实验的第一个拦路虎。 以前做的练手小项目导致新手产生一个惯性思维—...