0

I always get the OutOfMemory exception.

I know the issue is the Bitmap capacity I downloaded from server to my application already exceeded the cache memory.

But I don't know how much the Maximum Cache Memory of my application I can stored (images)is?

People who know,

Please tell me,

Thanks,

Huy Tower
  • 7,769
  • 16
  • 61
  • 86

1 Answers1

1

from doc:

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. 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.

and although per app limit is different from device to device but I think it is around 16-25 MB.

what should you do?

first you must store your images on the disk, then use function below to decode it and then assign it to what imageview you like:

I have changed the function from the doc to read images from file not from resource folder.

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;
}

public static Bitmap decodeSampledBitmapFromFile(String filePath,int reqWidth, int reqHeight) {

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

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

    // Decode bitmap with inSampleSize set
    options.inJustDecodeBounds = false;
    return BitmapFactory.decodeFile(filePath, options);
}

just call decodeSampledBitmapFromFile(String filePath,int reqWidth, int reqHeight).

Reference:

Displaying Bitmaps Efficiently

another option is using image downloader library, for comparison look at:

Local image caching solution for Android: Square Picasso vs Universal Image Loader

they just take care of most of your problems.

Community
  • 1
  • 1
mmlooloo
  • 18,937
  • 5
  • 45
  • 64