admin 管理员组

文章数量: 886993

在大部分情况下,我们的程序中会包含或下载有其他一些不能识别的文件,如PDF,压缩文件等。这时我们就为了优化用户体验,会弹窗提示用户是否使用第三方程序打开文件。就需要使用如下类:

代码拷贝即可使用


/**
 * 文件格式数据
 */
val MIME_MapTable = arrayOf(
    //{后缀名, MIME类型}
    arrayOf(".3gp", "video/3gpp"),
    arrayOf(".apk", "application/vnd.android.package-archive"),
    arrayOf(".asf", "video/x-ms-asf"),
    arrayOf(".avi", "video/x-msvideo"),
    arrayOf(".bin", "application/octet-stream"),
    arrayOf(".bmp", "image/bmp"),
    arrayOf(".c", "text/plain"),
    arrayOf(".class", "application/octet-stream"),
    arrayOf(".conf", "text/plain"),
    arrayOf(".cpp", "text/plain"),
    arrayOf(".doc", "application/msword"),
    arrayOf(
        ".docx",
        "application/vnd.openxmlformats-officedocument.wordprocessingml.document"
    ),
    arrayOf(".xls", "application/vnd.ms-excel"),
    arrayOf(
        ".xlsx",
        "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
    ),
    arrayOf(".exe", "application/octet-stream"),
    arrayOf(".gif", "image/gif"),
    arrayOf(".gtar", "application/x-gtar"),
    arrayOf(".gz", "application/x-gzip"),
    arrayOf(".h", "text/plain"),
    arrayOf(".htm", "text/html"),
    arrayOf(".html", "text/html"),
    arrayOf(".jar", "application/java-archive"),
    arrayOf(".java", "text/plain"),
    arrayOf(".jpeg", "image/jpeg"),
    arrayOf(".jpg", "image/jpeg"),
    arrayOf(".js", "application/x-javascript"),
    arrayOf(".log", "text/plain"),
    arrayOf(".m3u", "audio/x-mpegurl"),
    arrayOf(".m4a", "audio/mp4a-latm"),
    arrayOf(".m4b", "audio/mp4a-latm"),
    arrayOf(".m4p", "audio/mp4a-latm"),
    arrayOf(".m4u", "video/vnd.mpegurl"),
    arrayOf(".m4v", "video/x-m4v"),
    arrayOf(".mov", "video/quicktime"),
    arrayOf(".mp2", "audio/x-mpeg"),
    arrayOf(".mp3", "audio/x-mpeg"),
    arrayOf(".mp4", "video/mp4"),
    arrayOf(".mpc", "application/vnd.mpohun.certificate"),
    arrayOf(".mpe", "video/mpeg"),
    arrayOf(".mpeg", "video/mpeg"),
    arrayOf(".mpg", "video/mpeg"),
    arrayOf(".mpg4", "video/mp4"),
    arrayOf(".mpga", "audio/mpeg"),
    arrayOf(".msg", "application/vnd.ms-outlook"),
    arrayOf(".ogg", "audio/ogg"),
    arrayOf(".pdf", "application/pdf"),
    arrayOf(".png", "image/png"),
    arrayOf(".pps", "application/vnd.ms-powerpoint"),
    arrayOf(".ppt", "application/vnd.ms-powerpoint"),
    arrayOf(
        ".pptx",
        "application/vnd.openxmlformats-officedocument.presentationml.presentation"
    ),
    arrayOf(".prop", "text/plain"),
    arrayOf(".rc", "text/plain"),
    arrayOf(".rmvb", "audio/x-pn-realaudio"),
    arrayOf(".rtf", "application/rtf"),
    arrayOf(".sh", "text/plain"),
    arrayOf(".tar", "application/x-tar"),
    arrayOf(".tgz", "application/x-compressed"),
    arrayOf(".txt", "text/plain"),
    arrayOf(".wav", "audio/x-wav"),
    arrayOf(".wma", "audio/x-ms-wma"),
    arrayOf(".wmv", "audio/x-ms-wmv"),
    arrayOf(".wps", "application/vnd.ms-works"),
    arrayOf(".xml", "text/plain"),
    arrayOf(".z", "application/x-compress"),
    arrayOf(".zip", "application/x-zip-compressed"),
    arrayOf("", "*/*")
)

/**
 * 第三方软件打开文件
 */
fun File.openFile(context: AppCompatActivity) {
    try {
        Intent().apply {
            addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
            action = Intent.ACTION_VIEW
            //获取文件file的MIME类型
            val type: String = getMIMEType()
            //设置intent的data和Type属性。
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                flags = Intent.FLAG_GRANT_READ_URI_PERMISSION
                val uri: Uri = FileProvider.getUriForFile(context, context.packageName + ".provider", this@openFile)
                setDataAndType(uri, type)
            } else {
                setDataAndType(Uri.fromFile(this@openFile), type)
            }
            context.startActivity(this)
            Intent.createChooser(this, "请选择对应的软件打开该附件!")
        }
    } catch (e: ActivityNotFoundException) {
        // 弹窗提示(此处用的kotlin的扩展,提示弹窗可自己编写) todo
        context.showDialogMessage("附件:${name}不能打开,您可下载相关软件或至内存卡/${absolutePath}目录下操作")
    }
}

/**
 * 获取文件file的MIME类型
 */
fun File.getMIMEType() = when {
    name.lastIndexOf(".") < 0 -> "*/*" // 无后缀名
    name.substring(name.lastIndexOf(".")).toLowerCase(Locale.ROOT).isEmpty() -> "*/*" // 后缀名为空
    else -> {
        var type = "*/*"
        //在MIME和文件类型的匹配表中找到对应的MIME类型。
        MIME_MapTable.forEach {
            if (name.substring(name.lastIndexOf(".")).toLowerCase(Locale.ROOT) == it[0]) type = it[1]
        }
        type
    }
}

使用方法

文件对象.openFile(context)

仅此作为笔记。

本文标签: 文件 第三方 压缩文件 App pdf