我在 android 2.x 和 4.x 之间遇到了一些关于 gettin photo uri 的问题
以下代码在 2.x 上完美运行,但不适用于 4.x
public static Uri getPhotoURIFromAddress(Context activity, String address) {
String contactId = fetchContactIdFromPhoneNumber(address,activity);
//Se non trovo il contatto il rubrica
if(contactId.equals("0")){
return null;
}
ContentResolver contentResolver = activity.getContentResolver();
try {
Cursor cursor = contentResolver
.query(ContactsContract.Data.CONTENT_URI,
null,
ContactsContract.Data.CONTACT_ID
+ "="
+ contactId
+ " AND "
+ ContactsContract.Data.MIMETYPE
+ "='"
+ ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE
+ "'", null, null);
if (cursor != null) {
if (!cursor.moveToFirst()) {
Log.i("No photo","No photo");
return null; // no photo
}
} else {
return null; // error in cursor process
}
cursor.close();
} catch (Exception e) {
e.printStackTrace();
return null;
}
我如何整合 4.x 的工作代码?谢谢