2

Hi I am trying to create an android application to display images so that you can click on a 'next' button and then the next image shows. So far I have this method

public void buttonClick(){
        imgView = (ImageView)findViewById(R.id.imgView);
        buttonS = (Button)findViewById(R.id.button);

    buttonS.setOnClickListener(

            new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    current_image_index++;
                    current_image_index = current_image_index % images.length;
                    imgView.setImageResource(images[current_image_index]);

                }
            }
    );

}

However when I try to run this, I get a java.lang.OutOfMemoryError when I click the 'next' button twice. Am I doing something wrong? My image array looks like this:

int[] images = {R.drawable.img};

I have tried changing the Manifest file to include

android:largeHeap="true"

This is where I set the variables and call the method above

public class MainActivity extends AppCompatActivity {
    private static ImageView imgView;
    private static Button buttonS;
    private int current_image_index;

    int[] images = {R.drawable.dog,R.drawable.img};
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        buttonClick();
    }

The stack trace is:

08-29 23:17:34.781  15450-15450/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.daniel.firstapp, PID: 15450
java.lang.OutOfMemoryError: Failed to allocate a 2070252108 byte allocation with 16777216 free bytes and 482MB until OOM
        at dalvik.system.VMRuntime.newNonMovableArray(Native Method)
        at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
        at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:816)
        at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:637)
        at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:1019)
        at android.content.res.Resources.loadDrawableForCookie(Resources.java:3778)
        at android.content.res.Resources.loadDrawable(Resources.java:3651)
        at android.content.res.Resources.getDrawable(Resources.java:1865)
        at android.content.Context.getDrawable(Context.java:408)
        at android.widget.ImageView.resolveUri(ImageView.java:752)
        at android.widget.ImageView.setImageResource(ImageView.java:408)
        at com.example.daniel.firstapp.MainActivity$1.onClick(MainActivity.java:40)
        at android.view.View.performClick(View.java:5217)
        at android.view.View$PerformClick.run(View.java:20983)
        at android.os.Handler.handleCallback(Handler.java:739)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:145)
        at android.app.ActivityThread.main(ActivityThread.java:6141)
        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:1399)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
7
  • Can you add more code of the class where you call this method and also more importantly add the stack trace Commented Aug 29, 2015 at 21:04
  • ok cool, will do that now. Commented Aug 29, 2015 at 21:04
  • There is a new error now Commented Aug 29, 2015 at 21:08
  • Please add the complete Stack trace which you think is good, This does not explain anything. And where is this current_image_index specified? Commented Aug 29, 2015 at 21:12
  • Sorry, where do I find the stack trace? I have included more code as well Commented Aug 29, 2015 at 21:13

1 Answer 1

4

It is because the assigned heap for the application to handle the memory for all the images is low. You can increase it in the Android manifest file.

specify

android:largeHeap="true"  

as a property of <application> tag

eg:

 <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:largeHeap="true"
        android:theme="@style/AppTheme" >

and see if that fixes the issue.

  1. One option is to scale down the images before you display it.
  2. Recycle the ImageView before you add the new image (But this will not hold up if the image is very large).
Sign up to request clarification or add additional context in comments.

4 Comments

how do I increase it?
and it only happens when I click the next button twice
@Daniel what do you mean by twice, is it when you click the button 2 times faster?
What happens is that imgView is initially displayed and then when I click once, it shows the img Image, but when I click again it says the app has stopped working

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.