我们通过高版本获取的fileprovider,然后拍了个照片,如下

 imageUri = FileProvider.getUriForFile

但是我们发现当我们

            File file = new File(imgeUri);

这样建立文件时,就会报fileNotfound的io异常

为什么呢,因为我们发现imageUri是给我们返回的是content:/开头的这样的路径,显然是无法直接访问的

下面通过这个方法访问

    private String getProviderUriPath(Uri uri){
String filePath = "";
List<PackageInfo> packs = this.getPackageManager().getInstalledPackages(PackageManager.GET_PROVIDERS);
if(packs !=null){
String fileProviderClassName = FileProvider.class.getName();
for (PackageInfo pack : packs) {
ProviderInfo[] providers = pack.providers;
if (providers != null) {
for (ProviderInfo provider : providers) { if (uri.getAuthority().equals(provider.authority)){
if (provider.name.equalsIgnoreCase(fileProviderClassName)) {
Class<FileProvider> fileProviderClass = FileProvider.class;
try {
Method getPathStrategy = fileProviderClass.getDeclaredMethod("getPathStrategy", Context.class , String.class);
getPathStrategy.setAccessible(true);
Object invoke = getPathStrategy.invoke(null, this, uri.getAuthority());
if (invoke != null) {
String PathStrategyStringClass = FileProvider.class.getName()+"$PathStrategy";
Class<?> PathStrategy = Class.forName(PathStrategyStringClass);
Method getFileForUri = PathStrategy.getDeclaredMethod("getFileForUri", Uri.class);
getFileForUri.setAccessible(true);
Object invoke1 = getFileForUri.invoke(invoke, uri);
if (invoke1 instanceof File) {
filePath = ((File) invoke1).getAbsolutePath(); }
} } catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} break;
}
// Log.e(provider);
break;
}
} } } }
return filePath;
}

希望能帮到大家

原博主 文章地址 @https://blog.csdn.net/zswqaxcde/article/details/79205526

如何从相机拍照建立的Fileprovider中获取绝对路径的更多相关文章

  1. Java中获取文件路径

    Java中获取文件路径 1.实例说明 (1)得到 ClassPath的绝对URI路径 Thread.currentThread().getContextClassLoader().getResourc ...

  2. Android中获取文件路径的方法总结及对照

    最近在写文件存贮,Android中获取文件路径的方法比较多,所以自己也很混乱.找了好几篇博客,发现了以下的路径归纳,记录一下,以备不时之需 Environment.getDataDirectory() ...

  3. java web项目中 获取resource路径下的文件路径

    public GetResource{ String path = GetResource.class.getClassLoader().getResource("xx/xx.txt&quo ...

  4. java代码中获取classpath路径

    Javaweb工程中,有时候需要自己手动的去读取classpath下面的配置文件,这里总结一点读取classpath路径的方法,分享一下. 方法一: String path = Test.class. ...

  5. web项目中获取各种路径的方法

    ~Apple   web项目中各种路径的获取 1.可以在servlet的init方法里 String path = getServletContext().getRealPath("/&qu ...

  6. Net中获取程序集路径

      从内存中加载的程序集,无路径   IIS中路径 protected void Page_Load(object sender, EventArgs e) { Response.Write(&quo ...

  7. JAVA类中获取项目路径

    在java web项目中获取项目的src/main/resource下的文件路径 当前类名.class.getClassLoader().getResource("/").getP ...

  8. 【转】c#.net各种应用程序中获取文件路径的方法

    控制台应用程序:Environment.CurrentDirectory.Directory.GetCurrentDirectory() windows服务:Environment.CurrentDi ...

  9. java中获取文件路径的几种方式

    http://xyzroundo.iteye.com/blog/1116159关于绝对路径和相对路径: 绝对路径就是你的主页上的文件或目录在硬盘上真正的路径,(URL和物理路径)例如:C:xyz es ...

  10. Django中获取参数(路径,查询,请求头,请求体)

    一.通常HTTP协议向服务器传参有几种途径 : 提取URL的特定部分,如/weather/shanghai/2018,可以在服务器端的路由中用正则表达式截取: 查询字符串(query string), ...

随机推荐

  1. Linux常用命令 转于WoW_Boom

    一.常用命令 1.进入到用户根目录cd ~ 或 cd 2.查看当前所在目录pwd 3.进入到itcast用户根目录cd ~itcast 4.返回到原来目录cd - 5.返回到上一级目录cd .. 6. ...

  2. [Unity]射线的简单应用和对UGUI的检测

    最近做的小游戏,需要通过触屏来控制移动,主要做法就是在Update中检测Input.TouchCount,但是问题是会盖住UGUI的Button事件,第一时间想到射线检测 常用射线 Unity有射线类 ...

  3. Android图表控件MPAndroidChart——BarChart实现多列柱状图和LineChart多曲线 (完结)

    首先才接触Android,目前自学一个月,花了一星期,做出了柱状图和曲线图,踩过坑也不少,上代码(主要提供思路,大部分代码可直接用). 参考代码地址:①曲线:https://blog.csdn.net ...

  4. 将bat文件设置为开机启动

    设置该bat文件开机自启动 1.右键计算机–>管理,选择系统工具–>计划任务程序: 2.选择创建任务,在弹出窗口中,输入任务名称,并选择不管用户是否登录都要运行–>使用最高权限运行: ...

  5. Unity打包xcode修改工程配置代码

    1 using System.IO; 2 using UnityEngine; 3 using UnityEditor; 4 using UnityEditor.iOS.Xcode; 5 using ...

  6. MavenDependencies 报错缺少jar包

    1.首先看你的工程是否是maven项目,如果不是,则先转换为maven项目. 如果已经是maven项目了,maven的依赖库无法添加,则移步2: 2.项目右键,选择maven,然后子菜单,选择&quo ...

  7. pure-ftpd

    1.安装服务 yum install epel-release yum -y install pure-ftpd 2.启动服务如果报错,看一下是否是缺库文件导致. locate libpq.so.5 ...

  8. PYTHON中的CONCURRENT.FUTURES模块

    一 : 概述 concurrent.futures模块提供了高度封装的异步调用接口 ThreadPoolExecutor:线程池,提供异步调用 ProcessPoolExecutor: 进程池,提供异 ...

  9. CCF 201909-1 小明种苹果

    #include <iostream> #include <bits/stdc++.h> #include <string> using namespace std ...

  10. pip国内镜像永久

    一.Windows下永久设置pip镜像使用:# windows系统使用cmd(最好使用管理员权限运行)快速设置 pip install pip -U # 升级pip到最新版本 pip config s ...