使用BitmapFactory.Options
和inJustDecodeBounds
缩小它:
Bitmap bitmap = getBitmapFromFile(path, width, height);
public static Bitmap getBitmapFromFile(String path, int width, int height) {
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(path, options);
// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, width, height);
options.inJustDecodeBounds = false;
Bitmap bitmap = BitmapFactory.decodeFile(path, options);
return bitmap;
}
public static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {
// Raw height and width of image
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;
if (height > reqHeight || width > reqWidth) {
if (width > height) {
inSampleSize = Math.round((float)height / (float)reqHeight);
} else {
inSampleSize = Math.round((float)width / (float)reqWidth);
}
}
return inSampleSize;
}
在此处阅读有关它的更多信息:有效加载大型位图
另外,我不确定您在哪里调用此方法,但如果您有很多方法,请确保您使用LruCache
或类似方法缓存位图:缓存位图