/**
* 反射从 provider uri中获取 文件绝对路径
* @param context
* @param uri
* @return
*/
private static String getProviderUriPath(Context context, Uri uri){
String filePath = "";
List<PackageInfo> packs = context.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, context, 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;
}

从网上一位大神处整理而来,在此做笔记

Uri转绝对路径工具类的更多相关文章

  1. 路径工具类NSPathUtilities

    路径工具类NSPathUtilities.h 路径类NSPathUtilities.h包含了 NSString的函数和分类扩展,他允许你操作路径名.应该竟可能的使用这些函数,以便使程序更独立于文件系统 ...

  2. 获取当前应用的系统路径工具类和java的System.getProperty()方法介绍

    java的System.getProperty()方法可以获取的值,如下: 对于Java程序,无论是未打包的还是打包的JAR或WAR文件,有时候都需要获取它运行所在目录信息,如何做到这一点呢? /** ...

  3. Java 获取webapp,Root,classpath,项目等路径工具类

    public class UtilPath { public static void main(String[] args) { String systemName = System.getPrope ...

  4. javascript 绝对路径工具类

    // #region 取虚拟目录示例代码 //获取网站虚拟目录名称 function GetVirtualDirectoryName() { var pathname = removeFirstSla ...

  5. JDK中工具类的使用

    JDK中内置了很多常用的工具类,且多以“s”结尾,如:集合工具类Collections,数组工具类Arrays,对象工具类Objects,文件工具类Files,路径工具类Paths,数学工具类Math ...

  6. Android4.4以上Uri转换成绝对路径的工具类

    一.Android4.4版本以上Uri地址封装规范: content://com.android.providers.media.documents/document/image%3A659 二.An ...

  7. Android 常用工具类之SPUtil,可以修改默认sp文件的路径

    参考: 1. 利用Java反射机制改变SharedPreferences存储路径    Singleton1900 2. Android快速开发系列 10个常用工具类 Hongyang import ...

  8. Java中windows路径转换成linux路径等工具类

    项目中发现别人写好的操作系统相关的工具类: 我总结的类似相关博客:http://www.cnblogs.com/DreamDrive/p/4289860.html import java.net.In ...

  9. 【JDBC】学习路径5-提取JDBCUtils工具类

    回顾我们上面几节的内容,我们发现重复代码非常多,比如注册驱动.连接.关闭close()等代码,非常繁杂. 于是我们将这些重复的大段代码进行包装.提取成JDBCUtils工具类. 第一章:提取注册连接模 ...

  10. java工具类

    1.HttpUtilsHttp网络工具类,主要包括httpGet.httpPost以及http参数相关方法,以httpGet为例:static HttpResponse httpGet(HttpReq ...

随机推荐

  1. redis(14)主从复制

    Redis主从复制 主机数据更新后根据配置和策略, 自动同步到备机的 master/slaver 机制,Master 以写为主,Slave 以读为主,主从复制节点间数据是全量的. 作用: 读写分离,性 ...

  2. 马哥教育第一周作业N67044-张铭扬

    1. 图文并茂解释开源许可证 GPL.BSD.MIT.Mozilla.Apache和LGPL的区别? 1)MIT许可证:MIT是六种开源许可证中最自由宽容,它允许使用者自由修改后无需放置版权说明并且可 ...

  3. Django,Flask中的request

    request的结构获取 class Upload(Resource): def post(self): print(curPath) print(request.files['file'].__di ...

  4. 统计tomcat的access日志

    统计tomcat的access日志的ip次数,并按次数排序 awk '{sum[$1]++}END{for(i in sum) print i "\t" sum[i]}' loca ...

  5. java运算符相关学习

    java运算符 面试题1: 计算2*8如何操作效率更高? 剖析: 2 * 8 => 实际上是2 * 2 * 2 * 2 2<<3 System.out.println(2<&l ...

  6. VS2010 发布网站总是连同cs文件一起发布了

    选择第一个,保存再发布.cs文件 都删除了.

  7. 发誓要学会wepack

    9.9 学习资料: http://webpack.wuhaolin.cn/   <深入浅出Webpack> https://github.com/zshno1/wepack-study w ...

  8. 重启nginx 报错 nginx.pid

    先检查 nginx -c /etc/nginx/nginx.conf 看看端口是否被占用 若占用杀掉 ps -ef | grep nginx kill --- 然后再用nginx -c /etc/ng ...

  9. IDEA Download missing driver files 下载失败解决方法

    使用IDEA连接数据库时可能会遇到缺失驱动文件的问题(提示Download missing driver files) 正常情况下点击Download后IDEA会自动下载缺失的文件,但是因为网络原因, ...

  10. 升级grafana

    We recommend everyone to upgrade Grafana often to stay up to date with the latest fixes and enhancem ...