如何从相机拍照建立的Fileprovider中获取绝对路径
我们通过高版本获取的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中获取绝对路径的更多相关文章
- Java中获取文件路径
Java中获取文件路径 1.实例说明 (1)得到 ClassPath的绝对URI路径 Thread.currentThread().getContextClassLoader().getResourc ...
- Android中获取文件路径的方法总结及对照
最近在写文件存贮,Android中获取文件路径的方法比较多,所以自己也很混乱.找了好几篇博客,发现了以下的路径归纳,记录一下,以备不时之需 Environment.getDataDirectory() ...
- java web项目中 获取resource路径下的文件路径
public GetResource{ String path = GetResource.class.getClassLoader().getResource("xx/xx.txt&quo ...
- java代码中获取classpath路径
Javaweb工程中,有时候需要自己手动的去读取classpath下面的配置文件,这里总结一点读取classpath路径的方法,分享一下. 方法一: String path = Test.class. ...
- web项目中获取各种路径的方法
~Apple web项目中各种路径的获取 1.可以在servlet的init方法里 String path = getServletContext().getRealPath("/&qu ...
- Net中获取程序集路径
从内存中加载的程序集,无路径 IIS中路径 protected void Page_Load(object sender, EventArgs e) { Response.Write(&quo ...
- JAVA类中获取项目路径
在java web项目中获取项目的src/main/resource下的文件路径 当前类名.class.getClassLoader().getResource("/").getP ...
- 【转】c#.net各种应用程序中获取文件路径的方法
控制台应用程序:Environment.CurrentDirectory.Directory.GetCurrentDirectory() windows服务:Environment.CurrentDi ...
- java中获取文件路径的几种方式
http://xyzroundo.iteye.com/blog/1116159关于绝对路径和相对路径: 绝对路径就是你的主页上的文件或目录在硬盘上真正的路径,(URL和物理路径)例如:C:xyz es ...
- Django中获取参数(路径,查询,请求头,请求体)
一.通常HTTP协议向服务器传参有几种途径 : 提取URL的特定部分,如/weather/shanghai/2018,可以在服务器端的路由中用正则表达式截取: 查询字符串(query string), ...
随机推荐
- JavaScript之void
void是什么 void是JavaScript重要的关键字,该操作符指定要计算一个表达式但不返回. 语法格式: void func() javascript:void func() 或 void (f ...
- nvm安装和管理nodejs
一.NVM简介 NVM 全称 Node Version Manager,是一个管理 NodeJS 版本的工具. NVM 默认只支持 Linux 和 OS X,不支持 Windows windows使用 ...
- Qt5 CMake项目简单模板
cmake_minimum_required(VERSION 3.5) project(test VERSION 0.1 LANGUAGES CXX) set(CMAKE_INCLUDE_CURREN ...
- Python elasticsearch 使用心得
一.配置 python == 3.6/3.8 # 更高版本的elasticsearch会出现doc_type被统一成_doc导致旧版语句报错的情况 pip install elasticsearch= ...
- CMT: Convolutional Neural Networks Meet Vision Transformers概述
0.前言 相关资料: arxiv github 论文解读(CSDN,CSDN) 论文基本信息: 作者单位:华为诺亚, 悉尼大学 发表时间:CVPR2022(2021.7.13) 1.针对的问题 当前将 ...
- 524. 通过删除字母匹配到字典里最长单词 (Medium)
问题描述 524. 通过删除字母匹配到字典里最长单词 (Medium) 给你一个字符串 s 和一个字符串数组 dictionary ,找出并返回 dictionary 中最长的字符串,该字符串可以通过 ...
- 两张表合并到一个VO里面
@Overridepublic List<TbRemouldAirELe> findAll() { List<TbRemouldAirELe> list = new Array ...
- 《黑马旅游网》综合案例六 BaseServlet 抽取
BaseServlet抽取 减少Servlet的数量,现在是一个功能一个Servlet,将其优化为一个模块一个Servlet, 相当于在数据库中一张表对应一个Servlet,在Servlet中提供不同 ...
- Windos下 java后台软件服务化(举例)-WinSW
WinSW-软件服务化 1.1 举例:ApiWintool可执行jar ApiWintool.exe ApiWintool.jar ApiWintool.xml install.cmd uninsta ...
- element ui form 表单 校验upload是否有上传
查到资料 可以绑定在一个多选上,校验此绑定的值 1 <el-form-item label="上传图片" prop="双向绑定值"> 2 <e ...