我知道有很多关于“用android加载图像”主题的线程,但不幸的是我没有找到解决我的任何问题的解决方案。因此,这里是我的问题:Android的BitmapFactory不加载尺寸
我要救一个大的图像,另一个用于后裁剪,这里是我的代码:
BitmapFactory.Options bmOptions = new BitmapFactory.Options();
bmOptions.inJustDecodeBounds = true;
BitmapFactory.decodeFile(image, bmOptions);
final int REQUIRED_SIZE = 800;
int scale = 1;
while (bmOptions.outWidth/scale/2 >= REQUIRED_SIZE && bmOptions.outHeight/scale/2 >= REQUIRED_SIZE) {
scale *= 2;
}
bmOptions.inJustDecodeBounds = false;
bmOptions.inSampleSize = scale;
bmOptions.inPurgeable = true;
bmOptions.inInputShareable = true;
// create the Image for the original File
try {
File origFile = File.createTempFile(origImage, JPEG_FILE_SUFFIX, getAlbumDir());
OutputStream fOut2 = new FileOutputStream(origFile);
Bitmap thePic = BitmapFactory.decodeFile(image, bmOptions);
thePic.compress(Bitmap.CompressFormat.JPEG, 100, fOut2);
fOut2.flush();
fOut2.close();
} catch (Exception e) {
Log.e(TAG, "Cannot create original Image");
}
mImageBitmap = BitmapFactory.decodeFile(image, bmOptions);
此代码〜90%的时间。但有时bmOptions.outHeight
& bmOptions.outWidth returns -1
和行Bitmap thePic = BitmapFactory.decodeFile(image, bmOptions);
我得到异常:
10-31 12:07:31.645: E/AndroidRuntime(16618): java.lang.OutOfMemoryError
10-31 12:07:31.645: E/AndroidRuntime(16618): at android.graphics.BitmapFactory.nativeDecodeStream(Native Method)
10-31 12:07:31.645: E/AndroidRuntime(16618): at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:652)
10-31 12:07:31.645: E/AndroidRuntime(16618): at android.graphics.BitmapFactory.decodeFile(BitmapFactory.java:391)
我认为这个问题可以是:
- 我采取多种照片背靠背经过一段时间这个错误可能 发生
- 它发生在我把我的显示器虽然拍摄照片
但经过大量测试后,我仍然不确定这两种可能性中的任何一种是否属实,或者是否有其他问题。
有没有人知道我在做什么错?
编辑:
后大量的测试与我的应用程序,我有以下问题:
每次启动过程中,我失去〜2 MB的RAM。这意味着一段时间后我的应用程序将关闭。
我做了什么来解决这个问题:
- )不创建原始图像 - )设定的样本量为16(而不是计算2) - )完全去除位图
问题保持不变;我总是丢失2 MB RAM。有没有人知道问题可能是什么?
“bmOptions.inJustDecodeBounds = true;”比在Android的另一个错误?解决方案之一就是编写你自己的解码rutine来只读大小。在调用该代码之前请检查您的应用程序有多少可用内存。如果你有很少的:只有1 MB或类似的东西,比你的应用程序已经使用很多,没有地方执行代码 – 2013-10-31 11:54:46