FATAL EXCEPTION: main java.lang.OutOfMemoryError at android.graphics.BitmapFactory.nativeDecodeStream(Native Method)

AndroidKrayze picture AndroidKrayze · Feb 18, 2014 · Viewed 11.1k times · Source

I keep getting this error when I run my app. The app will compile fine and once I start interacting with it (ImageSlider) sometimes it breaks and comes up with that message.

LogCat

    02-18 12:25:05.426: E/AndroidRuntime(4545): FATAL EXCEPTION: main
02-18 12:25:05.426: E/AndroidRuntime(4545): java.lang.OutOfMemoryError
02-18 12:25:05.426: E/AndroidRuntime(4545):     at android.graphics.BitmapFactory.nativeDecodeStream(Native Method)
02-18 12:25:05.426: E/AndroidRuntime(4545):     at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:493)
02-18 12:25:05.426: E/AndroidRuntime(4545):     at android.graphics.BitmapFactory.decodeFile(BitmapFactory.java:299)
02-18 12:25:05.426: E/AndroidRuntime(4545):     at com.oneplc.viessmannapp.imageslider.adapter.FullScreenImageAdapter.instantiateItem(FullScreenImageAdapter.java:59)
02-18 12:25:05.426: E/AndroidRuntime(4545):     at android.support.v4.view.ViewPager.addNewItem(ViewPager.java:832)
02-18 12:25:05.426: E/AndroidRuntime(4545):     at android.support.v4.view.ViewPager.populate(ViewPager.java:1016)
02-18 12:25:05.426: E/AndroidRuntime(4545):     at android.support.v4.view.ViewPager.populate(ViewPager.java:914)
02-18 12:25:05.426: E/AndroidRuntime(4545):     at android.support.v4.view.ViewPager$3.run(ViewPager.java:244)
02-18 12:25:05.426: E/AndroidRuntime(4545):     at android.support.v4.view.ViewPager.completeScroll(ViewPager.java:1761)
02-18 12:25:05.426: E/AndroidRuntime(4545):     at android.support.v4.view.ViewPager.onInterceptTouchEvent(ViewPager.java:1894)
02-18 12:25:05.426: E/AndroidRuntime(4545):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1629)
02-18 12:25:05.426: E/AndroidRuntime(4545):     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:1957)
02-18 12:25:05.426: E/AndroidRuntime(4545):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1684)
02-18 12:25:05.426: E/AndroidRuntime(4545):     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:1957)
02-18 12:25:05.426: E/AndroidRuntime(4545):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1684)
02-18 12:25:05.426: E/AndroidRuntime(4545):     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:1957)
02-18 12:25:05.426: E/AndroidRuntime(4545):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1684)
02-18 12:25:05.426: E/AndroidRuntime(4545):     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:1957)
02-18 12:25:05.426: E/AndroidRuntime(4545):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1684)
02-18 12:25:05.426: E/AndroidRuntime(4545):     at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1917)
02-18 12:25:05.426: E/AndroidRuntime(4545):     at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1371)
02-18 12:25:05.426: E/AndroidRuntime(4545):     at android.app.Activity.dispatchTouchEvent(Activity.java:2364)
02-18 12:25:05.426: E/AndroidRuntime(4545):     at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1865)
02-18 12:25:05.426: E/AndroidRuntime(4545):     at android.view.View.dispatchPointerEvent(View.java:5721)
02-18 12:25:05.426: E/AndroidRuntime(4545):     at android.view.ViewRootImpl.deliverPointerEvent(ViewRootImpl.java:2890)
02-18 12:25:05.426: E/AndroidRuntime(4545):     at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2466)
02-18 12:25:05.426: E/AndroidRuntime(4545):     at android.view.ViewRootImpl.processInputEvents(ViewRootImpl.java:845)
02-18 12:25:05.426: E/AndroidRuntime(4545):     at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2475)
02-18 12:25:05.426: E/AndroidRuntime(4545):     at android.os.Handler.dispatchMessage(Handler.java:99)
02-18 12:25:05.426: E/AndroidRuntime(4545):     at android.os.Looper.loop(Looper.java:137)
02-18 12:25:05.426: E/AndroidRuntime(4545):     at android.app.ActivityThread.main(ActivityThread.java:4424)
02-18 12:25:05.426: E/AndroidRuntime(4545):     at java.lang.reflect.Method.invokeNative(Native Method)
02-18 12:25:05.426: E/AndroidRuntime(4545):     at java.lang.reflect.Method.invoke(Method.java:511)
02-18 12:25:05.426: E/AndroidRuntime(4545):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
02-18 12:25:05.426: E/AndroidRuntime(4545):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
02-18 12:25:05.426: E/AndroidRuntime(4545):     at dalvik.system.NativeStart.main(Native Method)

THE CLASS RESPONSIBLE / FullScreenImageAdapter.java at line : 59

    public class FullScreenImageAdapter extends PagerAdapter {

    private Activity _activity;
    private ArrayList<String> _imagePaths;
    private LayoutInflater inflater;

    // constructor
    public FullScreenImageAdapter(Activity activity,
            ArrayList<String> imagePaths) {
        this._activity = activity;
        this._imagePaths = imagePaths;
    }

    @Override
    public int getCount() {
        return this._imagePaths.size();
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == ((RelativeLayout) object);
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        TouchImageView imgDisplay;
        Button btnClose;

        inflater = (LayoutInflater) _activity
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View viewLayout = inflater.inflate(R.layout.layout_fullscreen_image, container,
                false);

        imgDisplay = (TouchImageView) viewLayout.findViewById(R.id.imgDisplay);
        btnClose = (Button) viewLayout.findViewById(R.id.btnClose);

        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inPreferredConfig = Bitmap.Config.ARGB_8888;
        Bitmap bitmap = BitmapFactory.decodeFile(_imagePaths.get(position), options);
        imgDisplay.setImageBitmap(bitmap);

        // close button click event
        btnClose.setOnClickListener(new View.OnClickListener() {            
            @Override
            public void onClick(View v) {
                _activity.finish();
            }
        });

        ((ViewPager) container).addView(viewLayout);

        return viewLayout;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        ((ViewPager) container).removeView((RelativeLayout) object);

    }
}

COMMENTS

I saw some other threads but they where not helpfull, or at least not implementable on my case.

Any idea how can I tacle this issue?

Answer

milosmns picture milosmns · Dec 27, 2014

If Google docs didn't help (and they have some neat techniques), as a last resort you can try adding android:largeHeap="true" to the <application> tag in the AndroidManifest.xml. But really, last resort.