Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Android Build a Self-Destructing Message Android App Sending Messages Sending the Message

PD Nghia
PD Nghia
10,536 Points

java.lang.OutOfMemoryError

I test app Ribbit on device real but was an error with images. Size image is 3000x4000 Thanks!

03-20 18:55:14.550 3232-3232/ribbit.pdnghiadev.com.ribbit E/AndroidRuntime﹕ FATAL EXCEPTION: main java.lang.OutOfMemoryError at android.graphics.Bitmap.nativeCreate(Native Method) at android.graphics.Bitmap.createBitmap(Bitmap.java:605) at android.graphics.Bitmap.createBitmap(Bitmap.java:551) at android.graphics.Bitmap.createScaledBitmap(Bitmap.java:437) at ribbit.pdnghiadev.com.ribbit.ImageResizer.resizeImage(ImageResizer.java:29) at ribbit.pdnghiadev.com.ribbit.ImageResizer.resizeImageMaintainAspectRatio(ImageResizer.java:57) at ribbit.pdnghiadev.com.ribbit.FileHelper.reduceImageForUpload(FileHelper.java:67) at ribbit.pdnghiadev.com.ribbit.RecipientsActivity.createMessage(RecipientsActivity.java:184) at ribbit.pdnghiadev.com.ribbit.RecipientsActivity.onOptionsItemSelected(RecipientsActivity.java:134) at android.app.Activity.onMenuItemSelected(Activity.java:2516) at android.support.v4.app.FragmentActivity.onMenuItemSelected(FragmentActivity.java:350) at android.support.v7.app.ActionBarActivity.onMenuItemSelected(ActionBarActivity.java:155) at android.support.v7.app.ActionBarActivityDelegate$1.onMenuItemSelected(ActionBarActivityDelegate.java:74) at android.support.v7.app.ActionBarActivityDelegateBase.onMenuItemSelected(ActionBarActivityDelegateBase.java:556) at android.support.v7.internal.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:802) at android.support.v7.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:153) at android.support.v7.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:949) at android.support.v7.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:939) at android.support.v7.widget.ActionMenuView.invokeItem(ActionMenuView.java:596) at android.support.v7.internal.view.menu.ActionMenuItemView.onClick(ActionMenuItemView.java:145) at android.view.View.performClick(View.java:3517) at android.view.View$PerformClick.run(View.java:14155) at android.os.Handler.handleCallback(Handler.java:605) at android.os.Handler.dispatchMessage(Handler.java:92) at android.os.Looper.loop(Looper.java:154) at android.app.ActivityThread.main(ActivityThread.java:4624) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:511) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:809) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:576) at dalvik.system.NativeStart.main(Native Method)

4 Answers

The image resolution 3000x4000 is way too big and not necessary. You can scale the image down to 60% and it would still be more than enough.

PD Nghia
PD Nghia
10,536 Points

How to Scale? Can you help me?

i am having the same problem here please help me!!!! 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)
bessem mouelhi
bessem mouelhi
19,119 Points

You can scale the image in the "FileHelper.java" class. In the reduceImageForUpload() function, change 100 to 60.

public static byte[] reduceImageForUpload(byte[] imageData) {
        Bitmap bitmap = ImageResizer.resizeImageMaintainAspectRatio(imageData, SHORT_SIDE_TARGET);

        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.PNG, 60, outputStream);
        byte[] reducedData = outputStream.toByteArray();
        try {
            outputStream.close();
        }
        catch (IOException e) {
            // Intentionally blank
        }

        return reducedData;
    }