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

例如:


## 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 {

            List 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 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) {

                                                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](https://uploader.shimo.im/f/rLQs2yplYfaX0OpE.java)

你可能感兴趣的:(Android 实现微信,QQ文件在其他应用打开并获取到路径)