android - 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
解决方案
推荐阅读
- javascript - 给定一个值为数组的对象,返回对 [key, array.length] 的最有效方法是什么?
- php - PHP 邮件在邮件中显示密件抄送地址
- arduino-uno - Arduino mills() 溢出
- java - 字符串如何相互比较?
- linux - sudoers:不能运行 2 个命令列表,其中第二个具有 glob * 并拒绝第一个
- node.js - 您如何在预中间件中实现 mongo 聚合管道?
- c# - Google 文字转语音延迟
- android - 从内部侦听器中删除 Firestore 快照侦听器
- excel - 如何在不定义行号的情况下生成 SUM(A+B)?
- android - Android MVVM:从广播接收器观察数据库变化