Oreo DocumentsContract.getDocumentId(uri)返回路径而不是Long

Ale*_*Ale 17 filesystems android file path android-8.1-oreo

我正在尝试获取存储在Android文件系统中的文件的真实路径(我正在使用模拟器进行测试Android 8.1)

这是我的代码:

final String id = DocumentsContract.getDocumentId(uri);
final Uri contentUri = ContentUris.withAppendedId(Uri.parse("content://downloads/public_downloads"), Long.valueOf(id));
return getDataColumn(context, contentUri, null, null);
Run Code Online (Sandbox Code Playgroud)

对于早期版本Android 8.0,变量id包含一个long值,因此下一行按预期工作.

Android 8变量中id包含这样的路径raw:/storage/emulated/0/Download/my_file.pdf,所以投射Long.valueOf(id))抛出一个'java.lang.NumberFormatException' Exception.

有任何想法吗?谢谢.

小智 16

如果通过以下方式解决了同样的问题.

final String id = DocumentsContract.getDocumentId(uri);
if (!TextUtils.isEmpty(id)) {
            if (id.startsWith("raw:")) {
                return id.replaceFirst("raw:", "");
            }
            try {
                final Uri contentUri = ContentUris.withAppendedId(
                        Uri.parse("content://downloads/public_downloads"), Long.valueOf(id));
                return getDataColumn(context, contentUri, null, null);
            } catch (NumberFormatException e) {
                 return null;
            }
      }
Run Code Online (Sandbox Code Playgroud)

解决方案在评论https://github.com/Yalantis/uCrop/issues/318中找到

  • 无法获取仅适用于视频的图像路径,我在获取“光标”时收到请求“标题查询不支持投影、选择或排序”我使用的是 Android 9 (2认同)