手机
Uri解析
摘要:从第三方拿到的uri为:content://com.alivecor.aliveecg.fileprovider/temp_ecg_repo...

从第三方拿到的uri为:content://com.alivecor.aliveecg.fileprovider/temp_ecg_reports/ecg-20170421-150341.pdf。如何获得文件的绝对路径。根据网上的好多方法拿不到,请各位路过的大神指点。

回复讨论(解决方案)

参考了http://blog.csdn.net/dj0379/article/details/50765021这篇博文,按照这个进行也没有得到,甚是疑惑! http://blog.csdn.net/dj0379/article/details/50765021 http://blog.csdn.net/dj0379/article/details/50765021 哎呀,弄错了。我想打开这个网页看看的,结果给发回复了,好尴尬。。。 看你这个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 要说明了他公开了。

http://blog.csdn.net/dj0379/article/details/50765021 哎呀,弄错了。我想打开这个网页看看的,结果给发回复了,好尴尬。。。

不是绝对路径啊,前面的是它的authority,/temp_ecg_reports/ecg-20170421-150341.pdf这部分是path。不知道这个path是不是就是文件的相对路径啊?         < 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         < 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

        < 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中获得文件的路径或者获得这个文件 问题已经解决了,我的主要意图就是从第三的内容提供者中获取到资源。我采用了如下方法:

                                       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();

【Uri解析】相关文章:

Android Spinner 下拉菜单的使用

Android之PreferenceActivity应用详解(2)

基于Android XML解析与保存的实现

解析android截屏问题

Intel HAXM为Android 模拟器加速解决模拟器运行慢的问题

Android开发之XML文件解析的使用

Android中使用pull解析器操作xml文件的解决办法

android底部菜单栏实现原理与代码

Android开发:14个解决实际问题的代码片段

Android三种菜单实例分析

精品推荐
分类导航