In my Flutter application, I want users to be able to delete a music file and have Android MediaStore recognize this change. When I have the MANAGE_EXTERNAL_STORAGE permission, I can successfully delete the file without any issues. However, without this permission, the file deletion fails and I receive the following error:
PathNotFoundException: Cannot delete file, path = '/storage/emulated/0/Download/Fatih
Bulut & Elmas - Gör Bak.mp3' (OS Error: No such file or directory, errno = 2)
void getFilePath(File file, BuildContext context, int index) {
log('file '+ file.toString());
try {
if (file.existsSync()) {
index = Provider.of<PlayerStateles>(context, listen: false).currentIndex;
file.deleteSync();
OnAudioQuery _audioQuery = OnAudioQuery();
_audioQuery.scanMedia(file.path);
Provider.of<PlayerStateles>(context, listen: false).updateCurrentIndex(index + 1);
snackBarMessage(context);
} else {
log('File not found: ' + file.path);
}
} catch (error) {
log('Error: ' + error.toString());
}
}
When the MANAGE_EXTERNAL_STORAGE permission is not granted, the file is not deleted, and the mentioned error occurs. How can I delete the file and update the MediaStore properly without needing the MANAGE_EXTERNAL_STORAGE permission?