致命异常:Android中的doInBackground()方法中的异步任务java.lang.OutOfMemoryError?

时间:2016-09-27 09:59:21

标签: java android bitmap android-asynctask ui-thread

我有一个线性布局,我将以编程方式添加Image View。我正在使用imageView.setImageBitmap(bitmap);设置图像。我得到了内存不足错误。通过https://developer.android.com/training/displaying-bitmaps/process-bitmap.html阅读后,我决定在异步任务中设置图像位图,但我仍然得到相同的错误。此错误有时不一致我有时候得到它我不会。这是我的代码异步任务类。

class SetImageInAsync extends AsyncTask<String, String, ImageWrapper>{
    ImageView imageView;
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        imageView = new ImageView(DisplayContent.this);
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
        layoutParams.setMargins(16,16,16,16);
        imageView.setLayoutParams(layoutParams);
        imageView.setScaleType(ImageView.ScaleType.FIT_XY);
        web_linearLayout.addView(imageView);

    }

    @Override
    protected ImageWrapper doInBackground(String... params)
    {
        Uri image_uri = Uri.fromFile(new File("/sdcard/rreadyreckoner_images/" + params[0]));
        Bitmap  mBitmap = null;
        try {
            mBitmap = MediaStore.Images.Media.getBitmap(DisplayContent.this.getContentResolver(), image_uri);

        } catch (IOException e) {
            e.printStackTrace();
        }
        ImageWrapper w = new ImageWrapper();
        w.bitmap = mBitmap;
        w.filename = params[0];
        return w;
    }

    @Override
    protected void onPostExecute(final ImageWrapper imgWR) {

        Bundle extras = getIntent().getExtras();
        final String subcategory = extras.getString("subcategory");
        imageView.setAdjustViewBounds(true);
        imageView.setScaleType(TouchImageView.ScaleType.FIT_CENTER);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
            imageView.setCropToPadding(false);
        }
        imageView.setImageBitmap(imgWR.bitmap);
        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view)
            {
                Intent intent = new Intent(DisplayContent.this,DisplayImage.class);
                intent.putExtra("filename",imgWR.filename);
                intent.putExtra("subcategory",subcategory);
                startActivity(intent);


            }
        });


    }
}

这是我得到的错误日志: -

09-27 15:10:07.587 10366-10898/com.inevitable.org.tab_sample E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #1
Process: com.inevitable.org.tab_sample, PID: 10366
java.lang.RuntimeException: An error occured while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:300)
at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
at java.util.concurrent.FutureTask.run(FutureTask.java:242)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:841)
Caused by: java.lang.OutOfMemoryError
at android.graphics.BitmapFactory.nativeDecodeStream(Native Method)
at android.graphics.BitmapFactory.decodeStreamInternal(BitmapFactory.java:736)
at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:712)
at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:750)
at android.provider.MediaStore$Images$Media.getBitmap(MediaStore.java:830)
at com.inevitable.org.tab_sample.DisplayContent$SetImageInAsync.doInBackground(DisplayContent.java:236)
at com.inevitable.org.tab_sample.DisplayContent$SetImageInAsync.doInBackground(DisplayContent.java:216)
at android.os.AsyncTask$2.call(AsyncTask.java:288)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231) 
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112) 
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587) 
at java.lang.Thread.run(Thread.java:841) 

我是编程新手,所以感谢任何帮助或建议。谢谢。

2 个答案:

答案 0 :(得分:3)

位图可能太大而导致OOM。这不是一个线程问题。

你应该读到这个,这是一个很好的起点。它对许多Android开发人员都很有帮助,包括我自己。

Loading Large Bitmaps Efficiently

答案 1 :(得分:1)

我使用了@brahmyadigopula建议的示例http://tutorials-android.blogspot.in/2011/11/outofmemory-exception-when-decoding.html中的示例代码。

// Read bitmap
    public Bitmap readBitmap(Uri selectedImage) {
        Bitmap bm = null;

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



        AssetFileDescriptor fileDescriptor =null;
        try {
            fileDescriptor = this.getContentResolver().openAssetFileDescriptor(selectedImage,"r");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        finally{
            try {
                bm = BitmapFactory.decodeFileDescriptor(fileDescriptor.getFileDescriptor(), null, options);

                // Calculate inSampleSize
                options.inSampleSize = calculateInSampleSize(options,200, 200);

                // Decode bitmap with inSampleSize set
                options.inJustDecodeBounds = false;
                bm = BitmapFactory.decodeFileDescriptor(fileDescriptor.getFileDescriptor(), null, options);


                fileDescriptor.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return bm;
    }

然后根据@Lev建议的https://developer.android.com/training/displaying-bitmaps/load-bitmap.html使用以下代码计算样本数量。

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

使用如下: -

 Uri image_uri = Uri.fromFile(new File("/sdcard/rreadyreckoner_images/" + params[0]));

            Bitmap mBitmap = readBitmap(image_uri);

感谢@Lev和@brahmyadigopula的指导。保持良好的工作。