java android 读取微信文件,Android 实现微信,QQ文件在其他应用打开并获取到路径

编程知识 更新时间:2023-04-17 17:10:14

例如:

1.想实现其他应用打开出现自己的应用,需要在清单文件中增加配置

注:自己需要什么样的文件格式自己可以配置,其中 minType 代表要打开的文件类型。

常用的类型:

{".3gp", "video/3gpp"},

{".apk", "application/vnd.android.package-archive"},

{".asf", "video/x-ms-asf"},

{".avi", "video/x-msvideo"},

{".bin", "application/octet-stream"},

{".bmp", "image/bmp"},

{".c", "text/plain"},

{".class", "application/octet-stream"},

{".conf", "text/plain"},

{".cpp", "text/plain"},

{".doc", "application/msword"},

{".docx", "application/vnd.openxmlformats-officedocument.wordprocessingml.document"},

{".xls", "application/vnd.ms-excel"},

{".xlsx", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"},

{".exe", "application/octet-stream"},

{".gif", "image/gif"},

{".gtar", "application/x-gtar"},

{".gz", "application/x-gzip"},

{".h", "text/plain"},

{".htm", "text/html"},

{".html", "text/html"},

{".jar", "application/java-archive"},

{".java", "text/plain"},

{".jpeg", "image/jpeg"},

{".jpg", "image/jpeg"},

{".js", "application/x-javascript"},

{".log", "text/plain"},

{".m3u", "audio/x-mpegurl"},

{".m4a", "audio/mp4a-latm"},

{".m4b", "audio/mp4a-latm"},

{".m4p", "audio/mp4a-latm"},

{".m4u", "video/vnd.mpegurl"},

{".m4v", "video/x-m4v"},

{".mov", "video/quicktime"},

{".mp2", "audio/x-mpeg"},

{".mp3", "audio/x-mpeg"},

{".mp4", "video/mp4"},

{".mpc", "application/vnd.mpohun.certificate"},

{".mpe", "video/mpeg"},

{".mpeg", "video/mpeg"},

{".mpg", "video/mpeg"},

{".mpg4", "video/mp4"},

{".mpga", "audio/mpeg"},

{".msg", "application/vnd.ms-outlook"},

{".ogg", "audio/ogg"},

{".pdf", "application/pdf"},

{".png", "image/png"},

{".pps", "application/vnd.ms-powerpoint"},

{".ppt", "application/vnd.ms-powerpoint"},

{".pptx", "application/vnd.openxmlformats-officedocument.presentationml.presentation"},

{".prop", "text/plain"},

{".rc", "text/plain"},

{".rmvb", "audio/x-pn-realaudio"},

{".rtf", "application/rtf"},

{".sh", "text/plain"},

{".tar", "application/x-tar"},

{".tgz", "application/x-compressed"},

{".txt", "text/plain"},

{".wav", "audio/x-wav"},

{".wma", "audio/x-ms-wma"},

{".wmv", "audio/x-ms-wmv"},

{".wps", "application/vnd.ms-works"},

{".xml", "text/plain"},

{".z", "application/x-compress"},

{".zip", "application/x-zip-compressed"},

{"", "*/*"}

2.获取到文件路径

if (intent.ACTION_VIEW.equals(action)) {

Uri uri = intent.getData();

String str = Uri.decode(uri.getEncodedPath());

//获取到真实路径 GetRealPath.getFPUriToPath()

String path="file://" + GetRealPath.getFPUriToPath(this, uri)}

2.1 获取真实路径 GetRealPath.getFPUriToPath()方法:

public class GetRealPath {

public static String getFPUriToPath(Context context, Uri uri) {

try {

Listpacks = 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)) {

ClassfileProviderClass = 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) {

String filePath = ((File) invoke1).getAbsolutePath();

return replace(filePath);

}

}

} catch (NoSuchMethodException e) {

e.printStackTrace();

} catch (InvocationTargetException e) {

e.printStackTrace();

} catch (IllegalAccessException e) {

e.printStackTrace();

} catch (ClassNotFoundException e) {

e.printStackTrace();

}

break;

}

break;

}

}

}

}

}

} catch (Exception e) {

e.printStackTrace();

}

return null;

}

public static String replace(String filePath) {

if (filePath.contains("%")) {

filePath = filePath.replace("%", "%25");

}

if (filePath.contains("#")) {

filePath = filePath.replace("%", "%23");

}

if (filePath.contains("&")) {

filePath = filePath.replace("%", "%26");

}

if (filePath.contains("?")) {

filePath = filePath.replace("%", "%3F");

}

return filePath;

}

}

2.2获取真实路径.java:

GetRealPath.java

更多推荐

java android 读取微信文件,Android 实现微信,QQ文件在其他应用打开并获取到路径

本文发布于:2023-04-14 02:08:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/c6e061c6e4cfb0386bef75929e858eb1.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:文件   路径   android   java   Android

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!

  • 74226文章数
  • 14阅读数
  • 0评论数