我在获取和设置联系人的图像作为视图的背景时遇到了问题,令人惊讶的是,关于如何执行此操作的示例很少.我正在尝试构建类似于显示大型联系人照片的人物应用程序.
这就是我现在正在做的事情:
Uri uri = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, Long.valueOf(id));
InputStream input = ContactsContract.Contacts.openContactPhotoInputStream(context.getContentResolver(), uri);
Bitmap bm = BitmapFactory.decodeStream(input);
Drawable d = new BitmapDrawable(bm);
button.setBackgroundDrawable(drawable);
Run Code Online (Sandbox Code Playgroud)
这适用于它使用的URI获取缩略图,因此即使有大照片,当缩放以适合imageView时,图像看起来非常糟糕.我知道另一种方法来获取实际获得大照片的URI:
final Uri imageUri = Uri.parse(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.PHOTO_URI)));
Run Code Online (Sandbox Code Playgroud)
但是我没有设法将它带到imageView,也许上面的代码可以适应使用第二个uri.如果您知道如何使用第二个uri,或者如果有更简单的方法来获取联系人图像而不是通过URI,请告诉我.任何信息都将被感谢.
我试图用SimpleCursorAdapter一个ViewBinder来从数据库中获取的图像,并把它放到我的ListView项目视图.这是我的代码:
private void setUpViews() {
mNewsView = (ListView) findViewById(R.id.news_list);
Cursor cursor = getNews();
SimpleCursorAdapter curAdapter = new SimpleCursorAdapter(
getApplicationContext(), R.layout.cursor_item, cursor,
new String[] { "title", "content", "image" },
new int[] { R.id.cursor_title, R.id.cursor_content,
R.id.news_image });
ViewBinder viewBinder = new ViewBinder() {
public boolean setViewValue(View view, Cursor cursor,
int columnIndex) {
ImageView image = (ImageView) view;
byte[] byteArr = cursor.getBlob(columnIndex);
image.setImageBitmap(BitmapFactory.decodeByteArray(byteArr, 0, byteArr.length));
return true;
}
};
ImageView image = (ImageView) findViewById(R.id.news_image);
viewBinder.setViewValue(image, cursor, cursor.getColumnIndex("image")); …Run Code Online (Sandbox Code Playgroud) android blob cursor indexoutofboundsexception android-viewbinder
使用我目前的代码,我只得到联系电话.但我想得到contact's name and contact's photo path.通过谷歌搜索尝试了很多代码,但我无法完成它.试过这个,但得到了FileNotFoundException.有人可以通过在下面的代码中添加代码片段来帮助我实现这一目标吗?
public void getContact(View view)
{
Intent intent = new Intent(Intent.ACTION_PICK);
intent.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_TYPE);
startActivityForResult(intent, 1);
}
protected void onActivityResult(int requestCode, int resultCode,Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
if(resultCode==RESULT_OK && requestCode == 1)
{
if (data != null) {
Uri uri = data.getData();
if (uri != null) {
Cursor c = null;
try {
c = getContentResolver().query(uri, new String[]{
ContactsContract.CommonDataKinds.Phone.NUMBER,
ContactsContract.CommonDataKinds.Phone.TYPE },
null, null, null);
if (c != null && c.moveToFirst()) …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)