質問

I'm trying to retrieve the raw contact's photo. I can successfully get the high resolution photo for a given raw contact but when I want to get the thumbnail photo for the same raw contact I get this exception:

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)

I'm using RESTlet framework, but I don't think it has anything to do with this problem.

Here's my code:

Here mHighResolution is a Boolean and when it is false it generates that exception. When it is true it displays the photo. So,

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;
}
役に立ちましたか?

解決

The thumbnail photo is saved in the Data table from the Contacts database.

Android saves the photos either in an image file (the high resolution) or in the database as a blob

To access the image file you can use the method I'm using in the question. To access the thumbnail image in the database you can use this code:

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;
    }
}
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top