Android 获取原始联系人缩略图照片
我正试图找回原始联系人的照片。我可以成功获取给定原始联系人的高分辨率照片,但当我想要获取同一原始联系人的缩略图照片时,我会遇到以下异常:Android 获取原始联系人缩略图照片,android,contacts,contactscontract,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.cont
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
时,它会生成该异常。如果为真,则显示照片。
所以
缩略照片保存在
联系人
数据库的数据
表中
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;
}
}
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;
}
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;
}
}