4

I am trying to show a slideshow of images more than 50 images. Whenever I run it though, I get an OutOfMemoryError. My code is:

public class SlideshowActivity extends Activity {

private ViewFlipper myViewFlipper;
int[] image = { R.drawable.one_facebook, R.drawable.two_facebook,
        R.drawable.three_facebook, R.drawable.four_facebook, R.drawable.five_facebook,
        R.drawable.six_facebook, R.drawable.seven_facebook, R.drawable.eight_facebook,
        R.drawable.nine_facebook, R.drawable.ten_facebook, R.drawable.eleven_facebook,
        R.drawable.onetwo_facebook, etc. };

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_slideshow);
    myViewFlipper = (ViewFlipper) findViewById(R.id.myflipper);

    for (int i = 0; i < image.length; i++) {
        ImageView imageView = new ImageView(SlideshowActivity.this);
        imageView.setImageResource(image[i]);
        myViewFlipper.addView(imageView);

    }
    myViewFlipper.setAutoStart(true);
    myViewFlipper.setFlipInterval(1000);
    myViewFlipper.startFlipping();
}

My logcat shows:

E/AndroidRuntime: FATAL EXCEPTION: main Process: com.applications.codegeek.slideshow, PID: 10126 java.lang.OutOfMemoryError: Failed to allocate a 52920012 byte allocation with 4194304 free bytes and 31MB until OOM at dalvik.system.VMRuntime.newNonMovableArray(Native Method) at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method) at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:620) at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:455) at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:1155) at android.content.res.ResourcesImpl.loadDrawableForCookie(ResourcesImpl.java:720) at android.content.res.ResourcesImpl.loadDrawable(ResourcesImpl.java:571) at android.content.res.Resources.getDrawable(Resources.java:771) at android.content.Context.getDrawable(Context.java:525) at android.widget.ImageView.resolveUri(ImageView.java:856) at android.widget.ImageView.setImageResource(ImageView.java:448) at com.applications.codegeek.slideshow.SlideshowActivity$override.onCreate(SlideshowActivity.java:29) at com.applications.codegeek.slideshow.SlideshowActivity$override.access$dispatch(SlideshowActivity.java) at com.applications.codegeek.slideshow.SlideshowActivity.onCreate(SlideshowActivity.java:0) at android.app.Activity.performCreate(Activity.java:6679) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2618) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726) at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:4519) at android.app.ActivityThread.-wrap19(ActivityThread.java) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1483) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:154) at android.app.ActivityThread.main(ActivityThread.java:6119) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)

Any help would be appreciated! Thanks!

2
  • 3
    why don't you use image loader libraries like glide, picasso etc Commented Jan 21, 2017 at 19:55
  • 1
    What I found on Glide For Github was that it is useful to show images. I might have understood it wrong though. About picasso I saw some visual examples about it and what I got out of it is that it is useful for collages. I might have understood it wrong and if so, then please tell me what it actualy does. Thank you! Commented Jan 22, 2017 at 4:04

1 Answer 1

1

For reduce memory you can use this plugin Android Drawable Importer Plugin

And too..

android:largeHeap="true" in your Manifest It will increase your heap size and avoid the OutOfMemory

Good luck!

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

3 Comments

Its still not working. I already had the largeHeap in my manifest but I converted all my images to ldpi but it still shows this error. Please help
try with put in your gradle dexOptions android { compileSdkVersion 25 buildToolsVersion '23.0.3' dexOptions { preDexLibraries = false javaMaxHeapSize "4g" }
Nope, I still get that same error. Thanks for trying though.

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.