Flutter,如何从images文件夹中获取所有图像文件名?

时间:2019-01-22 15:45:21

标签: flutter

我想将图像/ pets /中的所有图像文件名加载到List<String> animals。我该怎么办?

3 个答案:

答案 0 :(得分:1)

path_provider,您可以获取目录temp和appDir目录

    final directory = await getApplicationDocumentsDirectory();
    String imagesDirectory = directory + "/images/pets/";

使用listSync方法查找此目录的文件后

final myDir = new Directory(imagesDirectory);
List<FileSystemEntity> _images;
_images = myDir.listSync(recursive: true, followLinks: false);

我希望我有所帮助

答案 1 :(得分:0)

Flutter生成一个名为AssetManifest.json的文件,您可以通过默认捆绑软件读取该文件,就像从资产中读取普通文本文件一样。

var manifestContent = DefaultAssetBundle.of(context).loadString('AssetManifest.json');

读取并解析此文件,然后根据所需的所有属性及其路径创建一个列表。只需仔细检查,以确保您拥有正确的路径,将来这可能会改变。在我看来,它就像一个占位符。

用于读取AssetManifest.json的伪代码

var manifestContent = DefaultAssetBundle.of(context).loadString('AssetManifest.json');
var manifestMap = json.decode(manifestContent);

var imagePetPaths = manifestMap.keys.where((key) => key.contains('images/pets/'));

// You can either use the keys and fetch the value from the map, 
or just use the key value since it's the same as the one in the pubspec.yaml

答案 2 :(得分:0)

继续 AdsHan 的回答

final dir = await getApplicationDocumentsDirectory();
final imagesDirectory = Directory(dir.path + "/images/pets/");

List<String> images = [];
final _imagesFile = imagesDirectory.listSync(followLinks: false, recursive: true);
_imagesFile.forEach((img) {
       String imgString = img.toString().substring(
       img.toString().lastIndexOf('/') + 1,
       img.toString().length);
       images.add(imgString); 
});