【发布时间】:2014-12-10 19:29:10
【问题描述】:
我知道这个question,据我所知,我实际上正在做与最佳答案建议相同的事情。但是我仍然在内存较少的设备上遇到 outOfMemory 崩溃。
编辑:我只是用手机的原生相机拍照。没什么好看的。
private Bitmap setImgViewFromFile(ImageView imgView, String file) {
// Get the dimensions of the View
int targetW = Math.max(imgView.getWidth(), 600);
int targetH = Math.max(imgView.getHeight(), 800);
// Get the dimensions of the bitmap
BitmapFactory.Options bmOptions = new BitmapFactory.Options();
bmOptions.inJustDecodeBounds = true;
BitmapFactory.decodeFile(file, bmOptions);
int photoW = bmOptions.outWidth;
int photoH = bmOptions.outHeight;
// Determine how much to scale down the image
int scaleFactor = Math.min(photoW / targetW, photoH / targetH);
// Decode the image file into a Bitmap sized to fill the View
bmOptions.inJustDecodeBounds = false;
bmOptions.inSampleSize = scaleFactor;
bmOptions.inPurgeable = true;
Bitmap bitmap = BitmapFactory.decodeFile(file, bmOptions);
imgView.setImageBitmap(bitmap);
return bitmap;
}
堆栈跟踪:
12-10 10:38:04.120 4144-4144/com.loop E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.loop, PID: 4144
java.lang.OutOfMemoryError
at android.graphics.BitmapFactory.nativeDecodeStream(Native Method)
at android.graphics.BitmapFactory.decodeStreamInternal(BitmapFactory.java:613)
at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:589)
at android.graphics.BitmapFactory.decodeFile(BitmapFactory.java:369)
谁能看出我做错了什么?
【问题讨论】:
-
请记住,不能保证任何特定的样本大小都会起作用,因为不能保证有足够大的空闲内存块用于任何给定的位图。
-
你的比例计算不对。但是由于您现在将缩小太多,因此不会导致内存不足错误。内存小的设备的屏幕是否大于 800x600?
-
只有 2 的幂才能保证为比例因子。那个相机的分辨率是多少。还有你内存不足的相机?
标签: android