0

am new to android..Am having some issues related to recycling of images in Horizontal View. I tried this Link"How to dynamic unload images from gallery?" as refrence.

This is my mainActivity.java:

            ContentResolver cr=getContentResolver();

            phones=cr.query(android.provider.Contacts.People.CONTENT_URI,null, android.provider.Contacts.People._ID + "=?", new String[]{String.valueOf(""+pVal[i])},null);


            Log.d("Curdsr="," "+phones);

            while (phones.moveToNext()) {   

                String name=phones.getString(phones.getColumnIndex(android.provider.Contacts.People.DISPLAY_NAME));
                String phoneNumber=phones.getString(phones.getColumnIndex(android.provider.Contacts.People.NUMBER));
                int PhotoId=phones.getColumnIndex(android.provider.Contacts.People._ID);


                Log.d("PathVal=",""+PhotoId);
                Log.d("P-Name=",name);

                long phId=phones.getLong(PhotoId);

                //String phoneNumber=getPhoneNumber(phId);
                //getPhoto(phId);
                Bitmap photoVal=getPhoto(phId);

                Log.d("PhoyId=",phId+"");


                 // RETRIEVE THE CONTACT PHOTO AS A BITMAP
                Uri uri = ContentUris.withAppendedId(People.CONTENT_URI, phId);
                Bitmap bitmap = People.loadContactPhoto(getApplicationContext(), uri, R.drawable.no_image, null);

                // SET IT HERE IN THE IMAGEVIEW
              //  profile.setImageBitmap(bitmap);

                ContactBean objContact = new ContactBean();
                objContact.setId(PhotoId);
                objContact.setName(name);
                objContact.setpId(phId);
                objContact.setPhoneNo(phoneNumber);


                objContact.setImage(phId);
                objContact.setPicture(bitmap);
                list.add(objContact);

        }
    }

    phones.close();

    objAdapter = new ContanctAdapter(
    this, R.layout.alluser_row, list);
    //listView.setAdapter(objAdapter);

    listview1.setAdapter(objAdapter);

and Here is my Adapter class named as "ContactsAdapter.java"

public class ContanctAdapter extends ArrayAdapter {

private Activity activity;
private List<ContactBean> items;
private int row;
private ContactBean objBean;

public ContanctAdapter(Activity act, int row, List<ContactBean> items) {
    super(act, row, items);
    //super(act, R.layout.alluser_row, items);

    this.activity = act;
    this.row = row;
    this.items = items;

}

@Override
public View getView(final int position, View convertView, ViewGroup parent) {

ImageView Parcel=new ImageView(this.getContext());
     System.out.println("getview:"+position+" "+convertView);

    View view = convertView;
    ViewHolder holder;
    if (convertView == null) {

        Log.d("CustomArrayAdapter", "New");
        //LayoutInflater inflater=activity.getLayoutInflater();
        LayoutInflater inflater = (LayoutInflater) activity
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view = inflater.inflate(row,parent, false);
        Log.d("View Value",view.toString());
        holder = new ViewHolder();

        holder.Imagess=(ImageView) view.findViewById(R.id.imageView1);
        holder.tvname=(TextView) view.findViewById(R.id.tvname);

        //holder.tvname=(TextView)view.findViewById(R.id.tvnames);
        //tvName=(TextView)findViewById(R.id.tvname);

    //  holder.Imagess.setVisibility(View.VISIBLE);

        view.setTag(holder);            


    } else {

        Log.d("pathChange","hi");

        holder = (ViewHolder) view.getTag();

    }

    objBean = items.get(position);      

    holder.tvname.setText(objBean.getName());
    holder.tvname.bringToFront();
    Log.d("Names", objBean.getName());        
    Log.d("thisItem","This Item is Index " + position + " " + view.toString() + " " + Integer.toHexString(System.identityHashCode(view)));
    holder.Imagess.setImageBitmap(objBean.getPicture());

    if((position-4)>=0){

        items.get(position-4).getPicture().recycle();
    }
    else{
        Log.d("before",""+position);
    }


    return view;
}

public class ViewHolder {
    public TextView tvname, tvPhoneNo;
    public ImageView  Imagess;
}

}

When i run this on Emulator,I got the Error log as:

