致命异常asynctask#2 doInBackground

时间:2013-04-07 19:59:12

标签: android-asynctask

此代码崩溃。为什么呢?

@Override
    protected String doInBackground(String... params) {

        try {
            image_compress = image_compress(changed_image);

            array_image.add(Effects_change.doColorFilter((image_compress), 0,
                    1, 0));
            array_image.add(Effects_change.doColorFilter((image_compress), 0,
                    0, 1));


            array_image.add(Effects_change.doColorFilter((image_compress), 0,
                    1.2, 1.8));

            array_image.add(Effects_change.doGreyscale(image_compress));
            array_image.add(Effects_change.doGamma((image_compress), 0.8, 0.8,
                    0.8));
            array_image.add(Effects_change.doGamma((image_compress), 1.8, 1.8,
                    1.8));
            array_image.add(Effects_change.doColorFilter((image_compress), 1.0,
                    0, 0));

            array_image.add(Effects_change.doColorFilter((image_compress), 0.5,
                    0.5, 0.5));
            array_image.add(Effects_change.doColorFilter((image_compress), 1.5,
                    1.5, 1.5));


            array_image.add(Effects_change.createSepiaToningEffect(
                    (image_compress), 100, 1.5, 0.6, 0.12));
            array_image.add(Effects_change.createSepiaToningEffect(
                    (image_compress), 100, 0.88, 1.45, 1.43));

            array_image.add(Effects_change.createSepiaToningEffect(
                    (image_compress), 100, 1.2, 0.87, 1.8));
            array_image.add(Effects_change.decreaseColorDepth((image_compress),
                    32));

            array_image.add(Effects_change.decreaseColorDepth((image_compress),
                    120));

            array_image.add(Effects_change.applyReflection(image_compress));
            //array_image.add(Effects_change.applynewReflection(image_compress));
            array_image.add(Effects_change.applyFleaEffect(image_compress));

            array_image.add(Effects_change.sharpen((image_compress), 15));



            array_image.add(Effects_change.createSepiaToningEffect(
                    (image_compress), 200, 0.50, 0.6, 0.20));
            array_image.add(Effects_change.createSepiaToningEffect(
                    (image_compress), 150, 1.2, 0.97, 2.1));

            array_image.add(Effects_change.applyGaussianBlur(image_compress));
            array_image.add(Effects_change.applyBlackFilter(image_compress));
            array_image.add(Effects_change.applyMeanRemoval(image_compress));

            array_image.add(Effects_change.applySnowEffect(image_compress));

            array_image.add(Effects_change.smooth((image_compress), 8));
            array_image.add(Effects_change.CharcoalEffect(image_compress));

            array_image.add(Effects_change.tintImage((image_compress), 80));
            array_image.add(Effects_change.roundCorner((image_compress), 45));

            array_image.add(Effects_change.boost((image_compress), 1,
                    (float) 1.5));
            array_image.add(Effects_change.boost((image_compress), 2,
                    (float) 0.30));
            array_image.add(Effects_change.boost((image_compress), 3,
                    (float) 0.67));
            array_image.add(Effects_change.emboss(image_compress));

            array_image.add(Effects_change.boost((image_compress), 1,
                    (float) .50));

            array_image.add(Effects_change.boost((image_compress), 2,
                    (float) 0.10));
            array_image.add(Effects_change.boost((image_compress), 3,
                    (float) 1.5));
            array_image.add(Effects_change.engrave(image_compress));
            array_image.add(Effects_change.applyHueFilter((image_compress), 3));

            array_image.add(Effects_change
                    .changeToNegativeEffect(image_compress));
            array_image.add(Effects_change.SketchImage(image_compress));

            array_image.add(Effects_change.doColorFilter(image_compress, 1.8, 1.2, 1));


            array_image.add(Effects_change.tintImage((image_compress), 180));

            array_image.add(Effects_change.applySaturationFilter(image_compress, 2));

            array_image.add(Effects_change.createContrast(image_compress, 100));

            array_image.add(Effects_change.applySaturationFilter((image_compress), 90));
            // array_image.add(Effects_change.posterize(image_compress, 10));

            // setProgress(""+(int)((total*100)/lenghtOfFile));

        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        //array_image.add(Effects_change.setSepiaColorFilter(image_compress));

        return null;
    }

    @Override
    public void onPostExecute(String result) {
        super.onPostExecute(result);

        g.setAdapter(new ImageAdapter(Image_add_effects.this));
        g.setOnItemSelectedListener(Image_add_effects.this);

        try {
            pb.dismiss();
            pb=null;
        } catch (Exception e) {
            // TODO: handle exception
        }
    }

}

这是日志

04-07 22:36:08.450: W/dalvikvm(18429): threadid=13: thread exiting with uncaught exception(group=0x41fe82a0)
04-07 22:36:08.450: E/AndroidRuntime(18429): FATAL EXCEPTION: AsyncTask #2
04-07 22:36:08.450: E/AndroidRuntime(18429): java.lang.RuntimeException: An error occured while executing doInBackground()
04-07 22:36:08.450: E/AndroidRuntime(18429):    at android.os.AsyncTask$3.done(AsyncTask.java:299)
04-07 22:36:08.450: E/AndroidRuntime(18429):    at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
04-07 22:36:08.450: E/AndroidRuntime(18429):    at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
04-07 22:36:08.450: E/AndroidRuntime(18429):    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
04-07 22:36:08.450: E/AndroidRuntime(18429):    at java.util.concurrent.FutureTask.run(FutureTask.java:137)
04-07 22:36:08.450: E/AndroidRuntime(18429):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
04-07 22:36:08.450: E/AndroidRuntime(18429):    at java.lang.Thread.run(Thread.java:856)
04-07 22:36:08.450: E/AndroidRuntime(18429): Caused by: java.lang.OutOfMemoryError

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

原因是java.lang.OutOfMemoryError意味着您的计算会占用太多RAM。

但是,如果不知道你做了什么,很难说究竟是什么导致了错误。可能你创建了太多的图像/位图。

以下是有关您的应用可获得多少内存的信息:https://stackoverflow.com/a/9940415/247013

调查的想法:

  • 使用Bitmap.recycle() - 只要您不需要已处理的位图,就调用它
  • 尽量不将图像存储在RAM中,而是将它们保存到文件中
  • 看起来您正在尝试提前为任何可能的效果预先创建图像 - 考虑重新架构 - 只有在用户明确要求(按需)时才会产生一种效果。