2

I have been trying to load a very basic activity with on ImageButton, 2 textViews, a textField and an ImageView.

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="false"
android:id="@+id/namescreenactivity">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text="Welcome to your Pet House"
    android:id="@+id/Welcome"
    android:textColor="#ff000000"
    android:textStyle="bold"
    android:layout_marginTop="94dp"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true" />

<EditText android:id="@+id/edit_message"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:hint="@string/edit_message"
    android:layout_marginTop="36dp"
    android:textColor="#ff000000"
    android:layout_below="@+id/Welcome"
    android:layout_alignEnd="@+id/Welcome" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:text="Name your pet"
    android:id="@+id/NameLabel"
    android:layout_alignTop="@+id/edit_message"
    android:layout_alignStart="@+id/Welcome" />

<ImageView
    android:layout_width="100dp"
    android:layout_height="100dp"
    android:src="@drawable/sun"
    android:layout_alignBottom="@+id/Welcome"
    android:layout_alignParentEnd="true"
    android:layout_marginBottom="39dp"
    android:id="@+id/sun" />

<ImageButton
    android:layout_width="250dp"
    android:layout_height="250dp"
    android:id="@+id/imageButton"
    android:src="@drawable/camel"
    android:clickable="true"
    android:scaleType="fitXY"
    android:onClick="sendMessage"
    android:layout_alignParentBottom="true"
    android:layout_alignEnd="@+id/edit_message"
    android:layout_marginBottom="31dp" />

And in MainActivity.java's OnCreate function I am simply calling setContentView(R.layout.activity_main); However, my application's memory usage graph shows that I am utilizing around 44MB (Although the total size of all my images in the app is less than 1MB and the one used for the ImageButton is 193KB).

I believe that this is the reason my app crashes and throws an Out of Memory error

Caused by: java.lang.OutOfMemoryError: Failed to allocate a 138018828 byte allocation with 4194304 free bytes and 35MB until OOM
        at dalvik.system.VMRuntime.newNonMovableArray(Native Method)
        at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
        at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:609)
        at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:444)
        at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:988)
        at android.content.res.Resources.loadDrawableForCookie(Resources.java:2474)
        at android.content.res.Resources.loadDrawable(Resources.java:2381)
        at android.content.res.TypedArray.getDrawable(TypedArray.java:749)
at android.widget.ImageView.<init>(ImageView.java:146)
at android.widget.ImageButton.<init>(ImageButton.java:86)
at android.widget.ImageButton.<init>(ImageButton.java:82)
at android.widget.ImageButton.<init>(ImageButton.java:78)
at java.lang.reflect.Constructor.newInstance(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:288)
at android.view.LayoutInflater.createView(LayoutInflater.java:607)
at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:55)
at android.view.LayoutInflater.onCreateView(LayoutInflater.java:682)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:741)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:378)
at android.app.Activity.setContentView(Activity.java:2145)
at com.example.healthypetdhr.MainActivity.onCreate(MainActivity.java:37)
at android.app.Activity.performCreate(Activity.java:5990)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5257)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)'

Please help me figure out what's wrong and how I can resolve this issue.

3
  • Can you please also provide the "@drawable/sun" and "@drawable/camel" images? Commented Jul 6, 2015 at 8:35
  • 1
    Keep in mind that images are decoded to a bitmap when they are drawn. This is normal but requires much more memory than the file in your APK. Commented Jul 6, 2015 at 8:46
  • Don't load the large images directly to imageview, instead use the bitmap options to resize it to the desire size and than set to imageview. Commented Jul 6, 2015 at 9:11

3 Answers 3

3

it may happen when Images are big and when they are placed in drawable(ldpi/mdpi...) folder so android OS tends to resize them according to need, but in your case you said they are very small, still try putting those images inside:

res/drawable-nodpi

folder as it avoids them from rescaling and to be used as it is.

Sign up to request clarification or add additional context in comments.

Comments

1

It might require more memory space after the images are decoded to bitmap, you can use bitmap options, modify the parameters to resize it;

You might want to clean and refresh your project;

Check the res folder as people said;

Refer this to study more: https://developer.android.com/training/displaying-bitmaps/manage-memory.html

Comments

0

It is highly probable that the drawables you are using cause this problem. You may want to refer to his question Android Outofmemory drawable

1 Comment

how should I use the getAssetImage() method mentioned in the answer? can you please guide me?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.