1
votes

I'm developing an application which uses many jpeg images. It was always giving outOfMemory error exception. Then I tried decoding it. But still it gives the error. Please help me with this. I can't understand where the error is. Thanks in advance.

public class Kumana extends FragmentActivity implements LocationListener{

    GoogleMap mGoogleMap;
    ArrayList<LatLng> mMarkerPoints;
    double mLatitude = 0;
    double mLongitude = 0;
    Bitmap mCurrentBitmap=null;
    BitmapFactory.Options mBitmapOptions;
    private static final int SWIPE_MIN_DISTANCE = 120;
    private static final int SWIPE_THRESHOLD_VELOCITY = 200;
    private ViewFlipper mViewFlipper,mViewFlipper2,mViewFlipper3;
    private AnimationListener mAnimationListener;
    ImageView iv1, iv2, iv3,iv4,iv5,iv6,iv7,iv8,iv9,iv10,iv11,iv12;
    private Context mContext;
    int mCurrentIndex=0;
    @SuppressWarnings("deprecation")
    private final GestureDetector detector = new GestureDetector(
            new SwipeGestureDetector());

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.kumana);

        final int[] imgIDs={R.drawable.blackneckedstork,R.drawable.eurasianspoonbill,R.drawable.greaterrackettaileddrongo,
                R.drawable.kumana5,R.drawable.lesseradjunt,R.drawable.lesserwhistlingduck,R.drawable.malabartrogan,R.drawable.redfacedmalkoha,
                R.drawable.sirkeermalkoha,R.drawable.thegreatthickknee,R.drawable.yellowfootedgreenpigeon};

        iv1=(ImageView)findViewById(R.id.imgvw_kumana1);
        iv2=(ImageView)findViewById(R.id.imgvw_kumana2);
        iv3=(ImageView)findViewById(R.id.imgvw_kumana3);
        iv4=(ImageView)findViewById(R.id.imgvw_kumana_breeding3);
        iv5=(ImageView)findViewById(R.id.imgvw_kumana_breeding1);
        iv6=(ImageView)findViewById(R.id.imgvw_kumana_breeding2);
        iv7=(ImageView)findViewById(R.id.imgvw_kumana_breeding4);
        iv8=(ImageView)findViewById(R.id.imgvw_kumanaDesc6_RareMigrants1);
        iv9=(ImageView)findViewById(R.id.imgvw_kumanaDesc6_RareMigrants2);
        iv10=(ImageView)findViewById(R.id.imgvw_kumanaDesc6_RareMigrants3);
        iv11=(ImageView)findViewById(R.id.imgvw_kumanaDesc6_RareMigrants4);
        iv12=(ImageView)findViewById(R.id.imgvw_kumanaDesc6_RareMigrants5);

        mBitmapOptions = new BitmapFactory.Options();
        mBitmapOptions.inJustDecodeBounds=true;
        BitmapFactory.decodeResource(getResources(), R.drawable.blackneckedstork);
        mCurrentBitmap=Bitmap.createBitmap(mBitmapOptions.outWidth,mBitmapOptions.outHeight,Bitmap.Config.ARGB_8888);
        mBitmapOptions.inJustDecodeBounds=false;
        mBitmapOptions.inBitmap=mCurrentBitmap;
        mBitmapOptions.inSampleSize=1;
        BitmapFactory.decodeResource(getResources(), R.drawable.blackneckedstork);
        iv1.setImageBitmap(mCurrentBitmap);
        mCurrentIndex=(mCurrentIndex+1)%imgIDs.length;
        BitmapFactory.Options bitmapOptions=null;
        bitmapOptions=mBitmapOptions;
        bitmapOptions.inBitmap=mCurrentBitmap;
        mCurrentBitmap=BitmapFactory.decodeResource(getResources(), imgIDs[mCurrentIndex],bitmapOptions);
        iv1.setImageBitmap(mCurrentBitmap);

        mContext = this;
        mViewFlipper = (ViewFlipper) this.findViewById(R.id.view_flipper_kumana1);
        mViewFlipper2 = (ViewFlipper) this.findViewById(R.id.view_flipper_kumana2);
        mViewFlipper3 = (ViewFlipper) this.findViewById(R.id.view_flipper_kumana3);

        // sets auto flipping
        mViewFlipper.setAutoStart(true);
        mViewFlipper.setFlipInterval(8000);
        mViewFlipper.startFlipping();

        mViewFlipper2.setAutoStart(true);
        mViewFlipper2.setFlipInterval(8000);
        mViewFlipper2.startFlipping();

        mViewFlipper3.setAutoStart(true);
        mViewFlipper3.setFlipInterval(8000);
        mViewFlipper3.startFlipping();

        mViewFlipper.setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(final View view, final MotionEvent event) {
                detector.onTouchEvent(event);
                return true;
            }
        });

        mViewFlipper2.setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(final View view, final MotionEvent event) {
                detector.onTouchEvent(event);
                return true;
            }
        });

        mViewFlipper3.setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(final View view, final MotionEvent event) {
                detector.onTouchEvent(event);
                return true;
            }
        });

        // animation listener
        mAnimationListener = new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
                // animation started event
            }

            @Override
            public void onAnimationRepeat(Animation animation) {
            }

            @Override
            public void onAnimationEnd(Animation animation) {
                // TODO animation stopped event
            }
        };



    class SwipeGestureDetector extends SimpleOnGestureListener {
        @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
                float velocityY) {
            try {
                // right to left swipe
                if (e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE
                        && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
                    mViewFlipper.setInAnimation(AnimationUtils.loadAnimation(mContext, R.anim.left_in));
                    mViewFlipper.setOutAnimation(AnimationUtils.loadAnimation(mContext, R.anim.left_out));
                    // controlling animation
                    mViewFlipper.getInAnimation().setAnimationListener(mAnimationListener);
                    mViewFlipper.showNext();

                    mViewFlipper2.setInAnimation(AnimationUtils.loadAnimation(mContext, R.anim.left_in));
                    mViewFlipper2.setOutAnimation(AnimationUtils.loadAnimation(mContext, R.anim.left_out));
                    // controlling animation
                    mViewFlipper2.getInAnimation().setAnimationListener(mAnimationListener);
                    mViewFlipper2.showNext();

                    mViewFlipper3.setInAnimation(AnimationUtils.loadAnimation(mContext, R.anim.left_in));
                    mViewFlipper3.setOutAnimation(AnimationUtils.loadAnimation(mContext, R.anim.left_out));
                    // controlling animation
                    mViewFlipper3.getInAnimation().setAnimationListener(mAnimationListener);
                    mViewFlipper3.showNext();
                    return true;
                } else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE
                        && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
                    mViewFlipper.setInAnimation(AnimationUtils.loadAnimation(mContext, R.anim.right_in));
                    mViewFlipper.setOutAnimation(AnimationUtils.loadAnimation(mContext, R.anim.right_out));
                    // controlling animation
                    mViewFlipper.getInAnimation().setAnimationListener(mAnimationListener);
                    mViewFlipper.showPrevious();

                    mViewFlipper2.setInAnimation(AnimationUtils.loadAnimation(mContext, R.anim.right_in));
                    mViewFlipper2.setOutAnimation(AnimationUtils.loadAnimation(mContext, R.anim.right_out));
                    // controlling animation
                    mViewFlipper2.getInAnimation().setAnimationListener(mAnimationListener);
                    mViewFlipper2.showPrevious();

                    mViewFlipper3.setInAnimation(AnimationUtils.loadAnimation(mContext, R.anim.right_in));
                    mViewFlipper3.setOutAnimation(AnimationUtils.loadAnimation(mContext, R.anim.right_out));
                    // controlling animation
                    mViewFlipper3.getInAnimation().setAnimationListener(mAnimationListener);
                    mViewFlipper3.showPrevious();
                    return true;
                }

            } catch (Exception e) {
                e.printStackTrace();
            }

            return false;
        }
    }

