【发布时间】:2013-10-31 11:50:28
【问题描述】:
我知道有很多关于“使用 android 加载图像”主题的主题,但不幸的是,我没有在其中任何一个中找到解决问题的方法。所以这是我的问题:
我想保存一张大图和另一张以供以后裁剪,这是我的代码:
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的另一个错误?解决方案之一是编写自己的解码程序以只读大小。在调用该代码之前检查您的应用程序有多少可用内存。如果你有很少:只有 1 mb 或类似的东西,那么你的应用程序已经使用了很多并且没有地方执行代码
标签: android image-processing out-of-memory android-image