【问题标题】:ERROR AsyncTask #1 while executing doInBackground And OutOfMemoryError执行 doInBackground 和 OutOfMemoryError 时出现错误 AsyncTask #1
【发布时间】:2017-02-23 05:24:38
【问题描述】:

我在加载多张图片时出错, 图像成功加载,但无法打开两次。 如果我关闭活动并重新打开活动,我会收到错误

这是错误:

E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #1
              java.lang.RuntimeException: An error occured while executing doInBackground()
                  at android.os.AsyncTask$3.done(AsyncTask.java:299)
                  at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
                  at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
                  at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
                  at java.util.concurrent.FutureTask.run(FutureTask.java:137)
                  at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
                  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
                  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
                  at java.lang.Thread.run(Thread.java:856)
               Caused by: java.lang.OutOfMemoryError
                  at android.graphics.BitmapFactory.nativeDecodeStream(Native Method)
                  at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:527)
                  at com.metroapp.lazyloader.ImageLoader.decodeFile(ImageLoader.java:181)
                  at com.metroapp.lazyloader.ImageLoader.getBitmap(ImageLoader.java:78)
                  at com.metroapp.activity.CatalogsDetailActivity$GetImageAsyncTask.doInBackground(CatalogsDetailActivity.java:440)
                  at com.metroapp.activity.CatalogsDetailActivity$GetImageAsyncTask.doInBackground(CatalogsDetailActivity.java:429)
                  at android.os.AsyncTask$2.call(AsyncTask.java:287)
                  at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
                  at java.util.concurrent.FutureTask.run(FutureTask.java:137) 
                  at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230) 
                  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076) 
                  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569) 
                  at java.lang.Thread.run(Thread.java:856) 

这是我的代码:

public class GetImageAsyncTask extends AsyncTask<Void, Void, String> {
    String bd_image;

    public GetImageAsyncTask(String bd_image) {
        this.bd_image=bd_image;
        load = new ImageLoader(ctx);
    }

    @Override
    protected String doInBackground(Void... params) {
        try {
            image = load.getBitmap(bd_image);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    protected void onPostExecute(String s) {
        super.onPostExecute(s);
        arr.add(image);
        imageArray.remove(0);

        try {
            if (imageArray.size() > 0) {
                new GetImageAsyncTask(imageArray.get(0)).execute();
            }else{
                loadData();
            }
        }catch (Exception e){
            e.printStackTrace();
            loadData();
        }
    }
}

private Bitmap decodeFile(File f) {
    Bitmap bitmap = null;
    FileInputStream stream1= null;
    FileInputStream stream2= null;

    try {
        BitmapFactory.Options o = new BitmapFactory.Options();
        o.inJustDecodeBounds = true;
        stream1 = new FileInputStream(f);
        BitmapFactory.decodeStream(stream1, null, o);
        stream1.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    BitmapFactory.Options o2 = new BitmapFactory.Options();

    for (o2.inSampleSize = 1; o2.inSampleSize <= 32; o2.inSampleSize++) {
        try {
            stream2 = new FileInputStream(f);
            bitmap = BitmapFactory.decodeStream(stream2, null, o2);
            stream2.close();

            Log.d("DECODE FILE : ", "Decoded successfully for sampleSize " + o2.inSampleSize);
            break;
        } catch (OutOfMemoryError outOfMemoryError) {
            // If an OutOfMemoryError occurred, we continue with for loop and next inSampleSize value
            Log.e("DECODE FILE : ", "outOfMemoryError while reading file for sampleSize " + o2.inSampleSize + " retrying with higher value");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return bitmap;
}

有什么办法吗?

【问题讨论】:

标签: android android-asynctask out-of-memory imageloader


【解决方案1】:

不要尝试使用android:largeHeap="true" 增加安卓应用程序的堆大小,因为不推荐这样做。

我个人建议您使用GlidePicasso 库来加载图像。因为它不仅可以处理内部缓存问题,而且足够稳定,能够支持所有安卓版本。

【讨论】:

    【解决方案2】:

    finally 块及其 Java 指南中关闭您的 stream1stream2。此外,在 Android 中,请确保在不需要时回收所有 Bitmap 实例。

    【讨论】:

      【解决方案3】:
      private Bitmap loadBitmapEfficiently() {
      
      // Get the dimensions of the View
      int targetW = mImageView.getWidth();
      int targetH = mImageView.getHeight();
      
      // Get the dimensions of the bitmap
      BitmapFactory.Options bmOptions = new BitmapFactory.Options();
      bmOptions.inJustDecodeBounds = true;
      BitmapFactory.decodeFile(mImagePath, bmOptions); // you can get imagePath from file
      int photoW = bmOptions.outWidth;
      int photoH = bmOptions.outHeight;
      
      // Determine how much to scale down the image
      int scaleFactor = Math.min(photoW/targetW, photoH/targetH);
      
      // Decode the image file into a Bitmap sized to fill the View
      bmOptions.inJustDecodeBounds = false;
      bmOptions.inSampleSize = scaleFactor;
      bmOptions.inPurgeable = true;
      
      return BitmapFactory.decodeFile(mImagePath, bmOptions);
      
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-12-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-01-11
        • 1970-01-01
        • 2013-06-01
        相关资源
        最近更新 更多