Antwan
Antwan

Reputation: 3876

Avoid bitmap from being recycled android

i have two async task one of them download the image and the other one add blur effect on it but my problem is that in the onPostExecute callback of the download Task i want to put the bitmap in an ImageView and then pass it to second Task to apply blur effect on it but sometimes i get

trying to use a recycled bitmap  

i have searched alot on this problem but i didn't find a clear solution to avoid this problem specially that i use this image in a fragment of view pager this is my code

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

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

and this my complete fragment code

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

            }
        }
    }

so i get the image then the blur effect appear but when i try to swipe to the next viewpager page i get this error
any help?
Thanks.

Upvotes: 4

Views: 1872

Answers (1)

Arkar Aung
Arkar Aung

Reputation: 3584

You can avoid trying to use a recycled bitmap by copying original bitmap to new bitmap before processing. Try this

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

Hope it will be useful for you.

Upvotes: 3

Related Questions