如何在 Flutter 中从内部和外部存储中获取所有 mp3 文件?

Shr*_*rma 3 dart flutter

我想使用 flutter 项目在列表视图中显示所有歌曲,但我不知道如何访问我的内部文件夹。

首先,我刚刚创建了一个名为“Shruti”的文件夹,但它是在 /storage/emulated/0/Android/data/com.example.musicplayer/files' 创建的,但我只想在 /storage/emulated 中创建该文件夹/0/ 路径。

因为我不知道,如何读写内部和外部存储。

final directory = await getApplicationDocumentsDirectory();
     print('Directory= $directory');
     // External storage directory: /storage/emulated/0
     Directory externalDirectory = await getExternalStorageDirectory();

     print('External Storage:$externalDirectory ');


     new Directory(externalDirectory.path+'/'+'Shruti').create(recursive: true)
     .then((Directory directory) {
       print('Path of New Dir: '+directory.path);
     });
Run Code Online (Sandbox Code Playgroud)

Rag*_*wal 8

此代码可用于读取存储。您必须指定路径。

Directory dir = Directory('/storage/emulated/0/');
String mp3Path = dir.toString();
print(mp3Path);
List<FileSystemEntity> _files;
List<FileSystemEntity> _songs = [];
_files = dir.listSync(recursive: true, followLinks: false);
for(FileSystemEntity entity in _files) {
  String path = entity.path;
  if(path.endsWith('.mp3'))
    _songs.add(entity);
}
print(_songs);
print(_songs.length);
Run Code Online (Sandbox Code Playgroud)

PS :- 不要忘记权限。

您还可以检查此包的其他功能。 https://pub.dev/packages/flutter_audio_query