Reputation: 765
Im trying to create a music app as my first app , wondering how would i go on about reading the files from the external storage , I saw something about path_provider but that didn't really get me anywhere , any help on the basic logic ?
Upvotes: 2
Views: 3157
Reputation: 12803
Can write like this:
Future<List<FileSystemEntity>> _files() async {
var root = await getExternalStorageDirectory();
final List<FileSystemEntity> entities = await root!.list().toList();
return entities;
}
Upvotes: 0
Reputation: 2009
To list all the files or folders, you have to use flutter_file_manager, path, and path_provider_ex flutter package. Add the following lines in your pubspec.yaml file to add this package in your dependency.
dependencies:
flutter:
sdk: flutter
path: ^1.6.4
path_provider_ex: ^1.0.1
flutter_file_manager: ^0.2.0
Add dd read / write permissions in your android/app/src/main/AndroidManifest.xml before tag.
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
If you still get the Permission Denied error, add the following line on AndroidManifest.xml file.
<application
android:requestLegacyExternalStorage="true"
Full code:
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_file_manager/flutter_file_manager.dart';
import 'package:path_provider_ex/path_provider_ex.dart';
//import package files
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: MyFileList(), //call MyFile List
);
}
}
//apply this class on home: attribute at MaterialApp()
class MyFileList extends StatefulWidget{
@override
State<StatefulWidget> createState() {
return _MyFileList();
}
}
class _MyFileList extends State<MyFileList>{
var files;
void getFiles() async { //asyn function to get list of files
List<StorageInfo> storageInfo = await PathProviderEx.getStorageInfo();
var root = storageInfo[0].rootDir; //storageInfo[1] for SD card, geting the root directory
var fm = FileManager(root: Directory(root)); //
files = await fm.filesTree(
//set fm.dirsTree() for directory/folder tree list
excludedPaths: ["/storage/emulated/0/Android"],
extensions: ["png", "pdf"] //optional, to filter files, remove to list all,
//remove this if your are grabbing folder list
);
setState(() {}); //update the UI
}
@override
void initState() {
getFiles(); //call getFiles() function on initial state.
super.initState();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title:Text("File/Folder list from SD Card"),
backgroundColor: Colors.redAccent
),
body:files == null? Text("Searching Files"):
ListView.builder( //if file/folder list is grabbed, then show here
itemCount: files?.length ?? 0,
itemBuilder: (context, index) {
return Card(
child:ListTile(
title: Text(files[index].path.split('/').last),
leading: Icon(Icons.image),
trailing: Icon(Icons.delete, color: Colors.redAccent,),
)
);
},
)
);
}
}
Upvotes: 2