【问题标题】:OutOfMemory android error with CircleImageView and setBackgroundResourceCircleImageView 和 setBackgroundResource 的 OutOfMemory android 错误
【发布时间】:2018-03-10 23:13:52
【问题描述】:

我在一项活动中使用了 2 种资源:

1 个圆形图像,如下所示:

String image = dataSnapshot.child("thumb_image").getValue().toString();
Picasso.with(pilotProfileImage.getContext()).load(image)
    .placeholder(R.drawable.pilot_default).into(pilotProfileImage);

当 thumb_image 是存储在 Firebase 存储中的图片时。

另一个资源:

mImage.setBackgroundResource(R.drawable.m_air);

虽然R.drawable.m_air 是一个保存在我的drawable 文件夹中的drawable

我收到了

java.lang.OutOfMemoryError: 无法分配 14745612 字节分配,10530520 可用字节和 10MB 直到 OOM

我已经看到Bitmap 的解决方案,但我没有在这里使用位图。

我试过了

mDroneImage.setImageBitmap(decodeSampledBitmapFromResource(getResources(), R.drawable.mavic_air, 100, 100))

在这个解决方案中:Strange out of memory issue while loading an image to a Bitmap object

但应用仍然崩溃。此外,它不能解决 firebase 图像问题。

我该如何解决?

编辑: 在文档中找到了 firebase 问题的答案: https://github.com/codepath/android_guides/wiki/Displaying-Images-with-the-Picasso-Library

【问题讨论】:

  • 图片分辨率是多少?
  • Firebase' 图片:2400*2400 jpg ,drawable:640*640 png
  • 好吧,我用过:Picasso.with(pilotProfileImage.getContext()).load(image) .fit().placeholder(R.drawable.pilot_default).into(pilotProfileImage);添加了 fit(),并且还减少了占位符的大小 - Pilot_default 到小于 500dx,它解决了问题。可以做的另一件事是减小可绘制图片的大小,但目前它可以在没有它的情况下工作
  • 这是一个不相关的 Firebase 存储问题。你试图在内存中放入一个巨大的图像。

标签: android firebase out-of-memory android-image


【解决方案1】:

如果您要加载 2400x2400 像素的图像,您将加载大约 16MB 的数据。您当前正在使用的进程将所有数据加载到内存中以显示它。显然,您尝试运行此代码的手机没有那么多可用内存。

Picasso documentation on dealing with OutOfMemory errors while loading an image 有一篇很棒的文章。其中一些方法会调整图像大小以减少内存使用量,因此这些方法应该是您减少内存使用量的一种方法。

【讨论】:

  • 谢谢你的详细信息,我现在明白了。我用毕加索文档解决了这个问题:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-10-16
  • 1970-01-01
  • 2016-11-26
  • 2015-01-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多