Uri解析

黑金白土 2017-05-08 01:49:35
从第三方拿到的uri为:content://com.alivecor.aliveecg.fileprovider/temp_ecg_reports/ecg-20170421-150341.pdf。如何获得文件的绝对路径。根据网上的好多方法拿不到,请各位路过的大神指点。
...全文
233 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
黑金白土 2017-06-02
  • 打赏
  • 举报
回复
问题已经解决了,我的主要意图就是从第三的内容提供者中获取到资源。我采用了如下方法: ParcelFileDescriptor mInputPFD; try { mInputPFD = context.getContentResolver().openFileDescriptor(uri, "r"); } catch (FileNotFoundException e) { e.printStackTrace(); Log.e("MainActivity", "File not found."); return; } // Get a regular file descriptor for the file FileDescriptor fd = mInputPFD.getFileDescriptor(); // Read the file FileInputStream in=new FileInputStream(fd); try { FileOutputStream out = new FileOutputStream(file); try { IOUtils.copy(in, out); Log.e("TAG", "IO复制文件!!"); } finally { out.close(); } } finally { in.close(); } String absolutePath = file.getAbsolutePath();
黑金白土 2017-05-08
  • 打赏
  • 举报
回复
引用 6 楼 ink_s 的回复:
<provider android:name="android.support.v4.content.FileProvider" android:authorities="com.ckt.fileproviderservice" android:exported="false" android:grantUriPermissions="true"> <meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/filepath" /> </provider> android:name --- 定义名子 android:authorities --定义authority android:exported="false" 对其它应用不可用 android:grantUriPermissions="true" 既然对其它应用不可用,只能授予content uri临时权限 <meta-data>中的 android:name="android.support.FILE_PROVIDER_PATHS" (这个名子是固定的)和 android:resource 指向一个xml文件,这个xml文件定义了要共享文件的路径 xml/filepath.xml <paths> <files-path name="my_images" path="image/" /> <external-path name="my_external" /> <cache-path name="my_cache" /> </paths> <files-path> 指向的是内部存储要共享的目录 <external-path> 指向外部存储要共享的目录 <cache-path> 指向缓存要共享的目录 其中name为我们自己定义 的名子,path为具体的路径 如果请求一个default_image.jpg的 content uri,FileProvider返回的content URI是这样的 content://com.ckt.fileproviderservice/my_images/default_image.jpg 你确定提供路径的APP android:exported="false" 对其它应用不可用 是否设置了其他程序可用? 你的应用有获取权限? http://blog.csdn.net/zwlove5280/article/details/50509224
引用 6 楼 ink_s 的回复:
<provider android:name="android.support.v4.content.FileProvider" android:authorities="com.ckt.fileproviderservice" android:exported="false" android:grantUriPermissions="true"> <meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/filepath" /> </provider> android:name --- 定义名子 android:authorities --定义authority android:exported="false" 对其它应用不可用 android:grantUriPermissions="true" 既然对其它应用不可用,只能授予content uri临时权限 <meta-data>中的 android:name="android.support.FILE_PROVIDER_PATHS" (这个名子是固定的)和 android:resource 指向一个xml文件,这个xml文件定义了要共享文件的路径 xml/filepath.xml <paths> <files-path name="my_images" path="image/" /> <external-path name="my_external" /> <cache-path name="my_cache" /> </paths> <files-path> 指向的是内部存储要共享的目录 <external-path> 指向外部存储要共享的目录 <cache-path> 指向缓存要共享的目录 其中name为我们自己定义 的名子,path为具体的路径 如果请求一个default_image.jpg的 content uri,FileProvider返回的content URI是这样的 content://com.ckt.fileproviderservice/my_images/default_image.jpg 你确定提供路径的APP android:exported="false" 对其它应用不可用 是否设置了其他程序可用? 你的应用有获取权限? http://blog.csdn.net/zwlove5280/article/details/50509224
我从第三方的意图中可以得到uri,我如何从uri中获得文件的路径或者获得这个文件
ink_s 2017-05-08
  • 打赏
  • 举报
回复
<provider android:name="android.support.v4.content.FileProvider" android:authorities="com.ckt.fileproviderservice" android:exported="false" android:grantUriPermissions="true"> <meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/filepath" /> </provider> android:name --- 定义名子 android:authorities --定义authority android:exported="false" 对其它应用不可用 android:grantUriPermissions="true" 既然对其它应用不可用,只能授予content uri临时权限 <meta-data>中的 android:name="android.support.FILE_PROVIDER_PATHS" (这个名子是固定的)和 android:resource 指向一个xml文件,这个xml文件定义了要共享文件的路径 xml/filepath.xml <paths> <files-path name="my_images" path="image/" /> <external-path name="my_external" /> <cache-path name="my_cache" /> </paths> <files-path> 指向的是内部存储要共享的目录 <external-path> 指向外部存储要共享的目录 <cache-path> 指向缓存要共享的目录 其中name为我们自己定义 的名子,path为具体的路径 如果请求一个default_image.jpg的 content uri,FileProvider返回的content URI是这样的 content://com.ckt.fileproviderservice/my_images/default_image.jpg 你确定提供路径的APP android:exported="false" 对其它应用不可用 是否设置了其他程序可用? 你的应用有获取权限? http://blog.csdn.net/zwlove5280/article/details/50509224
黑金白土 2017-05-08
  • 打赏
  • 举报
回复
引用 3 楼 ink_s 的回复:
引用 2 楼 ink_s 的回复:
http://blog.csdn.net/dj0379/article/details/50765021
哎呀,弄错了。我想打开这个网页看看的,结果给发回复了,好尴尬。。。
不是绝对路径啊,前面的是它的authority,/temp_ecg_reports/ecg-20170421-150341.pdf这部分是path。不知道这个path是不是就是文件的相对路径啊?
ink_s 2017-05-08
  • 打赏
  • 举报
回复
看你这个URI content://com.alivecor.aliveecg.fileprovider/temp_ecg_reports/ecg-20170421-150341.pdf 其实就是绝对路劲了吧 你看看手机andoid/data文件夹下有没有com.alivecor.aliveecg.fileprovider/temp_ecg_reports/ecg-20170421-150341.pdf这个文件或文件夹。 应该是要用注册ContentResolver 来获取吧。并且Content Provider 要说明了他公开了。
ink_s 2017-05-08
  • 打赏
  • 举报
回复
引用 2 楼 ink_s 的回复:
http://blog.csdn.net/dj0379/article/details/50765021
哎呀,弄错了。我想打开这个网页看看的,结果给发回复了,好尴尬。。。
ink_s 2017-05-08
  • 打赏
  • 举报
回复
http://blog.csdn.net/dj0379/article/details/50765021
黑金白土 2017-05-08
  • 打赏
  • 举报
回复
参考了http://blog.csdn.net/dj0379/article/details/50765021这篇博文,按照这个进行也没有得到,甚是疑惑!

80,492

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