如何从android pie中的 content:// 方案中获取文件对象?

How to get a file object from the content:// scheme in android pie?

提问人:RKB 提问时间:7/27/2020 更新时间:8/1/2020 访问量:268

问:

我有这个代码,它将图像文件的路径存储在本地数据库中,就像从运行 Android Pie 的设备内部存储中选择图像时返回的“content://media/external/images/media/25”。

现在,当我尝试从上述内容路径创建 File 对象时,它会抛出 FileNotFoundException。

val imagePath = URL("content://media/external/images/media/25")
val imageFile = File(imagePath)

任何帮助表示赞赏。

Android URI android-9.0-pie

评论

0赞 CommonsWare 7/27/2020
A 不是文件。用于将字符串解析为 a 并将其传递给您喜欢的图像加载库,例如 Glide 或 Picasso。请注意,您可能无权再访问这些内容UriUri.parse()UriUri
0赞 RKB 7/27/2020
我知道这不是一个文件,并且已经使用了 ,但这也导致了 FileNotFoundException。我正在使用 File 对象来获取 ExifInfo,'val exif = ExifInterface( imageFile.absolutePath)',暂时不要加载到 imageview 中。Uri Uri.prase()"external/images/media/25"imageFile = File(Uri.parse(ImagePath))
0赞 CommonsWare 7/27/2020
“我知道 Uri 不是一个文件”——那你为什么要做这样的事情?“我正在使用 File 对象来获取 ExifInfo” -- 使用 和 打开内容。将其传递给构造函数。确保您使用的是 Jetpack 中的 Jetpack,而不是框架 Jetpack,后者在旧设备上存在安全漏洞。imageFile = File(Uri.parse(ImagePath))InputStreamContentResolveropenInputStream()ExifInterfaceExifInterface
0赞 RKB 7/28/2020
谢谢你的建议!我肯定会尝试这个。

答:

0赞 DevKazonovic 8/1/2020 #1

尝试使用 ContentUris

Uri contentUri = ContentUris.withAppendedId(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,id);
File file =new File(contentUri)