Home > Net >  i want to ask for external storage access from the user to delete the files(songs)
i want to ask for external storage access from the user to delete the files(songs)

Time:09-30

            File file = File(widget.singleTrack!.data);
            try {
              file.delete();
              if (await file.exists()) {
                print('Song not deleted');
              } else {
                widget.allSongs!.remove(widget.singleTrack);
              }
            } catch (e) {
              print('$e');
            }

This code does not work for external storage .

CodePudding user response:

I think you need to append the root path of external storage which varies from OS to OS and device to device but with the help of ext_storage you can achieve it. Also take a look at this question

CodePudding user response:

Regular File.delete() method would work for android 10 and below if you have writing permission but for android 11 and above you'd have to get the all files access permission.

To do so add this line in you android/app/src/main/AndroidManifest.xml,

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

And get the user to grant the permission like this,

if (await Permission.manageExternalStorage.request().isGranted) {...}

NOTE: Google doesn't appreciate this method. To properly delete you'd have to use MediaStore API for which you'd have to use native.

  • Related