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 a Self-Destructing Message Android App Sending Messages Adding a File to the Message: Part 2

OutOfMemoryError

please I'm having these error on my app... and i don't know why.. here's my LogCat:

java.lang.OutOfMemoryError at android.graphics.BitmapFactory.nativeDecodeByteArray(Native Method) at android.graphics.BitmapFactory.decodeByteArray(BitmapFactory.java:551) at com.example.andrey.login.ImageResizer.resizeImage(ImageResizer.java:28) at com.example.andrey.login.ImageResizer.resizeImageMaintainAspectRatio(ImageResizer.java:57) at com.example.andrey.login.FileHelper.reduceImageForUpload(FileHelper.java:67) at com.example.andrey.login.RecipientsActivity.createMessage(RecipientsActivity.java:143) at com.example.andrey.login.RecipientsActivity.onOptionsItemSelected(RecipientsActivity.java:101)

4 Answers

Adrian Randall
Adrian Randall
4,807 Points

How big is your image?

It may not have enough memory allocated depending on how you setup or what your environment is. Or it could be a loop or anything...paste your code from where it's happening

i just realize that is the image that was too big, i tries added a smaller image and it worked. But does that mean that i cant send a good image or just a bad quality ones

Adrian Randall
Adrian Randall
4,807 Points

What's it for? And how big was your image?

You should be able to get pretty good resolution images loading with no issues...

it was 2.6 MB

Adrian Randall
Adrian Randall
4,807 Points

Mate that's huge!

If you have Photo Shop save it out as a JPEG and reduce the size of it. Most images that size have massive px heights and widths so resize to what it's going to be displayed at. If you're displaying at 300px by 300px make sure the image isn't 2000px by 2000px..this would give you an Out of Memory Exception in some cases :)

And if it needs an extra bit of love once it's resized (which it shouldnt) you can optimize it here: https://kraken.io/web-interface

If your image has to be super high res then there is a config in Android that allows you to set how much memory is allocated. However I'd recommend against this as it give poor perfromance

Ok now I get it, but my question is how apps like Whatsapp can send and retrieve an image that big?

Adrian Randall
Adrian Randall
4,807 Points

I don't think they do, I think they compress it for display purposes

OR

If they do they use streaming through sockets instead of brute loading

Google around for "image stream to byte array" or something like that, I haven't done pure Android in ages, been doing Cordova lately

ok ok thanks man!!! know I understand!!!! God Bless you man!!