07-12 09:12:44.383: E/AndroidRuntime(5245): java.lang.RuntimeException: Canvas: trying to use a recycled bitmap android.graphics.Bitmap@40e31210 07-12 09:12:44.383: E/AndroidRuntime(5245): at android.graphics.Canvas.throwIfRecycled(Canvas.java:1026) 07-12 09:12:44.383: E/AndroidRuntime(5245): at android.graphics.Canvas.drawBitmap(Canvas.java:1127) 07-12 09:12:44.383: E/AndroidRuntime(5245): at android.graphics.drawable.BitmapDrawable.draw(BitmapDrawable.java:393) 07-12 09:12:44.383: E/AndroidRuntime(5245): at android.widget.ImageView.onDraw(ImageView.java:985) 07-12 09:12:44.383: E/AndroidRuntime(5245): at android.view.View.draw(View.java:13712) 07-12 09:12:44.383: E/AndroidRuntime(5245): at android.view.View.draw(View.java:13596) 07-12 09:12:44.383: E/AndroidRuntime(5245): at android.view.ViewGroup.drawChild(ViewGroup.java:2928) 07-12 09:12:44.383: E/AndroidRuntime(5245): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2797) 07-12 09:12:44.383: E/AndroidRuntime(5245): at android.view.View.draw(View.java:13715) 07-12 09:12:44.383: E/AndroidRuntime(5245): at android.view.View.draw(View.java:13596) 07-12 09:12:44.383: E/AndroidRuntime(5245): at android.view.ViewGroup.drawChild(ViewGroup.java:2928) 07-12 09:12:44.383: E/AndroidRuntime(5245): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2797) 07-12 09:12:44.383: E/AndroidRuntime(5245): at android.view.View.draw(View.java:13594) 07-12 09:12:44.383: E/AndroidRuntime(5245): at android.view.ViewGroup.drawChild(ViewGroup.java:2928) 07-12 09:12:44.383: E/AndroidRuntime(5245): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2797) 07-12 09:12:44.383: E/AndroidRuntime(5245): at android.view.View.draw(View.java:13715) 07-12 09:12:44.383: E/AndroidRuntime(5245): at android.view.View.draw(View.java:13596) 07-12 09:12:44.383: E/AndroidRuntime(5245): at android.view.ViewGroup.drawChild(ViewGroup.java:2928) 07-12 09:12:44.383: E/AndroidRuntime(5245): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2797) 07-12 09:12:44.383: E/AndroidRuntime(5245): at android.view.View.draw(View.java:13715) 07-12 09:12:44.383: E/AndroidRuntime(5245): at android.view.View.draw(View.java:13596) 07-12 09:12:44.383: E/AndroidRuntime(5245): at android.view.ViewGroup.drawChild(ViewGroup.java:2928) 07-12 09:12:44.383: E/AndroidRuntime(5245): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2797) 07-12 09:12:44.383: E/AndroidRuntime(5245): at android.view.View.draw(View.java:13715) 07-12 09:12:44.383: E/AndroidRuntime(5245): at android.view.View.draw(View.java:13596) 07-12 09:12:44.383: E/AndroidRuntime(5245): at android.view.ViewGroup.drawChild(ViewGroup.java:2928) 07-12 09:12:44.383: E/AndroidRuntime(5245): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2797) 07-12 09:12:44.383: E/AndroidRuntime(5245): at android.view.View.draw(View.java:13594) 07-12 09:12:44.383: E/AndroidRuntime(5245): at android.view.ViewGroup.drawChild(ViewGroup.java:2928) 07-12 09:12:44.383: E/AndroidRuntime(5245): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2797) 07-12 09:12:44.383: E/AndroidRuntime(5245): at android.view.View.draw(View.java:13594) 07-12 09:12:44.383: E/AndroidRuntime(5245): at android.view.ViewGroup.drawChild(ViewGroup.java:2928) 07-12 09:12:44.383: E/AndroidRuntime(5245): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2797) 07-12 09:12:44.383: E/AndroidRuntime(5245): at android.view.View.draw(View.java:13715) 07-12 09:12:44.383: E/AndroidRuntime(5245): at android.widget.FrameLayout.draw(FrameLayout.java:467) 07-12 09:12:44.383: E/AndroidRuntime(5245): at com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java:2211) 07-12 09:12:44.383: E/AndroidRuntime(5245): at android.view.ViewRootImpl.drawSoftware(ViewRootImpl.java:2281) 07-12 09:12:44.383: E/AndroidRuntime(5245): at android.view.ViewRootImpl.draw(ViewRootImpl.java:2177) 07-12 09:12:44.383: E/AndroidRuntime(5245): at android.view.ViewRootImpl.performDraw(ViewRootImpl.java:2045) 07-12 09:12:44.383: E/AndroidRuntime(5245): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1854) 07-12 09:12:44.383: E/AndroidRuntime(5245): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:989) 07-12 09:12:44.383: E/AndroidRuntime(5245): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4351) 07-12 09:12:44.383: E/AndroidRuntime(5245): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749) 07-12 09:12:44.383: E/AndroidRuntime(5245): at android.view.Choreographer.doCallbacks(Choreographer.java:562) 07-12 09:12:44.383: E/AndroidRuntime(5245): at android.view.Choreographer.doFrame(Choreographer.java:532) 07-12 09:12:44.383: E/AndroidRuntime(5245): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735) 07-12 09:12:44.383: E/AndroidRuntime(5245): at android.os.Handler.handleCallback(Handler.java:725) 07-12 09:12:44.383: E/AndroidRuntime(5245): at android.os.Handler.dispatchMessage(Handler.java:92) 07-12 09:12:44.383: E/AndroidRuntime(5245): at android.os.Looper.loop(Looper.java:137) 07-12 09:12:44.383: E/AndroidRuntime(5245): at android.app.ActivityThread.main(ActivityThread.java:5041) 07-12 09:12:44.383: E/AndroidRuntime(5245): at java.lang.reflect.Method.invokeNative(Native Method) 07-12 09:12:44.383: E/AndroidRuntime(5245): at java.lang.reflect.Method.invoke(Method.java:511) 07-12 09:12:44.383: E/AndroidRuntime(5245): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) 07-12 09:12:44.383: E/AndroidRuntime(5245): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 07-12 09:12:44.383: E/AndroidRuntime(5245): at dalvik.system.NativeStart.main(Native Method)

