FileProvider:- Setting up file sharing
我知道更改 android nougat 中的文件策略。
文件共享到其他应用程序的所需应用程序通过 FileProvider 生成 uri。
uri 格式为 content://com.example.myapp.fileprovider/myimages/default_image.jpg
。
我想知道如何从 FileProvider.getUriForFile()
生成的 uri 中获取文件路径。
因为我的应用程序需要知道物理文件路径才能保存、加载、读取信息等。
有可能吗
[简而言之]
- 我的应用程序收到了其他应用程序在 andorid 7 nougat 上的 intent uri。
- uri 格式为
content://com.example.myapp.fileprovider/myimages/default_image.jpg
- 可能是由
FileProvider.getUriForFile
生成的。 - 我想知道从 uri 获取文件路径的方法。
- 我可以从
getContentResolver().openFileDescriptor()
中获取 mime 类型、显示名称、文件大小和读取 binay。 但我想知道文件路径。
最佳答案
您无法获得 Uri 的“文件路径”,原因很简单,因为不要求 Uri 指向文件。使用 ContentResolver和 openInputStream() 之类的方法来访问 Uri 表示的内容。
To share a file with another app using a content URI, your app has to generate the content URI. To generate the content URI, create a new File for the file, then pass the File to getUriForFile(). You can send the content URI returned by getUriForFile() to another app in an Intent. The client app that receives the content URI can open the file and access its contents by calling ContentResolver.openFileDescriptor to get a ParcelFileDescriptor. Source: Android developers
关于Android 7 nougat,如何从传入 Intent 的uri获取文件路径?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43178911/