Android调用系统安装程序打开本地文件(包括 Android7.0以上)

版权声明: https://blog.csdn.net/luoyayun361/article/details/82746576

前言

在 Android 手机文件管理中,点击某个文件的时候,会弹出选择打开文件的方式,那么,如果在我们自己的软件中要实现这种效果该怎么做呢?其实 Android 打开本地文件很常见,打开的时候会根据不同的文件类型来执行,所以需要先判断文件的MIME 类型,在网上查了很多资料,实现了自己想要的效果,这里做个总结。

正文

首先来看看以下代码,原文地址在这里,我在此技术上多添加了一些文件类型。代码如下:

private static final String[][] MIME_MapTable={
            //{后缀名,    MIME类型}
            {".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/msword"},
            {".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"},
            {".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.ms-powerpoint"},
            {".prop",    "text/plain"},
            {".rar",    "application/x-rar-compressed"},
            {".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/xml"},
            {".xml",    "text/plain"},
            {".z",        "application/x-compress"},
            {".zip",    "application/zip"},
            {"",        "*/*"}
    };

    private String getMIMEType(File file) {

        String type="*/*";
        String fName = file.getName();
        //获取后缀名前的分隔符"."在fName中的位置。
        int dotIndex = fName.lastIndexOf(".");
        if(dotIndex < 0)
            return type;
        /* 获取文件的后缀名 */
        String fileType = fName.substring(dotIndex,fName.length()).toLowerCase();
        if(fileType == null || "".equals(fileType))
            return type;
        //在MIME和文件类型的匹配表中找到对应的MIME类型。
        for(int i=0;i<MIME_MapTable.length;i++){
            if(fileType.equals(MIME_MapTable[i][0]))
                type = MIME_MapTable[i][1];
        }
        return type;
    }

上面两个方法是用于判断文件类型的。那么接下来看看如何调用并打开文件:

public static void openAndroidFile(String filepath){
        Intent intent = new Intent();
        File file = new File(filepath);
//        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);//设置标记
        intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
        intent.setAction(Intent.ACTION_VIEW);//动作,查看
        intent.setDataAndType(Uri.fromFile(file), m_instance.getMIMEType(file));//设置类型
        m_instance.startActivity(intent);
    }

OK,通过上面的方式确实能调用本地软件来打开文件,但是只能在 android6.0以下的系统上使用,在 Android7.0以上系统就不管用了,原因是 Android7.0开始,系统禁止不安全路径被外部访问,所以增加了文件的权限检查。那么怎么解决这个问题呢,比较正常的方式就是在去申请文件权限,整体操作比较麻烦,可以参考这篇文章,而还可以通过另外一种比较暴力的方式来解决该问题,那就是直接绕开系统检查文件的权限,具体操作如下,将打开文件的地方加上两句话,如下:

 public static void openAndroidFile(String filepath){
        Intent intent = new Intent();
        // 这是比较流氓的方法,绕过7.0的文件权限检查
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
            StrictMode.setVmPolicy(builder.build());
        }

        File file = new File(filepath);
//        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);//设置标记
        intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
        intent.setAction(Intent.ACTION_VIEW);//动作,查看
        intent.setDataAndType(Uri.fromFile(file), m_instance.getMIMEType(file));//设置类型
        m_instance.startActivity(intent);
    }

这里先判断系统版本,是否为 android7.0以上,然后再添加相应语句。这种方式最简单直接,编译后能立马正常打开文件。当然,这是种走捷径的方案,随着 android 版本的升级,不知道今后会不会不管用,毕竟牵扯系统安全问题,要不然 android 做那么多限制就没啥意义了。所以如果说项目不是那么急的话 可以用正常的方式来解决在 android7.0以上的设备打开文件问题。

参考文章:https://www.cnblogs.com/fwling/p/7239983.html

猜你喜欢

转载自blog.csdn.net/luoyayun361/article/details/82746576