Wie kann ich Kontakt Foto von einer Kontaktadresse (Telefonnummer) erhalten?Android - Erhalten Sie Kontakt Foto von Telefonnummer
Antwort
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;
}
Dank dieser Lösung funktioniert Fin außer Sie müssen überprüfen, ob contactId null ist oder nicht, bevor Sie den Eingabestream öffnen –
Sie können die folgende Spalte verwenden, um die Kontakte Foto uri, ContactsContract.CommonDataKinds.Phone.PHOTO_URI zu erhalten.
Rufen Sie diese Methode an, um alle Kontaktinformationen abzurufen.
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 Ich benutze Ihren Code mit ausgewählten Argument, so dass ich kann Foto nach Nummer abrufen –
Sie immer herzlich willkommen ....... –
Dies ist die Funktion:
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;
}
Ich denke, das vesion besser funktioniert in allen Android Versionen:
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;
}
Sie einige Änderungen vornehmen können, wenn Sie wollen Erhalten Sie diegrößeres Foto
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;
}
für das Abrufen der Miniaturgroßen Foto
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;
}
für weitere Einzelheiten:
- 1. Kontakt Telefonnummer aus URI in Android abrufen
- 2. So erhalten Sie Telefonnummern-Typenschild von gegebener Telefonnummer in Android
- 3. auf Kontakt einfügen erhalten Contact_ID
- 4. Erhalten Sie Kontakt SMS-Count und Kontakt Anrufzeit
- 5. Android - Aktualisieren Sie einen Kontakt
- 6. So erhalten Sie die Telefonnummer des SMS-Empfängers in Android
- 7. Android hinzufügen benutzerdefinierte Telefonnummer
- 8. Wie würde ich auch die Telefonnummer von dem Kontakt erhalten, den der Benutzer ausgewählt hat?
- 9. Android-Kontakt-Picker, die E-Mail-Adresse
- 10. Ionic erhalten Benutzer-Telefonnummer
- 11. Uwp erhalten Halter Telefonnummer als ContactInformation
- 12. Android Handy Kontakt 2.1
- 13. Android: Suchkontakt basierend auf der Telefonnummer
- 14. Telegram API, erhalten die Telefonnummer des Absenders?
- 15. Erhalten Sie Kontakt Name von nur der Nummer
- 16. So erhalten Sie UserContactLists von konstanten Kontakt mit C#
- 17. Wie erhalten Sie eine ID von einer Telefonnummer?
- 18. Wie suche ich einen Kontakt mit der Telefonnummer in AgileCRM?
- 19. Wie individuelle Kontakt innen Kontakt wie WhatsApp android
- 20. Android - Kontakt von Nummer mit Ländercode
- 21. Wie kann ich ein Foto für einen Kontakt Bibliothek mit dem Google Kontakt .NET-Client laden
- 22. Erhalten Sie Empfängernummer von SMS in Android
- 23. So erhalten Sie die Telefonnummer von Person Name, die in meiner Kontaktliste sind in Android
- 24. Joining Google und Facebook Kontakt auf Android
- 25. Android: Löschen Code Land von einem Kontakt
- 26. Hinzufügen von Kontakt mit Intent in Android
- 27. Telefonbuch Einstellungen (dh Kontakt nur Filter) Android-Code Beispiel
- 28. Android: Abrufen des Kontaktnamens von der Telefonnummer
- 29. android facebook veröffentlichen foto
- 30. Android - Capture Foto
beziehen sich diese URL: http://developer.android.com/reference /android/provider/ContactsContract.Contacts.Photo.html – ATRS
Mögliche Duplikate von [wie man Kontakt Foto URI erhalten] (http://StackOverflow.com/questions/7738192/how-to-get-kontaktieren-Foto-uri) – Mussa
Versuchen Sie, vielleicht hilft es Ihnen http://StackOverflow.com/A/38992166/4593930 –