【问题标题】:Unable open file using file_picker & open_file.dart无法使用 file_picker 和 open_file.dart 打开文件
【发布时间】:2021-09-15 17:39:20
【问题描述】:

我正在尝试使用 file_icker.dart 和 open_file.dart 打开文件。 我正在使用 AS articfox、flutter 2.5 和 android 11(在模拟器和安卓设备上尝试过(Remi note))。

这是我的 openFile 代码:

void _openFileExplorer() async {
    setState(() => _loadingPath = true);

    try {
      _directoryPath = null;
      _paths = (
          await FilePicker.platform.pickFiles(
            type: _pickingType,
            allowMultiple: _multiPick,
            onFileLoading: (FilePickerStatus status) => print(status),
            allowedExtensions: (_extension?.isNotEmpty ?? false)
            ? _extension?.replaceAll(' ', '').split(',')
            : null,
          )
      )?.files;
    } on PlatformException catch (e) {
      print("Unsupported operation" + e.toString());
    } catch (ex) {
      print(ex);
    }

    if (!mounted) return;

    _loadingPath = false;
    _fileName =
      _paths != null
          ? _paths!.map((e) => e.name).toString()
          : '...';
    if (_paths != null) {
      print("FILEPATH FOUND==> $_paths");
      //try crating File using path
      File file = File(_paths.toString());
      //checking file exist
      (await file.exists()) ? print("file exist") : print("file !exist");
      //what the hell...try open it !!
      final _result = await OpenFile.open(_paths.toString());
      print("result: "+ _result.message);
    }
    else {
      print("nope");
    }
  }

这是输出:

D/FilePickerUtils( 9666): File loaded and cached at:/data/user/0/com.example.bismillah/cache/file_picker/pdf_tmp_fie.pdf
D/FilePickerDelegate( 9666): File path:[com.mr.flutter.plugin.filepicker.FileInfo@98399df]
I/flutter ( 9666): FilePickerStatus.done
I/flutter ( 9666): FILEPATH FOUND==> [PlatformFile(path: /data/user/0/com.example.bismillah/cache/file_picker/pdf_tmp_fie.pdf, name: pdf_tmp_fie.pdf, bytes: null, readStream: null, size: 5948832)]
I/flutter ( 9666): file !exist
I/flutter ( 9666): result: the [PlatformFile(path: /data/user/0/com.example.bismillah/cache/file_picker/pdf_tmp_fie.pdf, name: pdf_tmp_fie.pdf, bytes: null, readStream: null, size: 5948832)] file does not exists

让我困惑的是 file_picker 清楚地确认了选择的文件:

File loaded and cached at:/data/user/0/com.example.bismillah/cache/file_picker/pdf_tmp_fie.pdf

但是当我尝试将该路径分配给我计划用 open_file.dart 打开的文件时,它说 文件不存在。有人可以帮我理解发生了什么。谢谢

【问题讨论】:

    标签: flutter android-studio dart


    【解决方案1】:

    在谷歌搜索 2 天后,我终于成功了。

    据我了解,使用 android 10 及更高版本,您可以使用 .您需要读取AsBytes 并在允许的目录中创建新文件。

    所以我使用 _readFileBytes 形式: https://flutteragency.com/how-to-read-bytes-of-a-local-image-file-in-flutter/ 在我用 open_file.dart 作为新文件打开它之前。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-11-02
      • 2019-01-06
      • 2010-12-17
      • 2020-12-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多