如何在Flutter应用程序中从磁盘打开文件? [英] How do I open a file from disk, in a Flutter app?

查看:290
本文介绍了如何在Flutter应用程序中从磁盘打开文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Flutter应用中,我有一个dart文件,位于 FlutterTest\sandbox\lib\my_widget\my_widget.dart

In a Flutter app I have a dart file located at FlutterTest\sandbox\lib\my_widget\my_widget.dart.

我有一个位于 FlutterTest\sandbox\lib\my_widget\imgs\myImage.png 的图片。

来自 my_widget.dart 如何将文件指向该图像,类似于 new File( ??? \my_widget\myImage.png)

From my_widget.dart how can I point a file to this image, something along the lines of new File("???\my_widget\myImage.png")?

在Java中我可以做点什么像 MyClass.class.getResource( myImage.png)。toString()

Just to clarify, in Java I could do something like MyClass.class.getResource("myImage.png").toString().

如果我 print(Directory.current.path)它只是给我 / 。如果我尝试列出此目录的内容,它会告诉我:目录列表失败,路径=‘/’(操作系统错误:权限被拒绝,errno = 13) 。所以我实际上不知道当前目录在哪里。

By the way if I print(Directory.current.path) it gives me simply /. And if I try to list the contents of this directory it tells me: "Directory listing failed, path = '/' (OS Error: Permission denied, errno = 13)". So I don't actually know where the current directory is.

推荐答案

您可以直接访问文件系统,但是由于在android和iOS上,您可能最终会使用路径提供程序插件。另外,请参见抖动 cookbook ,就像他们所说的那样。

You can directly access the file system but due to differences in android and iOS you'll probably end up using the path provider plugin. Also, see this flutter cookbook as they call it.

但是,如果您要尝试做的就是访问文件从构建中,您将需要在pubspec中将其定义为资产,然后使用Flutter的AssetBundle(或Image.asset)而不是直接使用File或Image.file来访问它。请参见关于资产的Flutter文档。

But if all you're trying to do is access a file from your build, you'll want to define it as an asset in your pubspec, and then access it using Flutter's AssetBundle (or Image.asset) rather than directly using File or Image.file. See the flutter documentation on assets.

这篇关于如何在Flutter应用程序中从磁盘打开文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