【问题标题】:Pulling a contact's picture isn't working提取联系人的图片不起作用
【发布时间】:2018-04-06 17:17:21
【问题描述】:

我是Call Manager 的开发者。我在向我的应用添加一些新功能时遇到了一些困难。

目前,当添加新呼叫(或编辑现有呼叫)时,用户可以从自己的联系人列表中选择联系人,姓名和电话号码将填充相应的字段。我现在正在尝试检索联系人的照片,但它不起作用。我检查了许多帖子,但没有任何效果。

目前获取联系人信息的代码是:

// Retrieve the contact data and set name and number to the appropriate fields
private void contactPicked(Intent data){

    ContentResolver cr = getContentResolver();
    Uri uri = data.getData();
    Cursor cur = cr.query(uri, null, null, null, null);
    cur.moveToFirst();

    String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
    String name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
    nameField.setText(name);
    String number = cur.getString(cur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
    numField.addTextChangedListener(new PhoneNumberFormattingTextWatcher());
    numField.setText(number);
    numField.addTextChangedListener(new PhoneNumberFormattingTextWatcher());;
}

这按预期工作。

目前的新代码如下所示:

// Retrieve the contact data and set name and number to the appropriate fields
private void contactPicked(Intent data){

    ContentResolver cr = getContentResolver();
    Uri uri = data.getData();
    Cursor cur = cr.query(uri, null, null, null, null);
    cur.moveToFirst();

    String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
    String name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
    nameField.setText(name);
    String number = cur.getString(cur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));

    InputStream photo_stream = ContactsContract.Contacts.openContactPhotoInputStream(cr, uri, true);
    BufferedInputStream buf = new BufferedInputStream(photo_stream);
    Bitmap my_btmp = BitmapFactory.decodeStream(buf);
    try {
        buf.close();
    } catch (IOException e) {
        e.printStackTrace();
    }

    numField.addTextChangedListener(new PhoneNumberFormattingTextWatcher());
    numField.setText(number);
    numField.addTextChangedListener(new PhoneNumberFormattingTextWatcher());

    Drawable photo = new BitmapDrawable(getResources(), my_btmp);
    contactPhoto.setBackground(photo);
}

如果有人能帮我解决这个问题,我将不胜感激。请随时通过GitHub repo 进一步检查代码以获得进一步说明。谢谢!

【问题讨论】:

    标签: java android android-contacts


    【解决方案1】:

    试试这个:

    Uri contactUri = ContentUris.withAppendedId(Contacts.CONTENT_URI, id); // create a plain contactUri from the _ID, instead of using the uri given by Intent data
    InputStream input = getContentResolver().openInputStream(contactUri)
    Bitmap bmp = null;
    if (input != null) {
        bmp = BitmapFactory.decodeStream(input);
        input.close();
        BitmapDrawable drawable = new BitmapDrawable(getResources(), bmp);
        contactPhoto.setBackgroundDrawable(drawable);
    } else {
        // clear any existing background here if needed
    }
    

    【讨论】:

    • 不,不起作用。另外,顺便说一句,不推荐使用 setBackgroundDrawable。
    猜你喜欢
    • 2011-09-23
    • 2021-02-07
    • 1970-01-01
    • 2013-01-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-19
    相关资源
    最近更新 更多