Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android Build an Interactive Story App (Retired) User Input Introducing ImageViews

Throwing OutOfMemory error and image not showing up in design preview.

This is part of my logcat: 2026-2026/com.tcw.interactivestory I/art﹕ Forcing collection of SoftReferences for 82MB allocation 04-21 16:46:33.035 2026-2026/com.tcw.interactivestory I/art﹕ Alloc concurrent mark sweep GC freed 3(96B) AllocSpace objects, 0(0B) LOS objects, 61% free, 633KB/1657KB, paused 1.930ms total 21.201ms 04-21 16:46:33.035 2026-2026/com.tcw.interactivestory E/art﹕ Throwing OutOfMemoryError "Failed to allocate a 86400012 byte allocation with 1048576 free bytes and 63MB until OOM" 04-21 16:46:33.036 2026-2026/com.tcw.interactivestory D/skia﹕ --- allocation failed for scaled bitmap 04-21 16:46:33.038 2026-2026/com.tcw.interactivestory D/AndroidRuntime﹕ Shutting down VM 04-21 16:46:33.042 2026-2026/com.tcw.interactivestory E/AndroidRuntime﹕ FATAL EXCEPTION: main Process: com.tcw.interactivestory, PID: 2026 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.tcw.interactivestory/com.tcw.interactivestory.MainActivity}: android.view.InflateException: Binary XML file line #7: Error inflating class android.widget.ImageView at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)

I hope that's legible. What's weird is that in the design preview the image selected for the 'src' in the ImageView doesn't show up.

2 Answers

Scale the resolution of the image down. The file size maybe too big.

Ben Junya
Ben Junya
12,365 Points

Working with images in Android is a *itch in general, as you can easily eat up all the memory with a few high resolution images - per your example and miguelcastro2's answer.

An easy fix would be to scale them down to a smaller size. However, there are other things you can do if you MUST use super high res images:

Check this out from the Android documentation. It's a little bit like spaghetti, but the stuff about LRUCache (Last Recently Used) is super helpful, and it's a trick I've used in building production level apps.

http://developer.android.com/training/displaying-bitmaps/load-bitmap.html

This seems to be mainly for apps and activities that load thumbnails. I need a way to import and use high resolution images as backgrounds when called on.

Ben Junya
Ben Junya
12,365 Points

Then you will definitely need to use LRUCache

http://developer.android.com/training/displaying-bitmaps/cache-bitmap.html

Read through the whole section on Displaying Bitmaps effectively. As an Android developer, you will always be referencing this documentation, and you're definitely going to be fighting this beast forever virtually.

Like I said, working with images in Android is a *itch.

Ben Junya
Ben Junya
12,365 Points

Looking closer at your logcat - You're attempting to allocate 82MB of memory for an application...

Being blunt and flat out here - that's absolutely ridiculous.

Most of the time, an individual Android app gets between 8 - 32 (and rarely 64+) MB of memory to play with. You need to manage your memory better.

Since you're fairly new to Android, I would seriously consider re-scoping your project, since diving into this heavy of memory management in the early phases of learning Android is very difficult. It would do you much better to start with a couple of smaller apps.

It's OK to screw up and fail, in fact, it's what makes us better as developers. You're totally on the right track when it comes to building your own stuff, but I feel that a project like this may be a bit too far out of scope for your skill level.

If you have questions, feel free to keep commenting in this thread. I'm happy to help any devs. I was in your shoes 14 months ago :D.