我是 android 新手。我遇到了一些与水平视图中的图像回收相关的问题。我试过这个链接“如何从画廊动态卸载图像? ”作为参考。
这是我的 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);
这是我的适配器类,名为“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;
}
}
当我在模拟器上运行它时,我得到的错误日志为:
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(视图组。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): 在 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): 在 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): 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) 07-12 09:12:44.383: E/AndroidRuntime(5245): 在 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)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): 在 java.lang.reflect.Method.invokeNative(Native Method) 07-12 09:12:44.383: E/AndroidRuntime( 5245): 在 java.lang.reflect.Method.invoke(Method.java:511) 07-12 09:12:44.383: E/AndroidRuntime(5245): 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run (ZygoteInit.java:793) 07-12 09:12:44.383: E/AndroidRuntime(5245): 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 07-12 09:12:44.383 : E/AndroidRuntime(5245): 在 dalvik.system.NativeStart.main(Native Method)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): 在 java.lang.reflect.Method.invokeNative(Native Method) 07-12 09:12:44.383: E/AndroidRuntime( 5245): 在 java.lang.reflect.Method.invoke(Method.java:511) 07-12 09:12:44.383: E/AndroidRuntime(5245): 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run (ZygoteInit.java:793) 07-12 09:12:44.383: E/AndroidRuntime(5245): 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 07-12 09:12:44.383 : E/AndroidRuntime(5245): 在 dalvik.system.NativeStart.main(Native Method)在 android.app.ActivityThread.main(ActivityThread.java:5041) 07-12 09:12:44.383: E/AndroidRuntime(5245): 在 java.lang.reflect.Method.invokeNative(Native Method) 07-12 09: 12:44.383: E/AndroidRuntime(5245): 在 java.lang.reflect.Method.invoke(Method.java:511) 07-12 09:12:44.383: E/AndroidRuntime(5245): 在 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)在 android.app.ActivityThread.main(ActivityThread.java:5041) 07-12 09:12:44.383: E/AndroidRuntime(5245): 在 java.lang.reflect.Method.invokeNative(Native Method) 07-12 09: 12:44.383: E/AndroidRuntime(5245): 在 java.lang.reflect.Method.invoke(Method.java:511) 07-12 09:12:44.383: E/AndroidRuntime(5245): 在 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)调用(Method.java:511)07-12 09:12:44.383:E/AndroidRuntime(5245):在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)07-12 09: 12:44.383: E/AndroidRuntime(5245): 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 07-12 09:12:44.383: E/AndroidRuntime(5245): 在 dalvik.system .NativeStart.main(本机方法)调用(Method.java:511)07-12 09:12:44.383:E/AndroidRuntime(5245):在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)07-12 09: 12:44.383: E/AndroidRuntime(5245): 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 07-12 09:12:44.383: E/AndroidRuntime(5245): 在 dalvik.system .NativeStart.main(本机方法)
请帮助我..提前谢谢。