//Google Map methods

}









<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >

        <RelativeLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:orientation="vertical" >

            <ViewFlipper
                android:id="@+id/view_flipper_kumana1"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent" >

                <RelativeLayout
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent" >

                    <ImageView
                        android:id="@+id/imgvw_kumana1"
                        android:layout_width="fill_parent"
                        android:layout_height="fill_parent"
                        android:layout_gravity="center"
                        android:adjustViewBounds="true"
                        android:contentDescription="@string/imageVw"
                        android:scaleType="centerCrop"
                        android:src="@drawable/kumana8" />

                    <TextView
                        style="@style/ImageTitle"
                        android:text="@string/kumanaLbl" />
                </RelativeLayout>

                <RelativeLayout
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent" >

                    <ImageView
                        android:id="@+id/imgvw_kumana2"
                        android:layout_width="fill_parent"
                        android:layout_height="fill_parent"
                        android:layout_gravity="center"
                        android:adjustViewBounds="true"
                        android:contentDescription="@string/imageVw"
                        android:scaleType="centerCrop"
                        android:src="@drawable/kumana5" />

                    <TextView
                        style="@style/ImageTitle"
                        android:text="@string/kumanaLbl" />
                </RelativeLayout>

                <RelativeLayout
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent" >

                    <ImageView
                        android:id="@+id/imgvw_kumana3"
                        android:layout_width="fill_parent"
                        android:layout_height="fill_parent"
                        android:layout_gravity="center"
                        android:adjustViewBounds="true"
                        android:contentDescription="@string/imageVw"
                        android:scaleType="centerCrop"
                        android:src="@drawable/lesserwhistlingduck" />

                    <TextView
                        style="@style/ImageTitle"
                        android:text="@string/kumanaLbl" />
                </RelativeLayout>
            </ViewFlipper>

            <ImageView
                android:id="@+id/swipe_left"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_centerVertical="true"
                android:contentDescription="@string/imageVw"
                android:src="@drawable/right_arrow" />

            <ImageView
                android:id="@+id/swipe_right"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"
                android:layout_centerVertical="true"
                android:contentDescription="@string/imageVw"
                android:src="@drawable/right_arrow2" />
        </RelativeLayout>


        <RelativeLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:orientation="vertical" >

            <ViewFlipper
                android:id="@+id/view_flipper_kumana2"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent" >

                <RelativeLayout
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent" >

                    <ImageView
                        android:id="@+id/imgvw_kumana_breeding3"
                        android:layout_width="fill_parent"
                        android:layout_height="fill_parent"
                        android:layout_gravity="center"
                        android:adjustViewBounds="true"
                        android:contentDescription="@string/imageVw"
                        android:scaleType="centerCrop"
                        android:src="@drawable/eurasianspoonbill" />

                    <TextView
                        style="@style/ImageTitle"
                        android:text="@string/kumanaDesc4_bird3" />
                </RelativeLayout>

                <RelativeLayout
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent" >

                    <ImageView
                        android:id="@+id/imgvw_kumana_breeding1"
                        android:layout_width="fill_parent"
                        android:layout_height="fill_parent"
                        android:layout_gravity="center"
                        android:adjustViewBounds="true"
                        android:contentDescription="@string/imageVw"
                        android:scaleType="centerCrop"
                        android:src="@drawable/blackneckedstork" />

                    <TextView
                        style="@style/ImageTitle"
                        android:text="@string/kumanaDesc4_bird1" />
                </RelativeLayout>

                <RelativeLayout
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent" >

                    <ImageView
                        android:id="@+id/imgvw_kumana_breeding2"
                        android:layout_width="fill_parent"
                        android:layout_height="fill_parent"
                        android:layout_gravity="center"
                        android:adjustViewBounds="true"
                        android:contentDescription="@string/imageVw"
                        android:scaleType="centerCrop"
                        android:src="@drawable/lesseradjunt" />

                    <TextView
                        style="@style/ImageTitle"
                        android:text="@string/kumanaDesc4_bird2" />
                </RelativeLayout>

                <RelativeLayout
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent" >

                    <ImageView
                        android:id="@+id/imgvw_kumana_breeding4"
                        android:layout_width="fill_parent"
                        android:layout_height="fill_parent"
                        android:layout_gravity="center"
                        android:adjustViewBounds="true"
                        android:contentDescription="@string/imageVw"
                        android:scaleType="centerCrop"
                        android:src="@drawable/thegreatthickknee" />

                    <TextView
                        style="@style/ImageTitle"
                        android:text="@string/kumanaDesc4_bird4" />
                </RelativeLayout>
            </ViewFlipper>

            <ImageView
                android:id="@+id/swipe_left1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_centerVertical="true"
                android:contentDescription="@string/imageVw"
                android:src="@drawable/arrowleft" />

            <ImageView
                android:id="@+id/swipe_right1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"
                android:layout_centerVertical="true"
                android:contentDescription="@string/imageVw"
                android:src="@drawable/arrowright" />
        </RelativeLayout>

        <TextView
            android:id="@+id/kumanaDesc5_header"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="6dp"
            android:paddingTop="10dp"
            android:text="@string/kumanaDesc5_header"
            android:textSize="15sp"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/kirinddescripion3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="6dp"
            android:paddingTop="10dp"
            android:text="@string/kumanaDesc6_RareMigrants"
            android:textSize="15sp"
            android:textStyle="bold" />

        <RelativeLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:orientation="vertical" >

            <ViewFlipper
                android:id="@+id/view_flipper_kumana3"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent" >

                <RelativeLayout
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent" >

                    <ImageView
                        android:id="@+id/imgvw_kumanaDesc6_RareMigrants1"
                        android:layout_width="fill_parent"
                        android:layout_height="fill_parent"
                        android:layout_gravity="center"
                        android:adjustViewBounds="true"
                        android:contentDescription="@string/imageVw"
                        android:scaleType="centerCrop"
                        android:src="@drawable/yellowfootedgreenpigeon" />

                    <TextView
                        style="@style/ImageTitle"
                        android:text="@string/kumanaDesc6_RareMigrants1" />
                </RelativeLayout>

                <RelativeLayout
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent" >

                    <ImageView
                        android:id="@+id/imgvw_kumanaDesc6_RareMigrants2"
                        android:layout_width="fill_parent"
                        android:layout_height="fill_parent"
                        android:layout_gravity="center"
                        android:adjustViewBounds="true"
                        android:contentDescription="@string/imageVw"
                        android:scaleType="centerCrop"
                        android:src="@drawable/greaterrackettaileddrongo" />

                    <TextView
                        style="@style/ImageTitle"
                        android:text="@string/kumanaDesc6_RareMigrants2_graterRacket" />
                </RelativeLayout>

                <RelativeLayout
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent" >

                    <ImageView
                        android:id="@+id/imgvw_kumanaDesc6_RareMigrants3"
                        android:layout_width="fill_parent"
                        android:layout_height="fill_parent"
                        android:layout_gravity="center"
                        android:adjustViewBounds="true"
                        android:contentDescription="@string/imageVw"
                        android:scaleType="centerCrop"
                        android:src="@drawable/malabartrogan" />

                    <TextView
                        style="@style/ImageTitle"
                        android:text="@string/kumanaDesc6_RareMigrants3" />
                </RelativeLayout>

                <RelativeLayout
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent" >

                    <ImageView
                        android:id="@+id/imgvw_kumanaDesc6_RareMigrants4"
                        android:layout_width="fill_parent"
                        android:layout_height="fill_parent"
                        android:layout_gravity="center"
                        android:adjustViewBounds="true"
                        android:contentDescription="@string/imageVw"
                        android:scaleType="centerCrop"
                        android:src="@drawable/redfacedmalkoha" />

                    <TextView
                        style="@style/ImageTitle"
                        android:text="@string/kumanaDesc6_RareMigrants4" />
                </RelativeLayout>

                <RelativeLayout
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent" >

                    <ImageView
                        android:id="@+id/imgvw_kumanaDesc6_RareMigrants5"
                        android:layout_width="fill_parent"
                        android:layout_height="fill_parent"
                        android:layout_gravity="center"
                        android:adjustViewBounds="true"
                        android:contentDescription="@string/imageVw"
                        android:scaleType="centerCrop"
                        android:src="@drawable/sirkeermalkoha" />

                    <TextView
                        style="@style/ImageTitle"
                        android:text="@string/kumanaDesc6_RareMigrants4" />
                </RelativeLayout>
            </ViewFlipper>

            <ImageView
                android:id="@+id/swipe_left1_3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_centerVertical="true"
                android:contentDescription="@string/imageVw"
                android:src="@drawable/arrowleft" />

            <ImageView
                android:id="@+id/swipe_right1_3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"
                android:layout_centerVertical="true"
                android:contentDescription="@string/imageVw"
                android:src="@drawable/arrowright" />
        </RelativeLayout>

        <fragment
            android:id="@+id/map_kumana"
            android:layout_width="match_parent"
            android:layout_height="466dp"
            class="com.google.android.gms.maps.SupportMapFragment" />
    </LinearLayout>

