搜索插件、代码仓与 APP 分类

插件详情v4.0.5

open_app_file

一个插件,用于使用系统默认提供的应用程序打开应用有权限访问的文件。

AndroidiOSWebmacOSWindowsLinux
近 30 天下载
8550
喜欢数
30
Pub 分数
150

安装插件

flutter pub add open_app_file

插件介绍

基于插件公开信息、社区数据与项目使用情况整理的选型参考。

一个 Flutter 插件,可使用系统默认应用程序打开应用可访问的文件。它支持在 Android、iOS、Web、macOS、Windows 和 Linux 上跨平台启动文件,使开发者能够轻松地将文件打开功能集成到应用中,而无需编写特定于平台的代码。该插件适用于应用拥有访问权限的文件,例如应用目录中的文件或通过文件选择器选择的文件。

适合场景

  • 打开下载的文档
  • 启动图像或PDF
  • 与原生应用共享文件
  • 集成文件预览
  • 启用用户驱动的文件操作

主要特点

  • 跨平台兼容性
  • 使用系统默认应用
  • 支持所有主要操作系统
  • 文件打开的简单 API
  • 通过授予的权限进行安全文件访问

适合对象

  • 文件管理应用
  • 文档查看器
  • 媒体播放器
  • 生产力工具
  • 需要文件共享的应用程序

使用建议

  • 需要适当的文件权限
  • 仅限应用可访问的文件
  • 平台特定的行为可能有所不同
  • 不处理文件创建
  • Web 支持取决于浏览器功能

原始 README

以下为关联源码仓库的 README 原文快照,最新内容请访问源码仓库。

open_file

pub package

A plugin to open files the app has permission to access with default system-provided applications.

Supports Android, iOS, Web, Linux, macOS and Windows.

Fork info

The library is forked from open_file by crazecoder

This version:

  • does not include REQUEST_INSTALL_PACKAGES permission on Android, so cannot install .apk's
  • does not include READ_EXTERNAL_STORAGE permission on Android
  • has completely rewritten Android implementation for more robust permission checks
  • has deprecated explicit list of MIME types, UTI's, and Linux launch options
  • web implementation is changed (see below)

The main use case for the plugin is opening a generated or downloaded file with a default system application (see example).

Usage

Add open_file as a dependency in your pubspec.yaml file.

dependencies:
  open_app_file: ^lastVersion

Example

import 'package:open_app_file/open_app_file.dart';

OpenAppFile.open('/sdcard/example.txt');

// You can provide overrides for MIME type (Android) and/or UTI (iOS). This is not necessary
// in most cases, but if you know that the file extension does not match its content, you can
// help the system to provide correct response to your request.
OpenAppFile.open('/sdcard/example.abc', mimeType: 'text/plain', uti: 'public.plain-text');

Behavior on Android

While the main purpose of the plugin is opening files owned by the host app, it is possible to open any file even considering Android 11 scoped storage limitations (see official docs). The plugin will not provide any tools for requesting permissions to prevent declaring them in the Manifest for apps that don't need to handle this case. An attempt to open a file with no permissions granted will produce an appropriate result (ResultType.permissionDenied).

See more details and instructions on how to test permission conditions in the example app.

Behavior on web

Direct local file access is generally discouraged on web, therefore we chose the approach that in most cases is the most logical web alternative: imitate user clicking on an element that targets the requested file. For most files it will result in browser downloading the requested file immediately. Some file types, like images, texts, or videos might be opened directly, so the library adds target: _blank attribute to prevent user from leaving the application by accident.

The important limitation of this approach is that there's no way to check file type or availability beforehand, so on web the open call always succeeds.

Check the example app for general idea on how app-generated or remote files can be handled on web.

所属分类

依赖

依赖清单 2 项

源码仓库

使用此插件的项目