首页 > 解决方案 > Phone.PHOTO_URI 到真实路径

问题描述

我正在使用以下代码获取电话联系

Cursor phones = Objects.requireNonNull(getActivity()).getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null,
                    ContactsContract.Contacts._ID + " = ? ", new String[]{id}, ContactsContract.Contacts.DISPLAY_NAME_PRIMARY + " ASC");
if (phones != null && phones.getCount() > 0) {
    while (phones.moveToNext()) {
        String name = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
        String photoUri = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.PHOTO_URI));
    }
}

在这里,我得到content://com.android.contacts/display_photo/17了 photoUri,但我需要真正的路径,我尝试过以下方式。

public static String getPath(Uri contentUri, Context context) {
    String result;
    Cursor cursor = context.getContentResolver().query(contentUri, null, null, null, null);
    if (cursor == null) { // Source is Dropbox or other similar local file path
        result = contentUri.getPath();
    } else {
        cursor.moveToFirst();
        int idx = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA);
        result = cursor.getString(idx);
        cursor.close();
    }
    return result;
}

但它抛出IllegalArgumentException at android.database.DatabaseUtils.readExceptionFromParcel

标签: android

解决方案


推荐阅读