【发布时间】:2014-01-14 21:49:21
【问题描述】:
我正在努力通过光标获取联系人。一切正常,但显示联系人照片并没有返回任何内容。我没有遇到任何崩溃或错误。我的代码是
ContactListActivity.java
Cursor cur= getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,null,null,null);
list.setAdapter(new contactAdapter(getApplicationContext(), cur));
我自定义创建的光标适配器是
class contactAdapter extends CursorAdapter{
String Name, phoneNumber;
private Cursor cursor;
private Context ccontext;
private LayoutInflater inflater;
public contactAdapter(Context context, Cursor c) {
super(context, c);
// TODO Auto-generated constructor stub
cursor = c;
ccontext = context;
inflater = LayoutInflater.from(context);
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@SuppressLint("InlinedApi")
@SuppressWarnings("deprecation")
@Override
public void bindView(View view, Context arg1, Cursor arg2) {
// TODO Auto-generated method stub
ViewHolder holder = (ViewHolder) view.getTag();
if (holder == null) {
holder = new ViewHolder();
holder.contactsImage = (ImageView) view.findViewById(R.id.contact_image);
holder.ContactName = (TextView) view.findViewById(R.id.contact_name);
holder.contactCheck = (CheckBox) view.findViewById(R.id.contact_check);
view.setTag(holder);
holder.contactCheck.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
cb = (CheckBox) v;
cb.setChecked(cb.isChecked());
// ContactPerson selected = (ContactPerson)cb.getTag();
Log.d("selcted", cb.getTag().toString());
if(cb.isChecked()){
phoneID.add(cb.getTag().toString());
}
}
});
}else{
holder = (ViewHolder) view.getTag();
}
// Uri uri = ContentUris.withAppendedId(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, cursor.getLong(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone._ID)));
// Log.d("ImagePath", ContactsContract.CommonDataKinds.Phone.PHOTO_FILE_ID);
if(cursor.getString(cursor.getColumnIndex("mimetype")).equals(ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE)){
byte[] photoByte = cursor.getBlob(cursor.getColumnIndex("data15"));
if(photoByte != null) {
Bitmap bitmap = BitmapFactory.decodeByteArray(photoByte, 0, photoByte.length);
// Getting Caching directory
File cacheDirectory = getBaseContext().getCacheDir();
// Temporary file to store the contact image
File tmpFile = new File(cacheDirectory.getPath() + "/wpta_"+cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.PHOTO_ID))+".png");
// The FileOutputStream to the temporary file
try {
FileOutputStream fOutStream = new FileOutputStream(tmpFile);
// Writing the bitmap to the temporary file as png file
bitmap.compress(Bitmap.CompressFormat.PNG,100, fOutStream);
// Flush the FileOutputStream
fOutStream.flush();
//Close the FileOutputStream
fOutStream.close();
holder.contactsImage.setImageBitmap(bitmap);
} catch (Exception e) {
e.printStackTrace();
}
String photoPath = tmpFile.getPath();
Log.d("photoPath", photoPath);
}
}
// holder.contactsImage.setImageBitmap(bitmap);
holder.ContactName.setText(cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME)));
holder.contactCheck.setTag(cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.CONTACT_ID)));
holder.contactCheck.setChecked(false);
}
@Override
public View newView(Context arg0, Cursor arg1, ViewGroup arg2) {
// TODO Auto-generated method stub
return inflater.inflate(R.layout.checkbox_item, arg2, false);
}
@Override
public View getView(int arg0, View arg1, ViewGroup arg2) {
// TODO Auto-generated method stub
// Log.d("don't know", cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)));
return super.getView(arg0, arg1, arg2);
}
}
谁能告诉我哪里出错并指导我获取照片。我到处搜索并实现了代码。但它们都没有产生所需的结果。
【问题讨论】:
标签: android android-contentprovider android-contacts android-cursoradapter