Tôi làm cách nào để có được ảnh liên hệ từ địa chỉ của số liên lạc (số điện thoại)?Android - Nhận Ảnh Liên hệ từ số điện thoại
Trả lời
public static Bitmap retrieveContactPhoto(Context context, String number) {
ContentResolver contentResolver = context.getContentResolver();
String contactId = null;
Uri uri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(number));
String[] projection = new String[]{ContactsContract.PhoneLookup.DISPLAY_NAME, ContactsContract.PhoneLookup._ID};
Cursor cursor =
contentResolver.query(
uri,
projection,
null,
null,
null);
if (cursor != null) {
while (cursor.moveToNext()) {
contactId = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.PhoneLookup._ID));
}
cursor.close();
}
Bitmap photo = BitmapFactory.decodeResource(context.getResources(),
R.drawable.default_image);
try {
InputStream inputStream = ContactsContract.Contacts.openContactPhotoInputStream(context.getContentResolver(),
ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, new Long(contactId)));
if (inputStream != null) {
photo = BitmapFactory.decodeStream(inputStream);
}
assert inputStream != null;
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
return photo;
}
Cảm ơn giải pháp này đang làm việc ngoại trừ việc bạn phải kiểm tra xem contactId có rỗng hay không trước khi mở luồng đầu vào –
Bạn có thể sử dụng cột bên dưới để lấy uri ảnh của địa chỉ liên hệ, Liên hệContract.CommonDataKinds.Phone.PHOTO_URI.
Gọi phương thức này để nhận tất cả thông tin liên hệ.
public void readContacts() {
StringBuffer sb = new StringBuffer();
sb.append("......Contact Details.....");
ContentResolver cr = getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null,
null, null, null);
String phone = null;
String emailContact = null;
String emailType = null;
String image_uri = "";
Bitmap bitmap = null;
if (cur.getCount() > 0) {
while (cur.moveToNext()) {
String id = cur.getString(cur
.getColumnIndex(ContactsContract.Contacts._ID));
String name = cur
.getString(cur
.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
image_uri = cur
.getString(cur
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.PHOTO_URI));
if (Integer
.parseInt(cur.getString(cur
.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
System.out.println("name : " + name + ", ID : " + id);
sb.append("\n Contact Name:" + name);
Cursor pCur = cr.query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID
+ " = ?", new String[] { id }, null);
while (pCur.moveToNext()) {
phone = pCur
.getString(pCur
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
sb.append("\n Phone number:" + phone);
System.out.println("phone" + phone);
}
pCur.close();
Cursor emailCur = cr.query(
ContactsContract.CommonDataKinds.Email.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Email.CONTACT_ID
+ " = ?", new String[] { id }, null);
while (emailCur.moveToNext()) {
emailContact = emailCur
.getString(emailCur
.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
emailType = emailCur
.getString(emailCur
.getColumnIndex(ContactsContract.CommonDataKinds.Email.TYPE));
sb.append("\nEmail:" + emailContact + "Email type:" + emailType);
System.out.println("Email " + emailContact
+ " Email Type : " + emailType);
}
emailCur.close();
}
if (image_uri != null) {
System.out.println(Uri.parse(image_uri));
try {
bitmap = MediaStore.Images.Media
.getBitmap(this.getContentResolver(),
Uri.parse(image_uri));
sb.append("\n Image in Bitmap:" + bitmap);
System.out.println(bitmap);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
sb.append("\n........................................");
}
textDetail.setText(sb);
}
}
tnx Tôi sử dụng mã của bạn với đối số chọn để tôi có thể truy xuất hình ảnh theo số –
Bạn luôn chào mừng bạn thân yêu ....... –
Đây là chức năng:
public static Bitmap getContactsDetails(String address) {
Bitmap bp = BitmapFactory.decodeResource(context.getResources(),
R.drawable.default_contact_photo);
String selection = ContactsContract.CommonDataKinds.Phone.NUMBER + " = '" + address + "'";
Cursor phones = context.getContentResolver().query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, selection,
null, null);
while (phones.moveToNext()) {
String image_uri = phones.getString(phones.getColumnIndex(
ContactsContract.CommonDataKinds.Phone.PHOTO_URI));
if (image_uri != null) {
try {
bp = MediaStore.Images.Media
.getBitmap(context.getContentResolver(),
Uri.parse(image_uri));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
return bp;
}
Tôi nghĩ vesion này hoạt động tốt hơn trong tất cả các phiên bản android:
public Bitmap getContactsDetails(String address) {
Bitmap bp = BitmapFactory.decodeResource(context.getResources(),
R.drawable.contact_default_picture);
Uri contactUri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(address));
// querying contact data store
Cursor phones = context.getContentResolver().query(contactUri, null, null, null, null);
while (phones.moveToNext()) {
String image_uri = phones.getString(phones.getColumnIndex(
ContactsContract.CommonDataKinds.Phone.PHOTO_URI));
if (image_uri != null) {
try {
bp = MediaStore.Images.Media
.getBitmap(context.getContentResolver(),
Uri.parse(image_uri));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
return bp;
}
Bạn có thể thực hiện một số thay đổi nếu bạn muốn lấy Retrievingảnh lớn hơn
public Bitmap retrieveContactPhoto(Context context, String number) {
ContentResolver contentResolver = context.getContentResolver();
String contactId = null;
Uri uri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(number));
String[] projection = new String[]{ContactsContract.PhoneLookup.DISPLAY_NAME, ContactsContract.PhoneLookup._ID};
Cursor cursor =
contentResolver.query(
uri,
projection,
null,
null,
null);
if (cursor != null) {
while (cursor.moveToNext()) {
contactId = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.PhoneLookup._ID));
}
cursor.close();
}
Bitmap photo = BitmapFactory.decodeResource(context.getResources(),
R.mipmap.popup);
try {
Uri contactUri = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, Long.valueOf(contactId));
Uri displayPhotoUri = Uri.withAppendedPath(contactUri, ContactsContract.Contacts.Photo.DISPLAY_PHOTO);
AssetFileDescriptor fd =
getContentResolver().openAssetFileDescriptor(displayPhotoUri, "r");
InputStream inputStream=fd.createInputStream();
if (inputStream != null) {
photo = BitmapFactory.decodeStream(inputStream);
}
assert inputStream != null;
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
return photo;
}
để tìm các mục hình thu nhỏ kích thước ảnh
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;
}
để xem chi tiết hơn:
- 1. Android: Truy xuất tên liên hệ từ số điện thoại
- 2. android nhận hình ảnh, tên và số điện thoại liên hệ
- 3. Android: Tìm kiếm liên hệ dựa trên số điện thoại
- 4. Điện thoại quay số sai từ liên kết điện thoại
- 5. Nhận số điện thoại liên lạc chỉ
- 6. Android: cách nhận số điện thoại từ SMS đến?
- 7. Tìm kiếm liên hệ theo số điện thoại
- 8. Magento: Nhận số điện thoại liên lạc của cửa hàng
- 9. Android - Hiển thị danh sách liên hệ có cùng số điện thoại
- 10. Liên hệ iOS Cách tìm nạp địa chỉ liên hệ theo số điện thoại
- 11. Cách nhận số điện thoại của điện thoại trong mã Android?
- 12. Cách truy xuất Tên và số điện thoại liên hệ trong Android
- 13. Android Liên Picker với số điện thoại duy nhất
- 14. Nhận Ảnh từ một Số liên lạc
- 15. Android: cách nhận số điện thoại người gửi và người nhận từ sms thô
- 16. Nhận tên liên lạc được cung cấp số điện thoại trong Android
- 17. Máy chủ không nhận ra cookie từ điện thoại Android
- 18. Truy xuất số điện thoại liên lạc từ URI trong Android
- 19. Nhận số điện thoại di động từ facebook api
- 20. nhà cung cấp Liên hệ Android chỉ nhận được danh bạ điện thoại với tất cả các email
- 21. Quyền của Android: Cuộc gọi điện thoại: đọc trạng thái điện thoại và nhận dạng
- 22. Cách nhận số điện thoại di động nếu điện thoại Android của tôi có hai thẻ SIM?
- 23. Thay đổi ngày hệ thống của điện thoại android trên nút bấm từ ứng dụng android
- 24. Cách nhận số điện thoại của người nhận SMS trong Android
- 25. Cách biên dịch android cho điện thoại
- 26. Thêm liên hệ mới qua mục đích với nhiều số điện thoại
- 27. Nhận ảnh từ Liên Swift
- 28. sqlite danh bạ chiết xuất Android với số điện thoại
- 29. Android - Cách nhận liên hệ từ nhật ký cuộc gọi?
- 30. Cách hiệu quả để tải tất cả các số liên lạc và tất cả các số điện thoại (Android 2.0)
tham khảo url này: http://developer.android.com/reference /android/provider/ContactsContract.Contacts.Photo.html – ATRS
Bản sao có thể có của [cách nhận URI ảnh liên hệ] (http://stackoverflow.com/question/7738192/how-to-get-contact-photo-uri) – Mussa
Hãy thử điều này, có thể nó sẽ giúp bạn http://stackoverflow.com/a/38992166/4593930 –