Flutter文件打开功能插件open_filef的使用
Flutter文件打开功能插件open_filef的使用
open_filef
是一个在 Flutter 中调用原生应用打开文件的插件,支持iOS (DocumentInteraction)、Android (intent)、PC (ffi) 和 Web (dart:html)。
注意事项
该插件是 open_file
的一个分叉版本,修复了以下问题:
- 移除 Android 上的
REQUEST_INSTALL_PACKAGES
权限以符合 Google Play 发布政策。 - 更新
ffi
至 2.0.1+ 版本。 - 升级 Android 13 对细粒度媒体权限的支持。
- 修复 Android 上的生命周期回调问题。
- 修复 iOS 上的 viewController 识别问题。
- 修复命令解析参数过滤问题。
- 将 Android 构建文件中的 JCenter 替换为 MavenCentral。
完整的更改列表请参见 CHANGELOG。
使用方法
要使用此插件,在你的 pubspec.yaml
文件中添加依赖项:
dependencies:
open_filef: ^lastVersion
示例代码
import 'package:open_filef/open_filef.dart';
// 打开指定路径的文件
OpenFilef.open("/sdcard/example.txt");
支持
Android
当与其他插件冲突时,可能需要在 /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
文件中添加以下代码:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<external-path name="external_storage_directory" path="." />
</resources>
如果遇到 Android 依赖冲突问题(如 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 with UTI (DocumentInteraction Auto)
对于 iOS,可以使用统一类型标识符(UTI)来打开文件。以下是一些常见的文件扩展名及其对应的 UTI:
{
{".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_filef的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html
更多关于Flutter文件打开功能插件open_filef的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
在Flutter中,open_file
插件可以用来打开本地文件,支持多种文件类型,包括PDF、图像、视频、音频、文档等。以下是使用 open_file
插件的基本步骤:
1. 添加依赖
首先,在 pubspec.yaml
文件中添加 open_file
插件的依赖:
dependencies:
flutter:
sdk: flutter
open_file: ^3.2.1 # 请使用最新版本
然后运行 flutter pub get
来获取依赖。
2. 导入包
在需要使用 open_file
的文件中导入包:
import 'package:open_file/open_file.dart';
3. 打开文件
使用 OpenFile.open
方法打开文件。该方法接受一个文件路径作为参数,并尝试使用设备的默认应用程序打开该文件。
void openFile(String filePath) async {
final result = await OpenFile.open(filePath);
if (result.type == ResultType.done) {
print("文件打开成功");
} else if (result.type == ResultType.noAppToOpen) {
print("没有找到可以打开该文件的应用程序");
} else if (result.type == ResultType.fileNotFound) {
print("文件未找到");
} else if (result.type == ResultType.permissionDenied) {
print("权限被拒绝");
} else if (result.type == ResultType.error) {
print("发生错误:${result.message}");
}
}
4. 处理文件路径
确保文件路径是有效的。OpenFile.open
需要文件的完整路径。你可以使用 path_provider
插件来获取应用目录中的文件路径。
import 'package:path_provider/path_provider.dart';
Future<String> getFilePath(String fileName) async {
Directory appDocumentsDirectory = await getApplicationDocumentsDirectory();
return '${appDocumentsDirectory.path}/$fileName';
}
5. 完整示例
以下是一个完整的示例,演示如何使用 open_file
插件打开一个文件:
import 'package:flutter/material.dart';
import 'package:open_file/open_file.dart';
import 'package:path_provider/path_provider.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
[@override](/user/override)
Widget build(BuildContext context) {
return MaterialApp(
home: FileOpenerDemo(),
);
}
}
class FileOpenerDemo extends StatelessWidget {
Future<void> openFile(String fileName) async {
final filePath = await getFilePath(fileName);
final result = await OpenFile.open(filePath);
if (result.type == ResultType.done) {
print("文件打开成功");
} else if (result.type == ResultType.noAppToOpen) {
print("没有找到可以打开该文件的应用程序");
} else if (result.type == ResultType.fileNotFound) {
print("文件未找到");
} else if (result.type == ResultType.permissionDenied) {
print("权限被拒绝");
} else if (result.type == ResultType.error) {
print("发生错误:${result.message}");
}
}
Future<String> getFilePath(String fileName) async {
Directory appDocumentsDirectory = await getApplicationDocumentsDirectory();
return '${appDocumentsDirectory.path}/$fileName';
}
[@override](/user/override)
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Open File Example'),
),
body: Center(
child: ElevatedButton(
onPressed: () => openFile('example.pdf'),
child: Text('Open PDF'),
),
),
);
}
}