我在Android中为联系人加载照片时遇到问题.我已经google了一个答案,但到目前为止已经空了.有没有人有查询联系人,然后加载照片的例子?
因此,给定一个来自一个名为using的Activity结果的contactUri
startActivityForResult(new Intent(Intent.ACTION_PICK,ContactsContract.CommonDataKinds.Phone.CONTENT_URI),PICK_CONTACT_REQUEST)
Run Code Online (Sandbox Code Playgroud)
是:
内容://com.android.contacts/data/1557
loadContact(..)工作正常.但是,当我调用getPhoto(...)方法时,我得到了照片InputStream的空值.它也令人困惑,因为URI值不同.contactPhotoUri评估为:
内容://com.android.contacts/contacts/1557
请参阅下面代码中的内联注释.
class ContactAccessor {
/**
* Retrieves the contact information.
*/
public ContactInfo loadContact(ContentResolver contentResolver, Uri contactUri) {
//contactUri --> content://com.android.contacts/data/1557
ContactInfo contactInfo = new ContactInfo();
// Load the display name for the specified person
Cursor cursor = contentResolver.query(contactUri,
new String[]{Contacts._ID,
Contacts.DISPLAY_NAME,
Phone.NUMBER,
Contacts.PHOTO_ID}, null, null, null);
try {
if (cursor.moveToFirst()) {
contactInfo.setId(cursor.getLong(0));
contactInfo.setDisplayName(cursor.getString(1));
contactInfo.setPhoneNumber(cursor.getString(2));
}
} finally {
cursor.close();
}
return contactInfo; // <-- returns info for contact
}
public Bitmap …Run Code Online (Sandbox Code Playgroud) 好吧,我只是想学习使用联系信息,但我有点卡住了.我希望能够为联系人显示图片.使用我拥有的以下代码,我如何能够将联系人的照片放在contact_entry中的ImageView中?
ListView contacts_list = (ListView) findViewById(R.id.contacts_list);
// Gets the URI of the db
Uri uri = ContactsContract.Contacts.CONTENT_URI;
// What to grab from the db
String[] projection = new String[] {
ContactsContract.Contacts._ID,
ContactsContract.Contacts.DISPLAY_NAME,
ContactsContract.Contacts.PHOTO_ID
};
String sortOrder = ContactsContract.Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC";
Cursor cursor = managedQuery(uri, projection, null, null, sortOrder);
String[] fields = new String[] {
ContactsContract.Data.DISPLAY_NAME
};
int[] values = {
R.id.contactEntryText
};
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.contact_entry, cursor,
fields, values);
contacts_list.setAdapter(adapter);
Run Code Online (Sandbox Code Playgroud)
contact_entry.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout …Run Code Online (Sandbox Code Playgroud) 我正在使用Android Contact ContentProvider.我有一个电话号码,我需要得到URI中的照片与该电话号码相关联的接触.我该怎么做???
我知道我可以获取照片的原始数据并构建一个InputStream,但我不想要输入流,我需要URI.
编辑:最初我使用以下代码来获取联系信息
Uri uri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(phoneNo));
Cursor cursor = context.getContentResolver().query(uri, details, null, null, null);
Run Code Online (Sandbox Code Playgroud) People.CONTENT_URI通过简单的查询, 查询联系人图片非常容易
People.loadContactPhoto(activity, ContentUris.withAppendedId(People.CONTENT_URI, contactId)
Run Code Online (Sandbox Code Playgroud)
因为我知道联系人ID.现在我需要在访问呼叫日志后做同样的事情.附:
String[] strFields = {
android.provider.CallLog.Calls.CACHED_NAME,
android.provider.CallLog.Calls.NUMBER,
};
String strUriCalls="content://call_log/calls";
Uri UriCalls = Uri.parse(strUriCalls);
Cursor cursorLog = this.getContentResolver().query(UriCalls, strFields, null, null, null);
Run Code Online (Sandbox Code Playgroud)
我从通话记录列表中,但我不能找到加载照片所需要的接触ID连接这一点任何方式.该应用程序适用于api level 4+.
任何帮助表示赞赏.谢谢.
在下面的Cristian的指导下,对我有用的解决方案是:
private long getContactIdFromNumber(String number) {
String[] projection = new String[]{Contacts.Phones.PERSON_ID};
Uri contactUri = Uri.withAppendedPath(Contacts.Phones.CONTENT_FILTER_URL,Uri.encode(number));
Cursor c = getContentResolver().query(contactUri, projection, null, null, null);
if (c.moveToFirst()) {
long contactId=c.getLong(c.getColumnIndex(Contacts.Phones.PERSON_ID));
return contactId;
}
return -1;
}
Run Code Online (Sandbox Code Playgroud)