</ScrollView>

threadid=1: thread exiting with uncaught exception (group=0x41df1700) E/AndroidRuntime(25252): FATAL EXCEPTION: main

E/AndroidRuntime(25252): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.isuru.allaboutkataragama/com.isuru.allaboutkataragama.Kumana}: android.view.InflateException: Binary XML file line #165: Error inflating class

E/AndroidRuntime(25252): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2295) E/AndroidRuntime(25252): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2349)

08-21 15:00:18.370: E/AndroidRuntime(25252): Caused by: java.lang.reflect.InvocationTargetException

08-21 15:00:18.370: E/AndroidRuntime(25252): at java.lang.reflect.Constructor.constructNative(Native Method) 08-21 15:00:18.370: E/AndroidRuntime(25252): at java.lang.reflect.Constructor.newInstance(Constructor.java:417) 08-21 15:00:18.370: E/AndroidRuntime(25252): at android.view.LayoutInflater.createView(LayoutInflater.java:600) 08-21 15:00:18.370: E/AndroidRuntime(25252): ... 28 more

08-21 15:00:18.370: E/AndroidRuntime(25252): Caused by: java.lang.OutOfMemoryError

08-21 15:00:18.370: E/AndroidRuntime(25252): at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)

08-21 15:00:18.370: E/AndroidRuntime(25252): at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:596)

