【问题标题】:How to read a file path that contains whitespace?如何读取包含空格的文件路径?
【发布时间】:2021-01-25 06:12:34
【问题描述】:

我的保存图片方法:

_saveScreen() async {
    RenderRepaintBoundary boundary =
        _globalKey.currentContext.findRenderObject();
    ui.Image image = await boundary.toImage();
    ByteData byteData = await image.toByteData(format: ui.ImageByteFormat.png);
    final result =
        await ImageGallerySaver.saveImage(byteData.buffer.asUint8List());
    imageDateList.add('${dateTime.day}.${dateTime.month}.${dateTime.year}');
    prefs.setStringList('imageDateList', imageDateList);
    Navigator.of(context).pop();

    String tmpStringHolder =
        result.toString().split('storage')[1].split(',')[0];
    photoData.addPhoto('storage$tmpStringHolder');
    _toastInfo(result.toString());
  }

我正在使用这些包来写入和读取图像文件:

image_picker: ^0.6.0+3
permission_handler: ^5.0.1+1
image_gallery_saver: ^1.6.7
path_provider: ^1.5.1+1

当我的应用名称中没有空格时,我的项目运行良好。 现在,当我尝试加载图像时出现此错误:

'storage/emulated/0/Buttocks%20leg/1611320083489.jpg' (OS Error: No such file or directory, Errno = 2)

我尝试使用其他软件包,但找不到任何解决方案。我试图读取这样的路径,但这也不起作用。

Image.file(File('\"storage/emulated/0/Buttocks leg/1611553265363.jpg\"')),

正如你在上面我尝试的方式所看到的。尝试此操作时出现此错误:

Cannot open file, path = '"storage/emulated/0/Buttocks leg/1611553265363.jpg"' (OS Error: No such file or directory, errno = 2)

我必须读取名称中包含空格的文件路径,我该怎么做?

【问题讨论】:

  • 你试过这个Image.file(file_path)吗?
  • 你的意思是,file_path 是这个? 'storage/emulated/0/Buttocks%20leg/1611320083489.jpg'
  • 是的,对,你可以试试这个
  • 或者请添加您的图像选择器代码,以便帮助您
  • 好的,我将添加选择器代码

标签: android image flutter dart


【解决方案1】:

尝试使用FileImage

例子:

new FileImage(
    new File('/storage/emulated/0/Buttocks leg/1611553265363.jpg')
);

【讨论】:

  • 很高兴它帮助了你:)
猜你喜欢
  • 2013-04-07
  • 1970-01-01
  • 2017-04-06
  • 2011-04-13
  • 1970-01-01
  • 2020-01-25
  • 1970-01-01
  • 2017-04-30
  • 1970-01-01
相关资源
最近更新 更多