2014-03-28 62 views
-1

我已經從API緩存圖像和上課的時候我在AsyncTask解碼位圖,還給我OutOfMemoryErrordecodeStream內存溢出(LruChace)

 
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

+0

檢查http://www.youtube.com/watch?v=pMRnGDR6Cu0對於您可以使用圖像緩存http://loopj.com/android-smart-image-view/ – playmaker420

回答

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

}