大佬教程收集整理的这篇文章主要介绍了Java-大图像的Android OutOfMemoryError,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
该方法对大尺寸图像抛出OutOfMemoryError(不是按分辨率)
我有12张MP照片,它们的大小都不同(1.5MB,2.5MB,3.2MB,4.1MB)等,并且它们的分辨率均为4000 x 3000(像素).
resizer方法适用于大小小于3MB的图像,但是对于那些大于3MB的图像,它会抛出OutOfMemoryError我不知道该如何解决,我的应用程序主要用于调整大图像的大小,这真的使我无处可去.
调整大小的方法是:
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)
解决方法:
图片尺寸:4000 * 3000像素
当图像加载时:4000 * 3000 * 4 =? KB
因此,Android设备的虚拟堆内存为:32 MB,64 MB,128 MB …
如果您使用:
<application
android:largeHeap="true">
</application>
这将使VHM增加一倍(如果32 MB = 2 * 32 MB).注意,这不是实现此目的的好方法,对OS产生影响
您需要减小图像的尺寸.
使用下面的类并传递图像的路径和width,height您想要的
类::::
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全部内容,希望文章能够帮你解决Java-大图像的Android OutOfMemoryError所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。