鸿蒙平台open_filex使用指南
open_filex 是一个支持 HarmonyOS 的 Flutter 文件打开插件,修复了原 open_file 的多个问题。它支持跨平台调用本地应用打开各种文件类型,包括文档、图片、音视频等,并优化了 Android 权限和 iOS 兼容性。安装时需通过 Git 引入依赖,Android 可配置 FileProvider 解决冲突。使用简单,只需调用 OpenFilex.open() 方法即
插件介绍
open_filex 是一个用于在 Flutter 应用中调用本地应用打开文件的插件,支持 HarmonyOS 平台。该插件是 open_file 插件的分支版本,修复了多个已知问题并增强了兼容性。主要功能包括:
- ✅ 调用本地应用打开各种类型的文件
- ✅ 支持 iOS(DocumentInteraction) / Android(Intent) / PC(FFI) / Web(dart:html) / HarmonyOS
- ✅ 移除了 Android 中的
REQUEST_INSTALL_PACKAGES权限,符合 Google Play 发布政策 - ✅ 更新 FFI 到 2.0.1+ 版本
- ✅ 支持 Android 13 的精细媒体权限
- ✅ 修复了 Android 中的插件生命周期问题
- ✅ 修复了 iOS 中的 viewController 识别问题
- ✅ 修复了参数解析问题
- ✅ 将 JCenter 替换为 MavenCentral,提高稳定性
- ✅ 兼容 Gradle 8+
- ✅ 支持 iOS 嵌入式 Flutter 应用
该插件为自定义修改版本,已适配 HarmonyOS 平台,确保在鸿蒙设备上稳定运行。
安装与配置
1. 添加依赖
由于该三方库为自定义修改版本,需要以 Git 形式引入。在引用的项目中,pubspec.yaml 中 dependencies 新增配置:
dependencies:
open_filex:
git:
url: "https://atomgit.com/"
path: "fluttertpc_open_filex-master"
2. 同步依赖
添加依赖后,运行以下命令同步项目依赖:
flutter pub get
3. Android 配置(可选)
如果与其他插件的 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>
同时,在 android/app/src/main/res/xml/filepaths.xml 中添加以下配置:
<paths>
<external-path name="external-path" path="."/>
<external-cache-path name="external-cache-path" path="."/>
<external-files-path name="external-files-path" path="."/>
<files-path name="files_path" path="."/>
<cache-path name="cache-path" path="."/>
<root-path name="root" path="."/>
</paths>
API 使用示例
1. 导入插件
import 'package:open_filex/open_filex.dart';
2. 打开文件
// 打开文本文件
OpenFilex.open("/sdcard/example.txt");
// 打开 PDF 文件
OpenFilex.open("/sdcard/example.pdf");
// 打开图片文件
OpenFilex.open("/sdcard/example.jpg");
// 打开视频文件
OpenFilex.open("/sdcard/example.mp4");
// 打开音频文件
OpenFilex.open("/sdcard/example.mp3");
// 指定文件类型打开
OpenFilex.open("/sdcard/example.txt", type: "text/plain");
3. 完整示例
import 'package:flutter/material.dart';
import 'package:open_filex/open_filex.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('open_filex 示例'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
ElevatedButton(
onPressed: () async {
final result = await OpenFilex.open("/sdcard/example.txt");
print("打开结果: ${result.message}");
},
child: const Text('打开文本文件'),
),
const SizedBox(height: 20),
ElevatedButton(
onPressed: () async {
final result = await OpenFilex.open("/sdcard/example.pdf");
print("打开结果: ${result.message}");
},
child: const Text('打开 PDF 文件'),
),
const SizedBox(height: 20),
ElevatedButton(
onPressed: () async {
final result = await OpenFilex.open("/sdcard/example.jpg");
print("打开结果: ${result.message}");
},
child: const Text('打开图片文件'),
),
],
),
),
),
);
}
}
支持的文件类型
Android 支持的文件类型
- 文档类:
.txt,.doc,.docx,.xls,.xlsx,.ppt,.pptx,.pdf,.rtf,.xml - 图片类:
.jpg,.jpeg,.png,.gif,.bmp,.ico - 音频类:
.mp3,.wav,.wma,.ogg,.m4a,.m4b,.m4p,.mp2,.mpga - 视频类:
.mp4,.avi,.mov,.m4v,.mpg,.mpeg,.mpe,.mpg4,.3gp,.3gpp,.asf,.wmv - 压缩包:
.zip,.rar,.tar,.gz,.gzip,.tgz,.z - 其他:
.apk,.bin,.conf,.cpp,.h,.java,.log,.prop,.sh,.torrent,.kml,.gpx
iOS 支持的文件类型
- 文档类:
.txt,.doc,.docx,.xls,.xlsx,.ppt,.pptx,.pdf,.rtf,.xml - 图片类:
.jpg,.jpeg,.png,.gif,.bmp,.ico - 音频类:
.mp3,.wav,.wm,.wma,.ogg,.m4a - 视频类:
.mp4,.avi,.mov,.mpg,.mpeg,.3gp,.3gpp - 压缩包:
.zip,.tar,.gz,.gzip,.tgz
总结
open_filex 插件为 HarmonyOS 平台的 Flutter 应用提供了便捷的文件打开功能。通过简单的 API 调用,开发者可以轻松实现调用本地应用打开各种类型文件的功能,无需自行处理复杂的文件格式和应用关联问题。该插件修复了多个已知问题并增强了兼容性,确保在 HarmonyOS 平台上稳定运行。同时,插件支持多种文件类型,满足不同应用场景的需求。
欢迎加入开源鸿蒙跨平台社区:https://openharmonycrossplatform.csdn.net
更多推荐




所有评论(0)