Search code examples
androidout-of-memoryhorizontalscrollview

HorizontalScrollView OutOfMemoryError


I'm trying to do a HorizontalScrollView with 5 pictures. I implemented the xml like this

  <?xml version="1.0" encoding="utf-8"?>
<HorizontalScrollView xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/gallery"
        android:layout_width="match_parent"
        android:layout_height="match_parent" 
        android:spacing="40dp"
        android:scrollbars="horizontal"
        android:fadeScrollbars="false"
        android:layout_marginBottom="50dp">

<LinearLayout 
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" 
    android:background="@drawable/background"/>

</HorizontalScrollView>

and the code is this

package com.infobest.praiser.activity;

import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.widget.HorizontalScrollView;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.infobest.praiser.R;
import com.infobest.praiser.actionbar.ActionBarActivity;

public class TutorialActivity extends ActionBarActivity
{
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.tutorial);
        Integer[] mImageIds = {R.drawable.tutorial1, R.drawable.tutorial2, R.drawable.tutorial3,
            R.drawable.tutorial4, R.drawable.tutorial5};
        HorizontalScrollView h = (HorizontalScrollView) this.findViewById(R.id.gallery);
        LinearLayout topLinearLayout = new LinearLayout(this);
        topLinearLayout.setOrientation(LinearLayout.HORIZONTAL);


        for (int i = 0; i < 5; i++)
        {
            final ImageView imageView = new ImageView(this);

            imageView.setImageResource(mImageIds[i]);
            topLinearLayout.addView(imageView);

        }
        h.addView(topLinearLayout);


    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu)
    {
        MenuInflater menuInflater = getMenuInflater();
        menuInflater.inflate(R.menu.menu_tutorial, menu);

        // Calling super after populating the menu is necessary here to ensure
        // that the
        // action bar helpers have a chance to handle this event.
        return super.onCreateOptionsMenu(menu);
    }


}

what exactly am i doing wrong because i'm getting OutOfMemoryError

LogCat

      FATAL EXCEPTION: main  java.lang.OutOfMemoryError     at
 android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)    at
 android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:483) 
    at
 android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:351)
    at
 android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:773)
    at android.content.res.Resources.loadDrawable(Resources.java:1937) 
    at android.content.res.Resources.getDrawable(Resources.java:664)    at
 android.widget.ImageView.resolveUri(ImageView.java:542)    at
 android.widget.ImageView.setImageResource(ImageView.java:315)      at
 com.infobest.praiser.activity.TutorialActivity.onCreate(TutorialActivity.java:30)
    at android.app.Activity.performCreate(Activity.java:4465)   at
 android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
    at
 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1919)
    at
 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1980)
    at android.app.ActivityThread.access$600(ActivityThread.java:122) 
    at
 android.app.ActivityThread$H.handleMessage(ActivityThread.java:1146) 
    at android.os.Handler.dispatchMessage(Handler.java:99)      at
 android.os.Looper.loop(Looper.java:137)    at
 android.app.ActivityThread.main(ActivityThread.java:4340)      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:784)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)     at
 dalvik.system.NativeStart.main(Native Method)

Solution

  • Your images are taking up too much memory, I had the same problem and what fixed it for me was to follow the guide found here:

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

    It contains 4 tutorials on how to optimally display bitmaps without running out of memory. It can be kind of confusing, but it works really well.

    On the other hand, if you want a shortcut, you can always just manually scale down your images in GIMP or something. Although this would not solve the long term problem if users will be choosing their own pictures for your app.