flutter_document_picker
文档选择器,image_picker只能选择图片和视频,如果要选择PDF,word文档、excel表格等就没办法了。这个时候可以使用flutter_document_picker插件,直接选取手机中的文件。
1、安装
flutter pub add flutter_document_picker
1.
或者
/// pubspec.yaml文件添加依赖,并在执行flutter pub get命令
dependencies
flutter_document_picker: ^5.1.0
或
flutter_document_picker: ^5.2.3
2、使用
本代码是打开手机文件管理器选择需要的文件并把文件存储到手机沙盒中(沙盒的存储路径重新编辑)
static Future<File?> getLocalDocumentFile() async { final path = await FlutterDocumentPicker.openDocument(); if (path != null) { var file = File(path); // ファイル名称を取得する String tgzName = file.toString().split("/").last; // サンドボックスのパース+file+ファイル名称 file = await file.rename('$_temporaryDirectory/$fileName/$tgzName'); return file; } return null; }
调用
FileUtil.getLocalDocumentFile().then((valueUsb) { if (valueUsb != null) { file = null; String fileSuffix = valueUsb.path.split("E1FW_PKG_v").last; if (fileSuffix.isNotEmpty) { if (fileSuffix.length == 8) { // ファイル名が E1FW_PKG_vxxx.tgz 以外ならば、FW 更新ファイルとして認識しないこと。 if (fileSuffix.contains(".tgz")) { file = valueUsb; } } } if (file == null) { CommonDialog.show( title: "確認", message: "想定外のファイルを選択しましたので、読み込めません。改めてファイルを選択してください。", task: null, action: (button) { if (DialogButtonSelectType.rightOrBottom == button) { setState(() {}); } }, main: '戻 る'); } } else { setState(() {}); } }