CircleImageView 和 setBackgroundResource 出现 OutOfMemory android 错误

OutOfMemory android error with CircleImageView and setBackgroundResource

我有一个 activity,其中我使用了 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 Storage中的图片时。

另一个资源:

mImage.setBackgroundResource(R.drawable.m_air);

虽然 R.drawable.m_air 是保存在我的可绘制文件夹中的可绘制文件

我正在收到

java.lang.OutOfMemoryError: Failed to allocate a 14745612 byte allocation with 10530520 free bytes and 10MB until 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

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

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