アンドロイドは初めてです。水平ビューでの画像のリサイクルに関連する問題がいくつかあります。このリンク「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);
そして、これが「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;
}
}
これをエミュレーターで実行すると、次のようなエラー ログが表示されます。
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(ネイティブ メソッド) 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(ネイティブ メソッド) で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(ネイティブ メソッド) 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(ネイティブメソッド)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(ネイティブ メソッド) 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(ネイティブメソッド)Android.app.ActivityThread.main(ActivityThread.java:5041) 07-12 09:12:44.383: E/AndroidRuntime(5245): java.lang.reflect.Method.invokeNative(ネイティブ メソッド) 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(ネイティブ メソッド) でAndroid.app.ActivityThread.main(ActivityThread.java:5041) 07-12 09:12:44.383: E/AndroidRuntime(5245): java.lang.reflect.Method.invokeNative(ネイティブ メソッド) 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(ネイティブ メソッド) で(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(ネイティブメソッド)
助けてください..よろしくお願いします。