【问题标题】:Unable to load image if there is a space in path or name file Flutter如果路径或名称文件中存在空格,则无法加载图像 Flutter
【发布时间】:2018-09-12 12:53:06
【问题描述】:

我在动态加载资源图像时遇到问题。

我有一张图片,我正在尝试加载:

new AssetImage(img_path)

img_path 是:

/storage/emulated/0/WhatsApp/Media/WhatsApp Images/test.jpg

然后它返回:

(27161):引发了另一个异常:无法加载资产: /storage/emulated/0/WhatsApp/Media/WhatsApp Images/test.jpg

如果我尝试移动我的文件,例如在

/storage/emulated/0/WhatsApp/Media/test.jpg

有效。

但如果我用空格更改文件名,例如:

/storage/emulated/0/WhatsApp/Media/test with space.jpg

所以我猜这可能是路径中有空格的问题。

我该如何解决这个问题?

【问题讨论】:

  • 你是如何在pubspec.yaml注册的?
  • 否,因为我必须在用户加载时显示图像,所以我无法注册。
  • /storage/emulated/0/WhatsApp/Media/WhatsApp Images/test.jpg 看起来不像AssetImage。您可能想改用FileImage
  • 是的!谢谢甘特!使用 FileImage 即可。
  • Image.file(File('/storage/emulated/0/WhatsApp/Media/WhatsApp Images/test.jpg'), ),

标签: flutter


【解决方案1】:

我认为这是你真正想要的

new FileImage(
    new File('/storage/emulated/0/WhatsApp/Media/WhatsApp Images/test.jpg')
)

另见https://docs.flutter.io/flutter/painting/FileImage-class.html

【讨论】:

  • 非常感谢它的工作。您能否在回复中解释一下 AssetImage 和 FileImage 之间的区别?
  • 资产是嵌入在 APK 中的资源,用于安装应用程序。您需要在 pubspec.yaml 中注册此类资产,以便将它们添加到 APK。资产嵌入在已安装的应用程序中,因此不能直接作为设备存储中的文件使用。 AssetBundle 允许您从资产中读取(AssetImage 使用它)。 ImageFile 用于设备存储中作为普通文件可用的图像文件。 flutter.io/assets-and-images
  • 对我不起作用。收到此消息:抛出异常:类型“FileImage”不是“Widget”类型的子类型
  • 改用Image.file(...)
  • 在这种情况下,loadImageFromFile 应该看起来像 loadImageFromFile(String imgPath) { return File(imgPath); }
【解决方案2】:

如果您想将图像显示为WidgetFileImage() 不起作用,因此您需要像这样使用Image.file()

...

Image.file(File('/storage/emulated/0/lilop_app/native_screenshot-20211107163229.png'), width: 100),

...

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多