08-21 15:00:18.370: E/AndroidRuntime(25252): at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:444)

08-21 15:00:18.370: E/AndroidRuntime(25252): at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:832)

08-21 15:00:18.370: E/AndroidRuntime(25252): at android.content.res.Resources.loadDrawable(Resources.java:2988)

08-21 15:00:18.370: E/AndroidRuntime(25252): at android.content.res.TypedArray.getDrawable(TypedArray.java:602) 08-21 15:00:18.370: E/AndroidRuntime(25252): at android.widget.ImageView. (ImageView.java:131)

08-21 15:00:18.370: E/AndroidRuntime(25252): at android.widget.ImageView.(ImageView.java:121)

5

5 Answers

1
votes

Try using Universal Image Loader or Picasso, these libraries will help you sort out OutOfmemory run time exception, Follow these steps(for using Picasso):

1- Download picasso jar file from here (http://square.github.io/picasso/).

2- Copy jar into your project's lib folder.

3- Now right click on your jar file and add it to build path.

4- Now in your java src file - After your findviewById(R.id.iv12) line, remove all Bitmap related code (eg. remove all these- mCurrentBitmap , BitmapFactory.Options mBitmapOptions , iv1.setImageBitmap(mCurrentBitmap); ) Also remove all src (e.g android:src="@drawable/kumana8") from your xml file.

5- Instead use : Picasso.with(context).load(R.drawable.kumana8).into(imageView1); for setting drawables to your imageview programmatically.

6- Also try adding android:largeHeapSize="true" in your project's android manifest file.

Also you can try universal image loader from here (https://github.com/nostra13/Android-Universal-Image-Loader) or other image loading libraries.

0
votes

You load big bitmap into your app. Try decrease size of bitmap or add

android:largeHeapSize="true"

into your manifest.

0
votes

I think your images size are large. Do you already run with 1 or 2 image? If you can run with 1 or 2 images that error depend on images. You need to reduce image size. Just my thinking. Hope for help. Compress PNG image tool

0
votes
0
votes

First of all you need to decide what dimensions should has image. For my example dimens is 1920x1080: for map markers you can use 100x100 bitmaps.

BitmapFactory.Options o = new BitmapFactory.Options();
                        o.inJustDecodeBounds = true;
                        BitmapFactory.decodeFile(filePath);
                        int srcWidth = o.outWidth;
                        int srcHeight = o.outHeight;
                        int dstWidth = 1920;
                        int dstHeight = 1080;
                        int inSampleSize = 1;
                        if (srcHeight > dstHeight || srcWidth > dstWidth) {
                            final int halfHeight = srcHeight / 2;
                            final int halfWidth = srcWidth / 2;
                            while ((halfHeight / inSampleSize) > dstWidth
                                    && (halfWidth / inSampleSize) > dstWidth) {
                                inSampleSize *= 2;
                            }
                        }
                        o.inSampleSize = inSampleSize;
                        o.inJustDecodeBounds = false;
                        Bitmap bitmap = BitmapFactory.decodeFile(filePath, o);

Also you can use Bitmap.recycle() to free memory.