【发布时间】:2015-12-04 08:21:24
【问题描述】:
在我的应用程序中,我使用 Touch imageview 为用户启用缩放和裁剪选项。首先,我使用此方法将图像 uri 转换为位图。
Uri 到位图
private Bitmap decodeUri(Uri selectedImage) throws FileNotFoundException {
// Decode image size
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
BitmapFactory.decodeStream(getContentResolver().openInputStream(selectedImage), null, o);
// The new size we want to scale to
final int REQUIRED_SIZE = 140;
// Find the correct scale value. It should be the power of 2.
int width_tmp = o.outWidth, height_tmp = o.outHeight;
String imageType = o.outMimeType;
int scale = 1;
while (true) {
if (width_tmp / 2 < REQUIRED_SIZE
|| height_tmp / 2 < REQUIRED_SIZE) {
break;
}
width_tmp /= 2;
height_tmp /= 2;
scale *= 2;
}
// Decode with inSampleSize
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize = scale;
return BitmapFactory.decodeStream(getContentResolver().openInputStream(selectedImage), null, o2);
}
public Bitmap decodeFile(String filePath) {
//Log.e(TAG, "Camera Image 3");
// Decode image size
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
BitmapFactory.decodeFile(filePath, o);
final int REQUIRED_SIZE = 1024;
int width_tmp = o.outWidth, height_tmp = o.outHeight;
int scale = 1;
while (true) {
if (width_tmp < REQUIRED_SIZE && height_tmp < REQUIRED_SIZE)
break;
width_tmp /= 2;
height_tmp /= 2;
scale *= 2;
}
o.inJustDecodeBounds = false;
// Decode with inSampleSize
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize = scale;
Bitmap bitmap = BitmapFactory.decodeFile(filePath, o2);
// imageView = (ImageView) findViewById(R.id.imgView);
// imageView.setImageBitmap(bitmap);
return bitmap;
}
之后我使用这种方法缩放了我的位图
public Bitmap scaleBitmap(Bitmap rotated)
{
final int maxSize = 300;
int outWidth;
int outHeight;
int inWidth = rotated.getWidth();
int inHeight = rotated.getHeight();
if(inWidth > inHeight){
outWidth = maxSize;
outHeight = (inHeight * maxSize) / inWidth;
} else {
outHeight = maxSize;
outWidth = (inWidth * maxSize) / inHeight;
}
Bitmap resizedBitmap = Bitmap.createScaledBitmap(rotated, outWidth, outHeight, false);
// imageView = (ImageView) findViewById(R.id.imgView);
// imageView.setImageBitmap(resizedBitmap);
// mGPUImageView = (GPUImageView) findViewById(R.id.gpuimage);
// mGPUImageView.setImage(resizedBitmap);
return resizedBitmap;
}
这个方法的输出是一个 resizedBitmap ,我已经把这个位图设置为 imageview 像这样
touchimageview.setImageBitmap( scaleBitmap(decoded));
在完成所有这些缩放和解码技术之后,我仍然摆脱了记忆错误,我该如何解决这个问题,谁能帮忙??
【问题讨论】:
-
您是否尝试过使用 Picasso、通用图像加载器等任何图像库?
-
是的,我正在使用 UIL 将 sdcard 图像加载到网格布局中,并且用户选择的图像位图直接用于 setImageBitmap 方法
-
我的代码有什么问题吗??你能帮忙吗?
-
UIL 的前例:ImageLoader.getInstance().loadImage(imageUrl, new ImageSize(viewWidth, viewheight), displayImageLoaderOptions, new SimpleImageLoadingListener() { @Override public void onLoadingComplete(String imageUri, View view, Bitmap加载图像) { mImageMap.setImageBitmap(loadedImage); } });尝试这样做。在设置要查看的图像时,您应该使用它。
-
@jaxon 看起来一切都很好,一旦尝试评论 fitImageToView();在 TouchIMageView 上的 setImageBitmap 方法并运行让我知道以防出现问题
标签: android bitmap android-memory