如何读取外部存储器上的Flutter?尝试读取音乐文件

xytpbqjk  于 2022-11-17  发布在  Flutter
关注(0)|答案(2)|浏览(205)

我试图创建一个音乐应用程序作为我的第一个应用程序,想知道我将如何继续从外部存储阅读文件,我看到了一些关于路径提供者,但这并没有真正让我去任何地方,任何帮助的基本逻辑?

jjhzyzn0

jjhzyzn01#

要列出所有的文件或文件夹,必须使用flutter_file_manager、path和path_provider_ex flutter包。在pubspec.yaml文件中添加以下行,以便将此包添加到依赖项中。

dependencies:
  flutter:
    sdk: flutter
  path: ^1.6.4
  path_provider_ex: ^1.0.1
  flutter_file_manager: ^0.2.0

在您的android/app/src/main/AndroidManifest.xml中的标签之前添加dd读/写权限。

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

如果仍然出现Permission Denied错误,请在AndroidManifest.xml文件中添加以下行。

<application
      android:requestLegacyExternalStorage="true"

完整代码:

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

pwuypxnk

pwuypxnk2#

可以这样写:

Future<List<FileSystemEntity>> _files() async {
    var root = await getExternalStorageDirectory();
    final List<FileSystemEntity> entities = await root!.list().toList();
    return entities;
  }

相关问题