android - 获取原始联系人缩略图

标签 android contacts contactscontract

我正在尝试检索原始联系人的照片。我可以成功获取给定原始联系人的高分辨率照片,但是当我想获取同一原始联系人的缩略图照片时,出现此异常:

02-17 05:43:44.695: E/DatabaseUtils(4071): Writing exception to parcel
02-17 05:43:44.695: E/DatabaseUtils(4071): java.lang.IllegalArgumentException: URI: content://com.android.contacts/raw_contacts/8/photo, calling user: com.pedro.notesquirrel, calling package:com.pedro.notesquirrel
02-17 05:43:44.695: E/DatabaseUtils(4071):  at com.android.providers.contacts.LegacyApiSupport.query(LegacyApiSupport.java:1914)
02-17 05:43:44.695: E/DatabaseUtils(4071):  at com.android.providers.contacts.ContactsProvider2.queryLocal(ContactsProvider2.java:6378)
02-17 05:43:44.695: E/DatabaseUtils(4071):  at com.android.providers.contacts.ContactsProvider2.query(ContactsProvider2.java:4999)
02-17 05:43:44.695: E/DatabaseUtils(4071):  at android.content.ContentProvider$Transport.query(ContentProvider.java:200)
02-17 05:43:44.695: E/DatabaseUtils(4071):  at android.content.ContentProviderNative.onTransact(ContentProviderNative.java:112)
02-17 05:43:44.695: E/DatabaseUtils(4071):  at android.os.Binder.execTransact(Binder.java:404)
02-17 05:43:44.695: E/DatabaseUtils(4071):  at dalvik.system.NativeStart.run(Native Method)

我正在使用 RESTlet 框架,但我认为它与这个问题没有任何关系。

这是我的代码:

此处 mHighResolution 是一个 Boolean,当它为 false 时,它会生成该异常。当它为真时,它会显示照片。 所以,

mHighResolution == false -> exception
mHighResolution == true -> works fine

public InputStream getPhotoInputStream() {
        Uri uri = Uri.withAppendedPath(ContactsContract.RawContacts.CONTENT_URI, String.valueOf(mRawContactId));
        return ContactsContract.Contacts.openContactPhotoInputStream(contentResolver, uri, mHighResolution);
}

    @Override
    public void handle(Request request, Response response) {

        String type = request.getMethod().getName();
        String uid = (String) request.getAttributes().get("uid");

        if(type.equalsIgnoreCase("get"))
        {
            try {
                Representation r = processGet(uid);
                response.setEntity(r);
            } catch (NotFoundException e) {
                Log.e(TAG, e.getMessage(), e);
                response.setStatus(new Status(Status.CLIENT_ERROR_NOT_FOUND, e.getMessage()));
            } catch (IOException e) {
                Log.e(TAG, e.getMessage(), e);
                response.setStatus(new Status(Status.SERVER_ERROR_INTERNAL, e.getMessage()));           
            } 
        }

private Representation processGet(String uid) throws NotFoundException, IOException
{
    Photo photo = new Photo(mContext, uid);
    Representation representation = new InputRepresentation(photo.getPhotoInputStream());

    return representation;
}

最佳答案

缩略图保存在 Contacts 数据库的 Data 表中。

Android 将照片保存在图像文件(高分辨率)或数据库中作为 blob

要访问图像文件,您可以使用我在问题中使用的方法。要访问数据库中的缩略图,您可以使用以下代码:

public InputStream getPhotoThumbnailInputStream(String uid)
{
    final String[] projection = new String[]{Data.DATA15};
    final String selection = Data.RAW_CONTACT_ID + "=? AND " + Data.MIMETYPE + " =?";
    final String[] selectionArgs = new String[]{uid, android.provider.ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE};

    final Cursor cursor = contentResolver.query(Data.CONTENT_URI, projection, selection, selectionArgs, null);

    if(cursor.moveToFirst())
    {
        byte[] photo = cursor.getBlob(0);
        InputStream is = new ByteArrayInputStream(photo);
        cursor.close();
        return is;
    }
    else {
        Log.e(TAG, "Photo thumbnail not found for the given raw contact id.");
        cursor.close();
        return null;
    }
}

关于android - 获取原始联系人缩略图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21834791/

相关文章:

php - 尝试在 WordPress 中使用 php 创建联系表单

java - 如何从android中的联系人读取电子邮件字段?

安卓网络连接代码

触发获取请求时出现 java.lang.IllegalArgumentException : Host name may not be null,

javascript - Google Contact API 错误 : Request via script load timed out. 可能原因:Feed URL 不正确;提要需要身份验证

php - 导入联系人

android - 如何在任何 Android 手机中查找和收集给定联系人中的所有可用信息?

android - 无法从联系人中检索电子邮件

java - 检查两个 View 之间 Activity 的碰撞检测

android - 在开始新 Activity 之前显示进度对话框