user1871951
user1871951

Reputation: 105

java.lang.IllegalStateException: Can't parcel a recycled bitmap error occurs

I am trying to use the following code to get the bitmap of image,which is taken from camera. I used this concept because I am adding image as a watermark to the picture taken.So I just make the Activity to draw as a bitmap.

While I am doing this I am getting the following error ( Can't parcel a recycled bitmap error occurs)

I want to send this bitmap to another Activity.

How can I check whether I am getting the image or not?

else if(v.equals(findViewById(R.id.ok_button))){


                topbar.setVisibility(View.GONE);
                menubar.setVisibility(View.GONE);
                bottom.setVisibility(View.GONE);

                View s = ml.getRootView();
                s.setDrawingCacheEnabled(true);

                b = s.getDrawingCache();
                Log.e("ok","ok");
                Intent i=new Intent(CameraActivity.this,Update.class);
                 i.putExtra("data",b);
                 startActivity(i);
      //           s.setDrawingCacheEnabled(false);
        //         s.setVisibility(View.GONE);
                 finish();

Thanks

Upvotes: 2

Views: 6363

Answers (1)

lucian.pantelimon
lucian.pantelimon

Reputation: 3669

A "little" late, but I've come across an issue similar to this one recently and someone else with the same issue might find your question, so it's best that there's an answer to it.

First, make sure that you get the desired image when calling the getDrawingCache method. Check out this post for a possible cause, if you are not getting an image.

Second, make sure that the image does not get recycled. This might not be the case, as an IllegalStateException, saying that you can't parcel a recycled bitmap, would be thrown and your application would crash. If your image does get recycled, just make a copy of it and put the copy in the Bundle. As a side note, calling setDrawingCacheEnabled(false) will call recycle().

Third (and last), make sure that the image gets through to the other activity. Mistakes such as deserializing the wrong fields or to the wrong variable are very common.

If all the above go well and the cause of your this happening is in the above code, you should not have just a black screen displayed. If you still get a black screen, then it's most likely that the bug is in the Update activity and not in the one that's sending the image.

EDIT: Misread the question.

You might be getting the exception because the image might (can't confirm this) be serialized after the intent is fired. If this is the case, just put a copy of the Bitmap on the Intent, instead of the Bitmap itself like this:

i.putExtra("data",b.copy(b.getConfig(), b);

This question helped me realize that.

Upvotes: 5

Related Questions