Java 尝试使用位图减小字节数据大小时发生OutOfMemoryError
在我的应用程序中,当我上传图像时,我想减少数据,因此我调用此函数。但这段代码给了我一个OutOfMemory错误 LOGCAT:09-17 15:32:01.712:E/AndroidRuntime(7771):位于com.technow.pereo.FileHelper.reduceImageForUpload(FileHelper.java:64)Java 尝试使用位图减小字节数据大小时发生OutOfMemoryError,java,android,performance,bitmap,Java,Android,Performance,Bitmap,在我的应用程序中,当我上传图像时,我想减少数据,因此我调用此函数。但这段代码给了我一个OutOfMemory错误 LOGCAT:09-17 15:32:01.712:E/AndroidRuntime(7771):位于com.technow.pereo.FileHelper.reduceImageForUpload(FileHelper.java:64) 导致此错误的原因是什么?我如何解决它 似乎ImageResizer是一个自定义类。在将图像用于进一步处理之前,请使用下面的代码段重新调整图像的大
导致此错误的原因是什么?我如何解决它 似乎ImageResizer是一个自定义类。在将图像用于进一步处理之前,请使用下面的代码段重新调整图像的大小
//decodes image and scales it to reduce memory consumption
private Bitmap decodeFile(File f){
try {
//decode image size
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
BitmapFactory.decodeStream(new FileInputStream(f),null,o);
//Find the correct scale value. It should be the power of 2.
final int REQUIRED_SIZE=70;
int width_tmp=o.outWidth, height_tmp=o.outHeight;
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(new FileInputStream(f), null, o2);
} catch (FileNotFoundException e) {}
return null;
}
//解码图像并对其进行缩放以减少内存消耗
私有位图解码文件(文件f){
试一试{
//解码图像大小
BitmapFactory.Options o=新的BitmapFactory.Options();
o、 inJustDecodeBounds=true;
解码流(新的FileInputStream(f),null,o);
//找到正确的刻度值。它应该是2的幂。
所需的最终int_尺寸=70;
内部宽度=o.向外宽度,高度=o.向外高度;
int标度=1;
while(true){
如果(width_tmp/2)您可以添加一些关于错误发生的确切位置(即在哪一行)的信息。当然,一般来说,“内存不足”意味着没有剩余的可用内存来存储其他数据…什么是ImageResizer
?您自己的类?这将是问题所在,我们必须看到。是的,这是我自己的类
//decodes image and scales it to reduce memory consumption
private Bitmap decodeFile(File f){
try {
//decode image size
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
BitmapFactory.decodeStream(new FileInputStream(f),null,o);
//Find the correct scale value. It should be the power of 2.
final int REQUIRED_SIZE=70;
int width_tmp=o.outWidth, height_tmp=o.outHeight;
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(new FileInputStream(f), null, o2);
} catch (FileNotFoundException e) {}
return null;
}