How to list files/folder from internal or SD card storage in Flutter App

In this example, we have shown to list all the files or folders from Internal or External (SD Card) storage. You can also add an extension filter to list files with specific path extension. This type of functionality is very important for a PDF viewer, image viewer, or audio player apps.

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 read / write permissions in your android/app/src/main/AndroidManifest.xml before <application> 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"

Flutter dart code, see the explanation in the comment.

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

Output:

In this way, you can list all the files or folders from Internal or External (SD card) storage and show in your app.

7 Comments on this Article

HAshir Awan

when i test it on android 11 12 it shows this error and cant show files 

 FileSystemException: Directory listing failed, path = ’/storage/emulated/0/Android/obb’ (OS Error: Permission denied, errno = 13)

 

3 weeks ago

Hari Prasad Chaudhary

Be sure you have given permission in AndroidMenifest.xml file, and also try asking storage permission before accessing file. 

3 weeks ago

HAshir Awan

it cant be work for android 11 and android 12 please help what can i do for this.

3 weeks ago

Ahmar

It works fine for android. What about iOS ? What I should do for iOS ?

1 year ago

yuu

how about using path_provider package? because path_provider_ex is depreceated

1 year ago

Unown

Thanks a lot for this article

1 year ago

Krishna Dhakal

Thank you very much Flutter Campus for awesome guide articles. It is helping me a lot. I’m happy to find this website.

1 year ago


Please Wait...