如何从Flutter中的外部存储获取文件? [英] How to get files from external storage in Flutter?

查看:1118
本文介绍了如何从Flutter中的外部存储获取文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用flutter项目在listview中获取所有文件,但是如何访问外部文件夹.

I want to get all file in listview with flutter project but how to access the external folder.

我创建了一个名为"MyFile"的文件夹,并在"/storage/emulated/0/MyFile"下创建了该文件夹,

I have created a folder with name 'MyFile' and it is created at "/storage/emulated/0/MyFile",

但下面的代码指向"/storage/emulated/0/Android/data/com.example.demo/MyFile".

but below code pointing at "/storage/emulated/0/Android/data/com.example.demo/MyFile".

我不知道为什么下面的代码不起作用

I don't know why below code is not working

 Directory externalDirectory = await getExternalStorageDirectory();
 print('External Storage:$externalDirectory');
 // External storage: /storage/emulated/0/Android/data/com.example.demo/MyFile

推荐答案

使用Flutter Official时 path_provider 软件包,getExternalStorageDirectory()将始终返回/storage/emulated/0/您的软件包名称/文件的路径.

When using Flutter Official path_provider package, getExternalStorageDirectory() will always return path to /storage/emulated/0/your-package-name/files.

要获取/storage/emulated/0/,您可以使用第三方软件包

To get /storage/emulated/0/, you can used a Third-party package ext_storage. Below code will return your desired Directory path

 var externalDirectoryPath = await ExtStorage.getExternalStorageDirectory();
 print(path);  // /storage/emulated/0

现在要创建文件夹,您可以使用以下功能:

Now to create a Folder, you can use the below function:

'//this will create a Folder in the storage/emulated/0
new Directory(externalDirectoryPath +'/YourfolderName')
  .create()
  .then((Directory directory) 
  {
    print(directory.path);
  });;

'

这篇关于如何从Flutter中的外部存储获取文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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