在我的应用程序中,我使用Touch imageview为用户启用缩放和裁剪选项。首先,我已使用此方法将图像uri转换为位图。
Uri to bitmap
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));
在完成所有这些缩放和解码技术之后,我仍然摆脱了记忆错误,我怎么能解决这个问题谁能帮忙?
答案 0 :(得分:1)
尝试使用UIL displayImage函数将Image加载到TouchIamgeView。
试试这个;
ImageLoader imageloader;
TouchImageView touchImageView = (TouchImageView)findViewById(R.id.);
imageLoader.displayImage(imageURL,touchImageView);
答案 1 :(得分:-1)
作为快速修复,您可以尝试将其添加到Manifest文件中的Application标记:
android:largeHeap="true"
BUT:
永远不要仅仅因为内存耗尽而需要快速修复而请求大堆 - 只有当您确切知道所有内存的分配位置以及必须保留的原因时才应使用它。