2

I have more than 8 activities in my android application all the screens has lot of imageView which will be rendered with bitmap images

when we open all the screens (activities) it will work for couple of times correctly then it will through out of memory Error and the application crashes out

Here is my code how display Images, kindly suggest how to avoid this/ manage this out of memory issue

XML:

<?xml version="1.0" encoding="utf-8"?>    
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    
    android:layout_width="match_parent"    
    android:layout_height="match_parent"    
    android:orientation="vertical"    
    android:background="@drawable/img_background">    

   <ImageView    
        android:layout_width="match_parent"   
        android:layout_height="match_parent"    
        android:scaleType="fitCenter"    
        android:adjustViewBounds="true"       
        android:id="@+id/artifactImage"      
        android:onClick="displayFullScreenImage"/>      

</LinearLayout>    

Activity:

ImageView artifact_Image = (ImageView) gridView.findViewById(R.id.artifactImage);     
artifact_Image.setImageBitmap(artifactImages[position]);       
artifact_Image.setDrawingCacheEnabled(false);

2 Answers 2

1

Probably the images you display take the whole memory space, that's why you're getting this error. You may want to check out this documentation about how to display bitmaps :

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

In your case, you should load the scaled down versions of the images. Check out this link for sample code:

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

I'm also copying the sample code here for convenience.

Calculate in sample size:

public static int calculateInSampleSize(
            BitmapFactory.Options options, int reqWidth, int reqHeight) {
    // Raw height and width of image
    final int height = options.outHeight;
    final int width = options.outWidth;
    int inSampleSize = 1;

    if (height > reqHeight || width > reqWidth) {

        final int halfHeight = height / 2;
        final int halfWidth = width / 2;

        // Calculate the largest inSampleSize value that is a power of 2 and keeps both
        // height and width larger than the requested height and width.
        while ((halfHeight / inSampleSize) > reqHeight
                && (halfWidth / inSampleSize) > reqWidth) {
            inSampleSize *= 2;
        }
    }

    return inSampleSize;
}

Decode bitmap:

public static Bitmap decodeSampledBitmapFromResource(Resources res, int resId,
        int reqWidth, int reqHeight) {

    // First decode with inJustDecodeBounds=true to check dimensions
    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeResource(res, resId, options);

    // Calculate inSampleSize
    options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);

    // Decode bitmap with inSampleSize set
    options.inJustDecodeBounds = false;
    return BitmapFactory.decodeResource(res, resId, options);
}

And finally set the scaled bitmap

mImageView.setImageBitmap(
    decodeSampledBitmapFromResource(getResources(), R.id.myimage, 100, 100));
Sign up to request clarification or add additional context in comments.

1 Comment

Do note that there is a bug in Google's calculateInSampleSize function above, for the case where inSampleSize should equal 2 it will return 1 in error.
0

see here are a number of reasons why loading bitmaps in your Android application is tricky:

  1. Mobile devices typically have constrained system resources. Android devices can have as little as 16MB of memory available to a single application. The Android Compatibility Definition Document (CDD), Section 3.7. Virtual Machine Compatibility gives the required minimum application memory for various screen sizes and densities. Applications should be optimized to perform under this minimum memory limit. However, keep in mind many devices are configured with higher limits.

  2. Bitmaps take up a lot of memory, especially for rich images like photographs. For example, the camera on the Galaxy Nexus takes photos up to 2592x1936 pixels (5 megapixels). If the bitmap configuration used is ARGB_8888 (the default from the Android 2.3 onward) then loading this image into memory takes about 19MB of memory (2592*1936*4 bytes), immediately exhausting the per-app limit on some devices.

  3. Android app UI’s frequently require several bitmaps to be loaded at once. Components such as ListView, GridView and ViewPager commonly include multiple bitmaps on-screen at once with many more potentially off-screen ready to show at the flick of a finger.

here is a nice blog as well to avoid memory leaks.

you should use image Loader library if you want to load multiple bitmap without memory error

Comments

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.