[译]Flutter调用原生应用打开文件插件open_file

[译]Flutter调用原生应用打开文件插件open_file本文翻译自 open_file | Flutter Package (pub.dev) 该插件在 Flutter 中用字符串来调用原生应用来打开文件。

本文翻译自 open_file | Flutter Package (pub.dev)


该插件在 Flutter 中用字符串来调用原生应用来打开文件。 支持 iOS(DocumentInteraction) / android(intent) / PC(ffi) / web(dart:html)

使用

要使用该插件,需要在 pubspec.yaml 中添加依赖。

dependencies:
  #androidx
  open_file: ^lastVersion 
  #support
  #open_file: ^1.3.0

示例

import 'package:open_file/open_file.dart';

OpenFile.open("/sdcard/example.txt");
//OpenFile.open("/sdcard/example.txt", type: "text/plain", uti: "public.plain-text");

支持的文件格式

Android

{
            {".3gp",    "video/3gpp"},
            {".torrent","application/x-bittorrent"},
            {".kml",    "application/vnd.google-earth.kml+xml"},
            {".gpx",    "application/gpx+xml"},
            {".csv",    "application/vnd.ms-excel"},
            {".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"},
            {"",        "*/*"}
}

如果和其它的有关 FileProvider 的插件冲突,在 /android/app/src/main/AndroidManifest.xml 中添加下面的代码

<manifest xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" package="xxx.xxx.xxxxx">
    <application>
        ...
        <provider android:name="androidx.core.content.FileProvider" android:authorities="${applicationId}.fileProvider" android:exported="false" android:grantUriPermissions="true" tools:replace="android:authorities">
            <meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/filepaths" tools:replace="android:resource" />
        </provider>
    </application>
</manifest>

当 Androird 依赖 ‘com.android.support:appcompat-v7’ 有不同版本的编译错误时,在 /android/build.gradle 添加下面的代码

subprojects {
    project.configurations.all {
        resolutionStrategy.eachDependency { details ->
            if (details.requested.group == 'com.android.support'
                    && !details.requested.name.contains('multidex') ) {
                details.useVersion "27.1.1"
            }
        }
    }
}

IOS 使用 UTI (DocumentInteraction Auto)

{
            {".rtf",    "public.rtf"},
            {".txt",    "public.plain-text"},
            {".html",   "public.html"},
            {".htm",    "public.html"},
            {".xml",    "public.xml"},
            {".tar",    "public.tar-archive"},
            {".gz",     "org.gnu.gnu-zip-archive"},
            {".gzip",   "org.gnu.gnu-zip-archive"},
            {".tgz",    "org.gnu.gnu-zip-tar-archive"},
            {".jpg",    "public.jpeg"},
            {".jpeg",   "public.jpeg"},
            {".png",    "public.png"},
            {".avi",    "public.avi"},
            {".mpg",    "public.mpeg"},
            {".mpeg",   "public.mpeg"},
            {".mp4",    "public.mpeg-4"},
            {".3gpp",   "public.3gpp"},
            {".3gp",    "public.3gpp"},
            {".mp3",    "public.mp3"},
            {".zip",    "com.pkware.zip-archive"},
            {".gif",    "com.compuserve.gif"},
            {".bmp",    "com.microsoft.bmp"},
            {".ico",    "com.microsoft.ico"},
            {".doc",    "com.microsoft.word.doc"},
            {".xls",    "com.microsoft.excel.xls"},
            {".ppt",    "com.microsoft.powerpoint.​ppt"},
            {".wav",    "com.microsoft.waveform-​audio"},
            {".wm",     "com.microsoft.windows-​media-wm"},
            {".wmv",    "com.microsoft.windows-​media-wmv"},
            {".pdf",    "com.adobe.pdf"}
}

今天的文章[译]Flutter调用原生应用打开文件插件open_file分享到此就结束了,感谢您的阅读。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:http://bianchenghao.cn/23086.html

(0)
编程小号编程小号

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注