Search code examples
androidfirebaseout-of-memoryandroid-image

OutOfMemory android error with CircleImageView and setBackgroundResource


I have an activity in which I use 2 resources:

1 round image, like this:

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

When thumb_image is a picture stored in Firebase Storage.

Another resource:

mImage.setBackgroundResource(R.drawable.m_air);

While R.drawable.m_air is a drawable saved in my drawable folder

I'm receiving

java.lang.OutOfMemoryError: Failed to allocate a 14745612 byte allocation with 10530520 free bytes and 10MB until OOM

I've seen solution with Bitmap but I'm not using bitmap here.

I've tried

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

In this solution: Strange out of memory issue while loading an image to a Bitmap object

but the app still crashes. In addition it doesn't solve the firebase image problem.

How can I solve it?

Edit: Found answers for the firebase problem in the docs: https://github.com/codepath/android_guides/wiki/Displaying-Images-with-the-Picasso-Library


Solution

  • If you're loading a 2400x2400 pixel image, you're loading about 16MB of data. The process you're currently using, loads all the data in memory in order to display it. Apparently the phone you're trying to run this code on doesn't have that amount of memory available.

    There is a great piece of Picasso documentation on dealing with OutOfMemory errors while loading an image. Some of these approaches resize the image to reduce memory usage, so those should be a way for you to reduce memory usage.