在 Flutter 中写入文件时出现“No such file or directory”错误

'No such file or directory' error when writing file in Flutter

提问人:CraigFoote 提问时间:1/3/2022 最后编辑:Sunderam DubeyCraigFoote 更新时间:6/19/2022 访问量:3136

问:

我正在尝试将图像字节写入本地文件,但出现错误:

Directory: '/data/user/0/ca.footeware.flutter.xkcdrandomizer/app_flutter'/flutter.png: open failed: ENOENT (No such file or directory)

我的相关代码:

AndroidManifest.xml:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.xkcd_randomizer">

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

<application
    android:label="xkcd_randomizer"
    android:icon="@mipmap/ic_launcher"
    android:requestLegacyExternalStorage="true">

[截图]

main.dart:

import 'dart:convert';
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:http/http.dart';
import 'package:http/io_client.dart';
import 'package:path_provider/path_provider.dart';
import 'package:permission_handler/permission_handler.dart';
import 'package:photo_view/photo_view.dart';
import 'package:share/share.dart';

[截图]

Future<PermissionStatus> get permission async {
    return await Permission.storage.request();
}

[截图]

  Future<void> share() async {
    if (await permission.isGranted) {
      final RenderBox box = context.findRenderObject() as RenderBox;
      var response = await get(Uri.parse(_url));
      var directory = await getApplicationDocumentsDirectory();
      print('dir=$directory');
      File imgFile = File('/data/user/0/ca.footeware.flutter.xkcdrandomizer/app_flutter/flutter.png');
      print('imgFile=$imgFile');
      var bodyBytes = response.bodyBytes;
      print(bodyBytes);
      imgFile.writeAsBytes(bodyBytes);
      File file = File('$directory/flutter.png');
      Share.shareFiles([file.path],
          subject: 'XKCD comic',
          text: 'test',
          sharePositionOrigin: box.localToGlobal(Offset.zero) & box.size);
    }
  }

print 语句的调试输出:

I/flutter ( 9509): dir=Directory: '/data/user/0/ca.footeware.flutter.xkcdrandomizer/app_flutter'
I/flutter ( 9509): imgFile=File: '/data/user/0/ca.footeware.flutter.xkcdrandomizer/app_flutter/flutter.png'
I/flutter ( 9509): [255, 216, 255, 224, 0, 16, 74, 70, 73, 70, 0, 1, 2, 1, 1, 144, 1, 144, 0, 0, 255, 225, 1, 26, 69, 120, 105, 102, 0, 0, 77, 77, 0, 42, 0, 0, 0, 8, 0, 9, 1, 15, 0, 2, 0, 0, 0, 6, 0, 0, 0, 122, 1, 16, 0, 2, 0, 0, 0, 15, 0, 0, 0, 128, 1, 18, 0, 3, 0, 0, 0, 1, 0, 1, 0, 0, 1, 26, 0, 5, 0, 0, 0, 1, 0, 0, 0, 143, 1, 27, 0, 5, 0, 0, 0, 1, 0, 0, 0, 151, 1, 40, 0, 3, 0, 0, 0, 1, 0, 2, 0, 0, 1, 49, 0, 2, 0, 0, 0, 21, 0, 0, 0, 159, 1, 50, 0, 2, 0, 0, 0, 20, 0, 0, 0, 180, 135, 105, 0, 4, 0, 0, 0, 1, 0, 0, 0, 200, 0, 0, 0, 0, 67, 97, 110, 111, 110, 0, 67, 97, 110, 111, 83, 99, 97, 110, 32, 57, 57, 53, 48, 70, 0, 0, 0, 1, 144, 0, 0, 0, 1, 0, 0, 1, 144, 0, 0, 0, 1, 67, 97, 110, 111, 83, 99, 97, 110, 32, 84, 111, 111, 108, 98, 111, 120, 32, 52, 46, 57, 0, 50, 48, 48, 53, 58, 49, 49, 58, 48, 51, 58, 50, 48, 58, 51, 51, 58, 50, 57, 0, 0, 4, 144, 0, 0, 7, 0, 0, 0, 4, 48, 50, 50, 49, 144, 4, 0, 2, 0, 0, 0, 20, 0, 0, 0, 254, 160, 0, 0, 7, 0, 0, 0, 4, 48, 49, 48, 48, 160, 1, 0, 3, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 50, 48, 48, 53

...然后是行:

imgFile.writeAsBytes(bodyBytes);

...导致错误。我尝试了writeAsBytesSync,但得到了相同的结果。我将文件路径硬编码为调试步骤,但您可以看到它与文档目录相同。

Android Flutter 文件 dart io

评论


答:

1赞 jamesdlin 1/3/2022 #1

密切注意打印输出。当您这样做时,您将获得:print('dir=$directory')

dir=Directory: '/path/to/directory'

而不是

dir=/path/to/directory

换句话说,不返回路径,而是返回一个字符串:Directory.toString()

Directory: '/path/to/directory'

同样的事情也适用于.相反,您应该使用以下属性:File.toString().path

File file = File('${directory.path}/flutter.png');