在android中加载位图的有效方法
我正在尝试使用以下代码将图片从图库加载到位图中:在android中加载位图的有效方法,android,memory,bitmap,Android,Memory,Bitmap,我正在尝试使用以下代码将图片从图库加载到位图中: Bitmap myBitmap = Bitmap.createScaledBitmap(BitmapFactory.decodeFile(adress), (int)viewWidth/2, (int)viewHeight/2, false); 其中“地址”是画廊中图片的地址。这在我的Galaxy Note 1 android版本2.3.6上运行良好,但在我的Galaxy 2 android版本4.1.2上由于“内存不足”而崩溃 我做错什么了吗
Bitmap myBitmap = Bitmap.createScaledBitmap(BitmapFactory.decodeFile(adress), (int)viewWidth/2, (int)viewHeight/2, false);
其中“地址”是画廊中图片的地址。这在我的Galaxy Note 1 android版本2.3.6上运行良好,但在我的Galaxy 2 android版本4.1.2上由于“内存不足”而崩溃
我做错什么了吗?有没有办法获得缩放位图?
产生的位图(当它工作时)由于缩放而有点模糊,但我不介意。
谢谢 是您的直接答案-您需要先计算大小,然后获取正确大小的图像使用此方法
Bitmap bm=decodeSampledBitmapFromPath(src, reqWidth, reqHeight);
实施-
public int calculateInSampleSize(BitmapFactory.Options options,
int reqWidth, int reqHeight) {
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;
}
public Bitmap decodeSampledBitmapFromPath(String path, int reqWidth,
int reqHeight) {
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(path, options);
options.inSampleSize = calculateInSampleSize(options, reqWidth,
reqHeight);
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
Bitmap bmp = BitmapFactory.decodeFile(path, options);
return bmp;
}
}请参阅可能的重复项,它将按原始比例显示位图。我需要图像来填充必要的空间,即使这意味着要拉伸