【发布时间】:2014-06-19 03:36:03
【问题描述】:
我正在使用以下代码来放大图像。
bmp=new BitmapFactory().decodeFile(util.getPathFromUri(tempFile));
Bitmap scaledBitmap=Bitmap.createScaledBitmap(bmp, newWidth, newHeight, true); //Line 2
ByteArrayOutputStream bos=new ByteArrayOutputStream();
scaledBitmap.compress(CompressFormat.JPEG, 100, bos);
bmp.recycle();
bmp=null;
OutputStream out;
out = new FileOutputStream(util.getTempFileName());
bos.writeTo(out);
bos.flush();
但有时 OutOfMemoryError 发生在第 2 行并且应用程序崩溃,我尝试将此代码包含在 try-catch 中,但我的应用程序仍然崩溃,因为 try-catch 仅捕获异常,createScaledBitmap() 函数也仅抛出 IllegalArgumentException。
因为,我不想显示图像,因此我不需要缩小它(正如我在 SOF 的其他问题中看到的那样)。
那么,如果我使用它(newWidth,newHeight),我如何预先检测到 OutOfMemoryError 会发生。有没有办法计算(newWidth, newHeight)的Bitmap在内存中所需的字节数和可以分配给bitmap的最大可用内存?
请帮忙!
【问题讨论】:
标签: android bitmap out-of-memory