在android我想獲取聯繫人數據,我得到了號碼成功,但聯繫人照片我面臨問題。我得到了聯繫人照片URI content://com.android.contacts/data/6376/photo
但是當我將它設置爲圖像視圖,圖像視圖將空白android:獲取聯繫人照片不工作,嘗試了很多東西,但仍然沒有工作
/*getting activity result */
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1) {
// return from file upload
if (resultCode == Activity.RESULT_OK) {
if (data != null) {
Uri uri = data.getData();
String contactID="";
if (uri != null) {
Cursor c = null;
try {
c = getContentResolver().query(uri, new String[]{
ContactsContract.CommonDataKinds.Phone.NUMBER,
ContactsContract.CommonDataKinds.Phone.TYPE,
ContactsContract.Contacts._ID
//ContactsContract.CommonDataKinds.Phone.p
}, null, null, null);
if (c != null && c.moveToFirst()) {
String number = c.getString(0);
int type = c.getInt(1);
contactID = c.getString(2);
Bitmap photoBitmap = null;
Uri photo=null;
try {
photo = Uri.withAppendedPath(uri, ContactsContract.Contacts.Photo.CONTENT_DIRECTORY);
ImageView imageView = (ImageView) findViewById(R.id.imageView1);
imageView.setImageURI(photo);
// ImageView profile = (ImageView)findViewById(R.id.imageView1);
// Uri my_contact_Uri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_URI, contactID);
// InputStream photo_stream = ContactsContract.Contacts.openContactPhotoInputStream(getContentResolver(), my_contact_Uri);
// BufferedInputStream buf = new BufferedInputStream(photo_stream);
// Bitmap my_btmp = BitmapFactory.decodeStream(buf);
// profile.setImageBitmap(my_btmp);
} catch (Exception e) {
e.printStackTrace();
}
// contactNumber = cursor.getString(cursor.getColumnIndex((ContactsContract.CommonDataKinds.Phone.NUMBER)));
showSelectedNumber(type, number,contactID,photo.toString());
}
// Bitmap photo = null;
//
// try {
//
// InputStream inputStream = ContactsContract.Contacts.openContactPhotoInputStream(getContentResolver(),
// ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, new Long(contactID)));
//
//
//
// if (inputStream != null) {
// photo = BitmapFactory.decodeStream(inputStream);
// ImageView imageView = (ImageView) findViewById(R.id.imageView1);
// imageView.setImageBitmap(photo);
// }
//
// assert inputStream != null;
// inputStream.close();
//
// } catch (IOException e) {
// e.printStackTrace();
// }
}catch(Exception e){
Log.w(TAG,e+"");
} finally {
if (c != null) {
c.close();
}
}
}
}
} else {
Log.w(TAG, "Unknown Activity Result from add contact: "
+ resultCode);
}
}
}
在上面的代碼我試過很多東西,比如輸入流等,但仍然沒有得到聯繫人的照片但成功獲取照片路徑。
當我要在圖像視圖上設置數據時,它不會顯示任何內容。
看看我的回答,並相應地嘗試。 – GrIsHu