避免位图被回收android

我有两个异步任务,其中一个下载图像,另一个添加模糊效果,但我的问题是在下载任务的onPostExecute回调我想将位图放在
ImageView中,然后将其传递给第二个任务应用模糊效果,但有时我得到

trying to use a recycled bitmap  

我已经搜索了很多这个问题,但我没有找到一个明确的解决方案,以避免这个问题特别是我在视图分页器的片段中使用此图像这是我的代码

  protected void onPostExecute(Bitmap result) {
        Bitmap bitmap=result;
        bmImage.setImageBitmap(bitmap);
        if(bitmap!=null && !bitmap.isRecycled()){
            Bitmap bb=bitmap;

        new blurTask().execute(bb);}
    }

这是我完整的片段代码

public class ScreenSlidePageFragment extends Fragment {
    int page;
    String img;
    // ImageView blured;
    String[] checkblur;
    int size;
    Boolean onCreateViewCalled = false;
    ImageView blur;
    ImageView imageView;

        public static ScreenSlidePageFragment newInstance(int page, String img, int size) {
            ScreenSlidePageFragment fragmentFirst = new ScreenSlidePageFragment();
            Bundle args = new Bundle();
            args.putInt("someInt", page);
            args.putString("img", img);
            args.putInt("size", size);
            fragmentFirst.setArguments(args);
            return fragmentFirst;
        }

        // Store instance variables based on arguments passed
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            page = getArguments().getInt("someInt", 0);
            img = getArguments().getString("img");
            size = getArguments().getInt("size");
            checkblur = new String[size];
            onCreateViewCalled = false;
        }

        public void ChangeAlpha(float alpha) {
            System.out.println(alpha);
            blur.setAlpha(alpha);

        }

        @Override
        public void onViewCreated(View view, Bundle savedInstanceState) {
            super.onViewCreated(view, savedInstanceState);

        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            View v = (View) inflater.inflate(
                    R.layout.fragment_screen_slide_page, container, false);

           imageView = (ImageView) v.findViewById(R.id.image);
            blur = (ImageView) v.findViewById(R.id.blur);
            //Glide.with(this).load(img).into(imageView);
            new DownloadImageTask(imageView).execute(img);
        return v;
        }
     class blurTask extends AsyncTask<Bitmap, Integer, BitmapDrawable> {
            Bitmap image;

            @Override
            protected void onPreExecute() {
                super.onPreExecute();
            }

            @Override
            protected BitmapDrawable doInBackground(Bitmap... params) {
                image = params[0];


                Bitmap b = Blur.blurBitmap(image, getActivity());
                final BitmapDrawable ob = new BitmapDrawable(getResources(), b);


                return ob;
            }

            @Override
            protected void onPostExecute(BitmapDrawable blur1) {
                // Do whatever you need with the string, you can update your UI from here
                // blured.setBackground(blur);
                if (blur1 != null)
                    System.out.println("yryryryry");
                blur.setBackground(blur1);
            }


        }
        class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
            ImageView bmImage;

            public DownloadImageTask(ImageView bmImage) {
                this.bmImage = bmImage;
            }

            protected Bitmap doInBackground(String... urls) {
                String urldisplay = urls[0];
                Bitmap mIcon11 = null;
                try {
                    InputStream in = new java.net.URL(urldisplay).openStream();
                    mIcon11 = BitmapFactory.decodeStream(in);
                } catch (Exception e) {
                    Log.e("Error", e.getMessage());
                    e.printStackTrace();
                }
                return mIcon11;
            }

            protected void onPostExecute(Bitmap result) {
                bmImage.setImageBitmap(Bitmap.createBitmap(result));
                new blurTask().execute(Bitmap.createBitmap(bmImage));

            }
        }
    }

所以我得到图像然后出现模糊效果,但是当我尝试滑动到下一个viewpager页面时,我收到此错误
任何帮助?
谢谢.

最佳答案 您可以通过在处理之前将原始位图复制到新位图来避免尝试使用回收的位图.试试这个

protected void onPostExecute(Bitmap result) {
    bmImage.setImageBitmap(result);
    new blurTask().execute(Bitmap.createBitmap(result));
}

希望它对你有用.

点赞