arash javanmard
arash javanmard

Reputation: 1387

IllegalArgumentException: Cannot draw recycled bitmaps by recreatign activity

i have a simple fragment with an ImageView, the pic token by the user can be set as its background. but as long as there is no pic there. a default PNG will be the background of the ImageView.

there are two XML-Files one for portrait and one for landscape direction.


portrait_fragment.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:orientation="vertical"
          android:layout_width="fill_parent"
          android:layout_height="match_parent">

        <ImageView
            android:layout_width="80dip"
            android:layout_height="80dip"
            android:id="@+id/crime_imageView"
            android:scaleType="centerCrop"
            android:focusableInTouchMode="false"
            android:cropToPadding="true"
            android:adjustViewBounds="true"
            android:src="@drawable/ic_menu_gallery" />

    </LinearLayout>

landscape_fragment.xml is same as portrait one.


Now to my fragment code.

imageview_fragment.java

public class CrimeFragment extends Fragment {

   @Override
   public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      ...
   }

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

   @Override
   public void onStart() {
      super.onStart();
      showPhoto();
   }

   @Override
   public void onStop() {
      super.onStop();
      cleanImageView(mImageView);
   }

   private void showPhoto() {

      // (Re)set the image button's image based on our photo
      Photo photo = mCrime.getPhoto();

      // get the default background of imageview 
      BitmapDrawable bitmapDrawable =
            (BitmapDrawable) getResources().getDrawable(R.drawable.ic_menu_gallery);

      // if we got any user related pic set it as imageviewsbackground
      if (photo != null) {
          String path =         
              getActivity().getFileStreamPath(photo.getFilename()).getAbsolutePath();
          bitmapDrawable = PictureUtils.getScaledDrawable(getActivity(), path);
      }

      mImageView.setImageDrawable(bitmapDrawable);
 }

public void cleanImageView(ImageView imageView) {
    if (!(imageView.getDrawable() instanceof BitmapDrawable))
        return;

    // clean up the view's image for the sake of memory
    BitmapDrawable bitmapDrawable = (BitmapDrawable) imageView.getDrawable();
    bitmapDrawable.getBitmap().recycle();
    imageView.setImageDrawable(null);
 }

Now the Problem ist after changin the driection i get each time this exceprion

IllegalArgumentException: Cannot draw recycled bitmaps

I know its not really necessary to recycle a bitmap as long as there is no reference on it.

However i want to understand whats wrong here. As i understood from different Questions here and the android-documentation after calling recycle and setting a bitmap to null as i do it in "cleanImageView" everything should be ok!!! so after recreating the activity "onStart" will be called and in "showPhoto" a new refernce to the PNG-resource willbe created and passed to the imageview!!!!

What am i doing wrong here???

thx

Upvotes: 0

Views: 142

Answers (1)

arash javanmard
arash javanmard

Reputation: 1387

finally i found a way to solve it..

since

    // get the default background of imageview 
    BitmapDrawable bitmapDrawable =
        (BitmapDrawable) getResources().getDrawable(R.drawable.ic_menu_gallery);

denies to load the same image two times. you have to force it to do that as follow:

BitmapDrawable bitmapDrawable =
            new BitmapDrawable(getResources(), BitmapFactory.decodeResource(getResources(), R.drawable.ic_menu_gallery));

Know the loaded bitmap has each time different address... i know it is not good to load a same image each time. but here i just wanted to understand what leads to illegalexception

Upvotes: 1

Related Questions