在Android中的Horizo​​ntalView中回收ImageView

时间:2013-07-12 07:38:47

标签: android recycle

我是android的新手。有一些与水平视图中的图像回收相关的问题。我试过这个链接" How to dynamic unload images from gallery?"作为参考。

这是我的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);

以下是我的Adapter类名为" ContactsAdapter.java"

公共类ContanctAdapter扩展了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;
}

}

当我在Emulator上运行时,我得到了错误日志:

07-12 09:12:44.383:E / AndroidRuntime(5245):java.lang.RuntimeException:Canvas:尝试使用回收的位图android.graphics.Bitmap@40e31210 07-12 09:12:44.383:E / AndroidRuntime(5245):在android.graphics.Canvas.throwIfRecycled(Canvas.java:1026) 07-12 09:12:44.383:E / AndroidRuntime(5245):在android.graphics.Canvas.drawBitmap(Canvas.java:1127) 07-12 09:12:44.383:E / AndroidRuntime(5245):在android.graphics.drawable.BitmapDrawable.draw(BitmapDrawable.java:393) 07-12 09:12:44.383:E / AndroidRuntime(5245):在android.widget.ImageView.onDraw(ImageView.java:985) 07-12 09:12:44.383:E / AndroidRuntime(5245):在android.view.View.draw(View.java:13712) 07-12 09:12:44.383:E / AndroidRuntime(5245):在android.view.View.draw(View.java:13596) 07-12 09:12:44.383:E / AndroidRuntime(5245):在android.view.ViewGroup.drawChild(ViewGroup.java:2928) 07-12 09:12:44.383:E / AndroidRuntime(5245):在android.view.ViewGroup.dispatchDraw(ViewGroup.java:2797) 07-12 09:12:44.383:E / AndroidRuntime(5245):在android.view.View.draw(View.java:13715) 07-12 09:12:44.383:E / AndroidRuntime(5245):在android.view.View.draw(View.java:13596) 07-12 09:12:44.383:E / AndroidRuntime(5245):在android.view.ViewGroup.drawChild(ViewGroup.java:2928) 07-12 09:12:44.383:E / AndroidRuntime(5245):在android.view.ViewGroup.dispatchDraw(ViewGroup.java:2797) 07-12 09:12:44.383:E / AndroidRuntime(5245):在android.view.View.draw(View.java:13594) 07-12 09:12:44.383:E / AndroidRuntime(5245):在android.view.ViewGroup.drawChild(ViewGroup.java:2928) 07-12 09:12:44.383:E / AndroidRuntime(5245):在android.view.ViewGroup.dispatchDraw(ViewGroup.java:2797) 07-12 09:12:44.383:E / AndroidRuntime(5245):在android.view.View.draw(View.java:13715) 07-12 09:12:44.383:E / AndroidRuntime(5245):在android.view.View.draw(View.java:13596) 07-12 09:12:44.383:E / AndroidRuntime(5245):在android.view.ViewGroup.drawChild(ViewGroup.java:2928) 07-12 09:12:44.383:E / AndroidRuntime(5245):在android.view.ViewGroup.dispatchDraw(ViewGroup.java:2797) 07-12 09:12:44.383:E / AndroidRuntime(5245):在android.view.View.draw(View.java:13715) 07-12 09:12:44.383:E / AndroidRuntime(5245):在android.view.View.draw(View.java:13596) 07-12 09:12:44.383:E / AndroidRuntime(5245):在android.view.ViewGroup.drawChild(ViewGroup.java:2928) 07-12 09:12:44.383:E / AndroidRuntime(5245):在android.view.ViewGroup.dispatchDraw(ViewGroup.java:2797) 07-12 09:12:44.383:E / AndroidRuntime(5245):在android.view.View.draw(View.java:13715) 07-12 09:12:44.383:E / AndroidRuntime(5245):在android.view.View.draw(View.java:13596) 07-12 09:12:44.383:E / AndroidRuntime(5245):在android.view.ViewGroup.drawChild(ViewGroup.java:2928) 07-12 09:12:44.383:E / AndroidRuntime(5245):在android.view.ViewGroup.dispatchDraw(ViewGroup.java:2797) 07-12 09:12:44.383:E / AndroidRuntime(5245):在android.view.View.draw(View.java:13594) 07-12 09:12:44.383:E / AndroidRuntime(5245):在android.view.ViewGroup.drawChild(ViewGroup.java:2928) 07-12 09:12:44.383:E / AndroidRuntime(5245):在android.view.ViewGroup.dispatchDraw(ViewGroup.java:2797) 07-12 09:12:44.383:E / AndroidRuntime(5245):在android.view.View.draw(View.java:13594) 07-12 09:12:44.383:E / AndroidRuntime(5245):在android.view.ViewGroup.drawChild(ViewGroup.java:2928) 07-12 09:12:44.383:E / AndroidRuntime(5245):在android.view.ViewGroup.dispatchDraw(ViewGroup.java:2797) 07-12 09:12:44.383:E / AndroidRuntime(5245):在android.view.View.draw(View.java:13715) 07-12 09:12:44.383:E / AndroidRuntime(5245):在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):在android.view.ViewRootImpl.drawSoftware(ViewRootImpl.java:2281) 07-12 09:12:44.383:E / AndroidRuntime(5245):在android.view.ViewRootImpl.draw(ViewRootImpl.java:2177) 07-12 09:12:44.383:E / AndroidRuntime(5245):在android.view.ViewRootImpl.performDraw(ViewRootImpl.java:2045) 07-12 09:12:44.383:E / AndroidRuntime(5245):在android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1854) 07-12 09:12:44.383:E / AndroidRuntime(5245):在android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:989) 07-12 09:12:44.383:E / AndroidRuntime(5245):在android.view.ViewRootImpl $ TraversalRunnable.run(ViewRootImpl.java:4351) 07-12 09:12:44.383:E / AndroidRuntime(5245):在android.view.Choreographer $ CallbackRecord.run(Choreographer.java:749) 07-12 09:12:44.383:E / AndroidRuntime(5245):在android.view.Choreographer.doCallbacks(Choreographer.java:562) 07-12 09:12:44.383:E / AndroidRuntime(5245):在android.view.Choreographer.doFrame(Choreographer.java:532) 07-12 09:12:44.383:E / AndroidRuntime(5245):在android.view.Choreographer $ FrameDisplayEventReceiver.run(Choreographer.java:735) 07-12 09:12:44.383:E / AndroidRuntime(5245):在android.os.Handler.handleCallback(Handler.java:725) 07-12 09:12:44.383:E / AndroidRuntime(5245):在android.os.Handler.dispatchMessage(Handler.java:92) 07-12 09:12:44.383:E / AndroidRuntime(5245):在android.os.Looper.loop(Looper.java:137) 07-12 09:12:44.383:E / AndroidRuntime(5245):在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)

请帮帮我.. 提前谢谢。

1 个答案:

答案 0 :(得分:0)

我已经检查了您粘贴的链接。在接受的答案中,我可以在回收后找到这一行

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

在回收位图后,他将ImageView位图设置为null,以便画布不会尝试加载它。由于您尚未在代码中调用上述行,因此canvas将尝试加载您已经回收的位图。请将您的代码更改为

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

希望这有帮助。