在android我试图获取联系人数据,我获得了数字的成功,但对于联系人照片我面临问题。我联系了照片uri content://com.android.contacts/data/6376/photo
但是当我将它设置为图像视图时,图像视图将会空白
/*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);
}
}
}
在上面的代码中,我尝试了很多东西,比如输入流等,但仍然没有得到联系人的照片但成功获得了照片路径。
当我要在图像视图上设置数据时,它没有显示任何内容。
答案 0 :(得分:0)
如果您已经拥有该路径,可以试试这个:
private void previewImage(String path) {
try {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 5; // setting the photo quality
final Bitmap bitmap = BitmapFactory.decodeFile(path, options);
photo.setImageBitmap(bitmap);
} catch (NullPointerException e) {
e.printStackTrace();
}
}
答案 1 :(得分:0)
检索缩略图图片请遵循此代码..
public InputStream openPhoto(long contactId) {
Uri contactUri = ContentUris.withAppendedId(Contacts.CONTENT_URI, contactId);
Uri photoUri = Uri.withAppendedPath(contactUri, Contacts.Photo.CONTENT_DIRECTORY);
Cursor cursor = getContentResolver().query(photoUri,
new String[] {Contacts.Photo.PHOTO}, null, null, null);
if (cursor == null) {
return null;
}
try {
if (cursor.moveToFirst()) {
byte[] data = cursor.getBlob(0);
if (data != null) {
return new ByteArrayInputStream(data);
}
}
} finally {
cursor.close();
}
return null;
}
答案 2 :(得分:0)
尝试以下代码,它将帮助您获取联系号码及其ID,并从该ID号码中获取联系号码。
public class DemoContact extends Activity {
private static final String TAG = DemoContact.class.getSimpleName();
private static final int REQUEST_CODE_PICK_CONTACTS = 1;
private Uri uriContact;
private String contactID, contactName; // contacts unique ID
private TextView m_contactName;
/**
* Called when the activity is first created.
*/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
m_contactName = (TextView) findViewById(R.id.textView1);
}
public void onClickSelectContact(View btnSelectContact) {
// using native contacts selection
// Intent.ACTION_PICK = Pick an item from the data, returning what was
// selected.
startActivityForResult(new Intent(Intent.ACTION_PICK,
ContactsContract.Contacts.CONTENT_URI),
REQUEST_CODE_PICK_CONTACTS);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_CODE_PICK_CONTACTS
&& resultCode == RESULT_OK) {
Log.d(TAG, "Response: " + data.toString());
uriContact = data.getData();
retrieveContactNumber();
retrieveContactPhoto();
m_contactName.setText("Contact Name: "+contactName);
}
}
//Retrieve the Contact photo based on the contactId
private void retrieveContactPhoto() {
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.img_contact);
imageView.setImageBitmap(photo);
}
assert inputStream != null;
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
//Get the contact number of the selected contact.
private void retrieveContactNumber() {
String contactNumber = null;
// getting contacts ID
Cursor cursorID = getContentResolver().query(uriContact,
new String[] { ContactsContract.Contacts._ID,ContactsContract.Contacts.DISPLAY_NAME }, null, null,
null);
if (cursorID.moveToFirst()) {
contactID = cursorID.getString(cursorID
.getColumnIndex(ContactsContract.Contacts._ID));
contactName = cursorID.getString(cursorID .getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
}
cursorID.close();
Log.d(TAG, "Contact ID: " + contactID);
// Using the contact ID now we will get contact phone number
Cursor cursorPhone = getContentResolver().query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
new String[] { ContactsContract.CommonDataKinds.Phone.NUMBER },
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ? AND "
+ ContactsContract.CommonDataKinds.Phone.TYPE + " = "
+ ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE,
new String[] { contactID }, null);
if (cursorPhone.moveToFirst()) {
contactNumber = cursorPhone
.getString(cursorPhone
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
}
cursorPhone.close();
Log.d(TAG, "Contact Phone Number: " + contactNumber);
}
}
<强>输出:强>