如何获取联系人照片URI [英] how to get contact photo URI
问题描述
我与Android联系ContentProvider的工作。我有一个的电话号码的,我需要得到的照片的的的 URI 的与该电话号码相关联的接触。我该怎么办呢???
我知道我能得到的的原始数据的照片和建设的的InputStream 的,但我不想输入流,我需要的 URI 。
编辑:最初我用以下code,以获取联系人信息
开放的URI = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI,Uri.en code(PHONENO));
光标光标= context.getContentResolver()查询(URI,细节,NULL,NULL,NULL);
要使用的电话号码请使用以下code得到conatct ID:
进口android.provider.ContactsContract.PhoneLookup;
公共字符串fetchContactIdFromPhoneNumber(字符串phoneNumber的){
开放的我们的uri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI,
Uri.en code(phoneNumber的));
光标光标= this.getContentResolver()查询(URI,
新的String [] {PhoneLookup.DISPLAY_NAME,PhoneLookup._ID},
NULL,NULL,NULL);
字符串的ContactID =;
如果(cursor.moveToFirst()){
做 {
使用ContactID = cursor.getString(光标
.getColumnIndex(PhoneLookup._ID));
}而(cursor.moveToNext());
}
返回的ContactID;
}
和使用得到得到contatc照片URI的接触ID。使用下面的code获取照片URI:
进口android.provider.ContactsContract;
进口android.provider.ContactsContract.CommonDataKinds.Phone;
公众开放的getPhotoUri(长的ContactID){
ContentResolver的ContentResolver的= getContentResolver();
尝试 {
光标光标= ContentResolver的
.query(ContactsContract.Data.CONTENT_URI,
空值,
ContactsContract.Data.CONTACT_ID
+=
+的ContactID
+和
+ ContactsContract.Data.MIMETYPE
+=
+ ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE
+',NULL,NULL);
如果(光标!= NULL){
如果(!cursor.moveToFirst()){
返回null; //没有照片
}
} 其他 {
返回null; //在光标过程中的错误
}
}赶上(例外五){
e.printStackTrace();
返回null;
}
乌里人= ContentUris.withAppendedId(
ContactsContract.Contacts.CONTENT_URI,使用ContactID);
返回Uri.withAppendedPath(人,
ContactsContract.Contacts.Photo.CONTENT_DIRECTORY);
}
希望这会有所帮助。
I am working with Android Contact ContentProvider. I have a Phone Number and I need to get the URI of the Photo of the contact associated with this phone number. How can I do it???
I know I can get the raw data of the photo and build an InputStream, but I dont want the input stream, I need the URI.
EDIT: Originally I'm using following code to fetch contact info
Uri uri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(phoneNo));
Cursor cursor = context.getContentResolver().query(uri, details, null, null, null);
To get the conatct id using the phone number use the following code:
import android.provider.ContactsContract.PhoneLookup;
public String fetchContactIdFromPhoneNumber(String phoneNumber) {
Uri uri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI,
Uri.encode(phoneNumber));
Cursor cursor = this.getContentResolver().query(uri,
new String[] { PhoneLookup.DISPLAY_NAME, PhoneLookup._ID },
null, null, null);
String contactId = "";
if (cursor.moveToFirst()) {
do {
contactId = cursor.getString(cursor
.getColumnIndex(PhoneLookup._ID));
} while (cursor.moveToNext());
}
return contactId;
}
and use the contact id obtained to get the contatc photo URI. Use the following code for getting photo URI:
import android.provider.ContactsContract;
import android.provider.ContactsContract.CommonDataKinds.Phone;
public Uri getPhotoUri(long contactId) {
ContentResolver contentResolver = getContentResolver();
try {
Cursor cursor = contentResolver
.query(ContactsContract.Data.CONTENT_URI,
null,
ContactsContract.Data.CONTACT_ID
+ "="
+ contactId
+ " AND "
+ ContactsContract.Data.MIMETYPE
+ "='"
+ ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE
+ "'", null, null);
if (cursor != null) {
if (!cursor.moveToFirst()) {
return null; // no photo
}
} else {
return null; // error in cursor process
}
} catch (Exception e) {
e.printStackTrace();
return null;
}
Uri person = ContentUris.withAppendedId(
ContactsContract.Contacts.CONTENT_URI, contactId);
return Uri.withAppendedPath(person,
ContactsContract.Contacts.Photo.CONTENT_DIRECTORY);
}
Hope this would help.
这篇关于如何获取联系人照片URI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!