decodeStream outOfMemory(LruChace)

时间:2014-03-28 09:23:37

标签: android out-of-memory android-lru-cache

我有一个从API缓存图像的类,当我在AsyncTask中解码位图时,它会返回OutOfMemoryError

03-28 09:15:07.455: E/AndroidRuntime(721): java.lang.RuntimeException: An error occured while executing doInBackground()
03-28 09:15:07.455: E/AndroidRuntime(721):  at android.os.AsyncTask$3.done(AsyncTask.java:278)
03-28 09:15:07.455: E/AndroidRuntime(721):  at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
03-28 09:15:07.455: E/AndroidRuntime(721):  at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
03-28 09:15:07.455: E/AndroidRuntime(721):  at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
03-28 09:15:07.455: E/AndroidRuntime(721):  at java.util.concurrent.FutureTask.run(FutureTask.java:137)
03-28 09:15:07.455: E/AndroidRuntime(721):  at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:208)
03-28 09:15:07.455: E/AndroidRuntime(721):  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
03-28 09:15:07.455: E/AndroidRuntime(721):  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
03-28 09:15:07.455: E/AndroidRuntime(721):  at java.lang.Thread.run(Thread.java:856)
03-28 09:15:07.455: E/AndroidRuntime(721): Caused by: java.lang.OutOfMemoryError
03-28 09:15:07.455: E/AndroidRuntime(721):  at android.graphics.BitmapFactory.nativeDecodeStream(Native Method)
03-28 09:15:07.455: E/AndroidRuntime(721):  at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:493)
03-28 09:15:07.455: E/AndroidRuntime(721):  at com.android.lifter.cache.ImageCache.loadRemoteBitmap(ImageCache.java:371)
03-28 09:15:07.455: E/AndroidRuntime(721):  at com.android.lifter.cache.ImageCache.access$7(ImageCache.java:331)
03-28 09:15:07.455: E/AndroidRuntime(721):  at com.android.lifter.cache.ImageCache$BitmapDownload.doInBackground(ImageCache.java:540)
03-28 09:15:07.455: E/AndroidRuntime(721):  at com.android.lifter.cache.ImageCache$BitmapDownload.doInBackground(ImageCache.java:1)
03-28 09:15:07.455: E/AndroidRuntime(721):  at android.os.AsyncTask$2.call(AsyncTask.java:264)
03-28 09:15:07.455: E/AndroidRuntime(721):  at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)

代码:http://pastebin.com/GpyLULye

1 个答案:

答案 0 :(得分:0)

请按此代码压缩您的位图并获取压缩位图对象。

package com.yuor.package.utils;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

import java.io.InputStream;


public class CompressBitmap {

public static Bitmap decodeBitmapResource(Resources res, int resId,
        int reqWidth, int reqHeight, boolean aspectRatio) {

    // 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 = calculateSampleSize(options, reqWidth, reqHeight);

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


public static Bitmap decodeBitmapFile(String filepath, 
        int reqWidth, int reqHeight, boolean aspectRatio) {

    // 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 = calculateSampleSize(options, reqWidth, reqHeight);

    // Decode bitmap with inSampleSize set
    options.inJustDecodeBounds = false;
    return Bitmap.createScaledBitmap(
            BitmapFactory.decodeFile(filepath, options), reqWidth,
            reqHeight, true);
}


public static Bitmap decodeSampledBitmapFromStream(InputStream is, 
        int reqWidth, int reqHeight) {

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


    BitmapFactory.decodeStream(is, null, options); 

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

    // Decode bitmap with inSampleSize set
    options.inJustDecodeBounds = false;
    return BitmapFactory.decodeStream(is, null, options);
}

public static int calculateSampleSize(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) {

        // Calculate ratios of height and width to requested height and
        // width
        final int heightRatio = Math.round((float) height
                / (float) reqHeight);
        final int widthRatio = Math.round((float) width / (float) reqWidth);

        // Choose the smallest ratio as inSampleSize value, this will
        // guarantee
        // a final image with both dimensions larger than or equal to the
        // requested height and width.
        inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
    }

    return inSampleSize;
}

}