【问题标题】:OutOfMemory Error Android: [duplicate]Android OutOfMemory 错误:[重复]
【发布时间】:2014-10-16 01:10:31
【问题描述】:

我正在尝试使用以下代码将图像转换为位图:

//Here file is the image file
Bitmap bm = BitmapFactory.decodeFile(file.getAbsolutePath());

现在问题是有时我得到了OutOfMemoryError。我不知道错误到底是什么时候发生的。

我的简单解决方案添加尝试捕获添加并避免崩溃。我知道这不是正确的方法。

这里如何处理OutOfMemoryError

【问题讨论】:

  • 内存不足。缩小图像
  • 您可以尝试使用 BitmapFactory.Options。但有可能文件太大而无法作为位图加载到内存中。
  • @Raghunandan 如何进行缩放?
  • @Joris 是的,我正在处理大文件,所以如何避免这种情况

标签: android android-imageview android-bitmap


【解决方案1】:

在解码中添加此选项以优化内存:

BitmapFactory.Options bmOptions = new BitmapFactory.Options();
bmOptions.inPurgeable = true;
bmOptions.inSampleSize = 2;
Bitmap bmp = BitmapFactory.decodeFile(file.getAbsolutePath(), bmOptions);

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-01-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多