gpt4 book ai didi

java - 大图像的 Android OutOfMemoryError

转载 作者:行者123 更新时间:2023-11-30 02:57:48 24 4
gpt4 key购买 nike

对于尺寸较大的图像(不是分辨率),该方法会抛出 OutOfMemoryError我有 12 张 MP 照片,它们的大小各不相同(1.5MB、2.5MB、3.2MB、4.1MB)等,它们的分辨率都是相同的 4000 x 3000(像素)。

resizer 方法适用于大小小于 3MB 的图像,但对于那些大于 3MB 的图像,它会抛出 OutOfMemoryError 我不知道有什么可以解决它,我的应用程序主要用于调整大图像的大小,这真的让我着迷无处可去。

resizer方法是:

public File resizeBitmap(String imPath, int reqSize) {
File fl = null;
try{

// First decode with inJustDecodeBounds=true to check dimensions

final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;

BitmapFactory.decodeFile(imPath, options);

// Calculate inSampleSize

options.inSampleSize = calculateInSampleSize(options, reqSize);

// Decode bitmap with inSampleSize set

options.inJustDecodeBounds = false;
Bitmap saveImg = BitmapFactory.decodeFile(imPath, options);

//save resized image
String tmpName = "IMG_"+String.valueOf(System.currentTimeMillis()) + ".jpg";
fl = fileCache.getRawFile(tmpName);
FileOutputStream fos = new FileOutputStream(fl);
saveImg.compress(Bitmap.CompressFormat.JPEG, 95, fos);
saveImg.recycle();


return fl;
}
catch (Throwable eex){
eex.printStackTrace();
if(eex instanceof OutOfMemoryError) {
runOnUiThread(new Runnable() {
public void run() {
Toast.makeText(Resizer.this, "Memory ran out", Toast.LENGTH_SHORT).show();
}
});
}
return null;
}
}
//Method for calculating insamplesize
public int calculateInSampleSize(BitmapFactory.Options options, int reqSize) {
// Raw height and width of image
final int height = options.outHeight;
final int width = options.outWidth;
Log.i("width",""+width);
Log.i("height",""+height);
int inSampleSize = 1;

if (height > reqSize || width > reqSize) {

if (width > height) {
inSampleSize = Math.round((float) height / (float) reqSize);
} else {
inSampleSize = Math.round((float) width / (float) reqSize);
}


}

return inSampleSize;

//Stacktrace
04-11 09:01:19.832: W/System.err(8832): java.lang.OutOfMemoryError
04-11 09:01:19.953: W/System.err(8832): at android.graphics.BitmapFactory.nativeDecodeStream(Native Method)
04-11 09:01:19.972: W/System.err(8832): at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:529)
04-11 09:01:19.993: W/System.err(8832): at com.scale.app.Resizer.resizeBitmap(Resizer.java:1290)

最佳答案

Read Article

图像尺寸:4000*3000 像素

当图像加载时:4000*3000*4 = ?知识库

因此,Android 设备的虚拟堆内存为:32 MB、64 MB、128 MB ...等等

如果您使用:

<application

android:largeHeap="true">

</application>

这将使 VHM 增加一倍(如果 32 MB = 2* 32 MB)。但是这不是一个好方法,对操作系统有影响

您需要减小图像的尺寸。


使用下面的类并传递图像的路径和你想要的宽度,高度

Bitmap bitmap = BitmapSize.getDecodedBitmap(path, 400, 400);

类::::

public class BitmapSize{



public static Bitmap getDecodedBitmap(String path, float target_width, float target_height) {
Bitmap outBitmap = null;
try {
Options decode_options = new Options();
decode_options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(path,decode_options); //This will just fill the output parameters
int inSampleSize = calculateInSampleSize(decode_options, target_width, target_height);

Options outOptions = new Options();
outOptions.inJustDecodeBounds = false;
outOptions.inSampleSize = inSampleSize;
outOptions.inPreferredConfig = Bitmap.Config.ARGB_8888;
outOptions.inScaled = false;

Bitmap decodedBitmap = BitmapFactory.decodeFile(path,outOptions);
outBitmap = Bitmap.createScaledBitmap(decodedBitmap,// (int)target_width, (int)target_height, true);
(int)((float)decodedBitmap.getWidth() / inSampleSize),
(int)((float)decodedBitmap.getHeight() / inSampleSize), true);
System.out.println("Decoded Bitmap: Width " + outBitmap.getWidth() + " Height = " + outBitmap.getHeight() + " inSampleSize = " + inSampleSize);

} catch (Exception e) {
// TODO: handle exception
}

return outBitmap;
}

public static int calculateInSampleSize(Options options, float reqWidth, float reqHeight) {
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;

if (height > reqHeight || width > reqWidth) {

final int heightRatio = Math.round((float) height
/ (float) reqHeight);
final int widthRatio = Math.round((float) width / (float) reqWidth);

inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
}

return inSampleSize;
}

}

关于java - 大图像的 Android OutOfMemoryError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23008913/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com