Please help me.. Thanks in advance.

Community
  • 1
  • 1
sheik
  • 1
  • 1
  • 2
  • somewhere you must have called bitmap.recycle(); method. This method should be called only when you have no further use of your bitmap. – Rajeev Jul 12 '13 at 07:59
  • Thanks Rajeev.. Will u able to post some code regarding my problem. – sheik Jul 12 '13 at 08:11
  • `if((position-4)>=0){ items.get(position-4).getPicture().recycle(); }` possibly this. What is (position-4) doing, and why you need to recycle that bitmap? – Rajeev Jul 12 '13 at 08:58
  • (position-4) is in the sense,that in my listview, i displayed 4 items in the view and remaining items are get View after get scrolled..The term position means that the current position of the item in View. – sheik Jul 12 '13 at 09:19

1 Answers1

0

I have checked the link now which you had pasted. There in the accepted answer I could find this line as well after recycle

listBitmap(position-3).setBitmap(null);

After recycling the bitmap he is setting the ImageView bitmap as null so that canvas won't try to load it. Since you have not called the above line in your code, canvas will try to load the bitmap which you have already recycled. Please change your code to

if((position-4)>=0){
    items.get(position-4).getPicture().recycle();
    items.get(position-4).<relevant_function_call_to_set_Picture>(null);
} 

Hope this helps.

Rajeev
  • 1,374
  • 1
  • 11
  • 18
  • Thanks Rajeev..I just now included this line in my code as: items.get(position-4).setPicture(null);..But in Emulator "Unfortunatelly get stopped" message ill came . – sheik Jul 12 '13 at 09:14
  • please post the stack traces. – Rajeev Jul 12 '13 at 09:21
  • there can be only one explanation for this...the position for which you are recycling the bitmap is still visible on the screen. So the canvas is trying to display the bitmap but you have made it null. Can't think of any other possibility. How many items are visible on the screen at any given time? – Rajeev Jul 12 '13 at 10:18
  • can you try by removing the "=" from if((position-4)>=0) as my last attempt? – Rajeev Jul 12 '13 at 10:37
  • Thanks Rajeev..Still i got the same as "Unfortunatelly has been stopped"..I dono what mistake is did.. – sheik Jul 12 '13 at 11:08
  • I go through my log cat...What happen is the recycling will not happen...Any help will be appreciated – sheik Jul 12 '13 at 11:37
  • With the current situation, I am afraid it will be difficult to help from my side. You will need to debug my friend and see what really is happening. – Rajeev Jul 12 '13 at 12:02
  • its ok..I will take care of that ..Thanks for your kind co-opeartion Rajeev – sheik Jul 12 '13 at 13:54