Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/228.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android Oreo DocumentsContract.getDocumentId(uri)返回路径而不是长路径_Android_File_Path_Filesystems_Android 8.1 Oreo - Fatal编程技术网

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

Android Oreo DocumentsContract.getDocumentId(uri)返回路径而不是长路径,android,file,path,filesystems,android-8.1-oreo,Android,File,Path,Filesystems,Android 8.1 Oreo,我正在尝试获取存储在Android文件系统中的文件的真实路径(我正在使用Android8.1在模拟器上进行测试) 这是我的密码: final String id = DocumentsContract.getDocumentId(uri); final Uri contentUri = ContentUris.withAppendedId(Uri.parse("content://downloads/public_downloads"), Long.valueOf(id)); return ge

我正在尝试获取存储在Android文件系统中的文件的真实路径(我正在使用
Android8.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);
对于早期版本的
Android 8.0
,变量
id
包含一个
long
值,因此下一行按预期工作

在安卓8上,变量
id
包含如下路径
raw:/storage/simulated/0/Download/my_file.pdf
,因此转换
Long.valueOf(id))
会引发
'java.lang.NumberFormatException'异常。


有什么想法吗?谢谢。

通过执行以下操作解决了相同的问题

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;
            }
      }

在注释中找到了解决方案

请在转换为long之前尝试添加此行:
str=str.replaceAll(“[^\\d.]”,“”)raw:/storage/emulated/0/Download/my_file.pdf
的内容,则路径是
/storage/emulated/0/Download/my_file.pdf
。你在找什么?这对我来说很有意义。在
Android 8
之前,变量
id
包含一个
Long
值,因此我可以使用有效路径准备
contentUri
。从
Android 8
,变量
id
包含在
Android
文件系统中找不到的无效路径。无法获取仅用于视频的图像的路径,我在获取“光标”时收到请求“标题查询不支持投影、选择或排序”我使用的是Android 9。这段代码捕获了异常,但之后没有任何有意义的操作。这是如何解决问题的?不幸的是,通过读取流来创建临时文件看起来更适用,看看这个