Shruti Ramnandan sharma
Shruti Ramnandan sharma

Reputation: 1057

How to get all mp3 files from internal as well as external storage in Flutter?

I want to show all songs in listview with flutter project but i dunno, how to access my internal folder.

first of all I have just created a folder with name 'Shruti' but it is created at /storage/emulated/0/Android/data/com.example.musicplayer/files' but i want to that folder only at /storage/emulated/0/ path.

Because i don't know, how to read & write to internal as well as external storage .

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);
     });

Upvotes: 4

Views: 9590

Answers (1)

Raghav Agarwal
Raghav Agarwal

Reputation: 121

This code can be used to read storage. Your have to specify the path.

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);

P.S. :- Don't forget the permissions though.

You can also check this package for other features. https://pub.dev/packages/flutter_audio_query

Upvotes: 12

Related Questions