一个 Flutter 插件,可将支持的 URI 转换为 Android 和 iOS 上的 File 对象。它使开发者能够通过将 URI 转换为可用的 File 实例,从各种 URI 源(如图片或文档)访问文件。在使用文件选择器、相机拍摄或外部文件引用时尤其有用。该插件通过抽象平台特定逻辑,简化了跨平台应用中的文件处理。
适合场景
将图库中的图像 URI 转换为文件
处理来自外部源的文档文件路径
处理来自摄像头的捕获媒体
从共享存储读取文件
与文件选择器插件集成
支持从 URI 上传文件
主要特点
Android 和 iOS 的跨平台支持
简单的 URI 到文件转换
处理常见文件类型
所需设置最少
与原生文件系统访问兼容
与文件选择器集成兼容
适合对象
需要从 URI 访问文件的应用程序
媒体处理工作流
文件上传功能
图像和文档处理
使用文件选择器或相机的开发者
使用建议
仅支持 Android 和 iOS
需要文件访问的正确权限
仅限支持的 URI 方案
可能不适用于所有自定义或加密的 URI
取决于底层平台的实现
原始 README
以下为关联源码仓库的 README 原文快照,最新内容请访问源码仓库。
uri_to_file
A Flutter plugin for converting supported uri to file. Supports Android & iOS.
Supported Uri Schema
content:// (Android Only)
Schema supported by File.fromUri(uri)
Get started
Add dependency
dependencies:uri_to_file:^1.0.0
Super simple to use
import'dart:io';
import'package:uri_to_file/uri_to_file.dart';
Future<void> convertUriToFile() async {
try {
String uriString = 'content://sample.txt'; // Uri string// Don't pass uri parameter using [Uri] object via uri.toString().// Because uri.toString() changes the string to lowercase which causes this package to misbehave// If you are using uni_links package for deep linking purpose.// Pass the uri string using getInitialLink() or linkStreamFile file = awaittoFile(uriString); // Converting uri to file
} catch (e) {
print(e); // Exception
}
}
Important note
Don't pass uri parameter using [Uri] object via uri.toString(). Because uri.toString() changes the string to lowercase which causes this package to misbehave
If you are using uni_links package for deep linking purpose. Pass the uri string using getInitialLink() or linkStream
Check out the full example: https://pub.dev/packages/uri_to_file/example
Working example
Background
content:// (Android Only)
uri_to_file creates a temporary file using the content:// uri with the help of native channel that stores this file in the application directory.
All the others uri are handled by flutter sdk itself with the help of
File.fromUri(uri)
Copyright & License
MIT License
Features and bugs
Please file feature requests and bugs at the issue tracker.