内存不足错误,在 TouchImageview 上设置位图
Out of Memory Error ,set bitmap on TouchImageview
在我的应用程序中,我使用触摸图像视图为 user.For 启用缩放和裁剪选项,首先我使用此方法将图像 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));
在完成所有这些缩放和解码技术之后,我仍然遇到内存错误,我该如何解决这个问题,有人能帮忙吗??
作为快速修复,您可以尝试将其添加到清单文件中的应用程序标签中:
android:largeHeap="true"
但是:
Never request a large heap simply because you've run out of memory and you need a quick fix—you should use it only when you know exactly where all your memory is being allocated and why it must be retained.
尝试使用 UIL displayImage 函数将图像加载到 TouchIamgeView。
试试这个;
ImageLoader imageloader;
TouchImageView touchImageView = (TouchImageView)findViewById(R.id.);
imageLoader.displayImage(imageURL,touchImageView);
在我的应用程序中,我使用触摸图像视图为 user.For 启用缩放和裁剪选项,首先我使用此方法将图像 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));
在完成所有这些缩放和解码技术之后,我仍然遇到内存错误,我该如何解决这个问题,有人能帮忙吗??
作为快速修复,您可以尝试将其添加到清单文件中的应用程序标签中:
android:largeHeap="true"
但是:
Never request a large heap simply because you've run out of memory and you need a quick fix—you should use it only when you know exactly where all your memory is being allocated and why it must be retained.
尝试使用 UIL displayImage 函数将图像加载到 TouchIamgeView。
试试这个;
ImageLoader imageloader;
TouchImageView touchImageView = (TouchImageView)findViewById(R.id.);
imageLoader.displayImage(imageURL,touchImageView);