android-未处理的异常:FileSystemException:无法打开文件,路径 = '/storage/emulated/0/Download/(file).csv'(操作系统错误:权限被拒绝,errno = 13)
'File.writeAsString' 到文件(存在)引发错误'未处理的异常:FileSystemException:无法打开文件,路径 ='/storage/emulated/0/Download/(file).csv'(操作系统错误:权限被拒绝,errno = 13)'
我已经尝试了下面的所有内容,但它一直在出错。有没有其他方法可以解决这个问题?
targetSdkVersion 28
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
android:requestLegacyExternalStorage="true"
var status = await Permission.storage.status;
if (!status.isGranted) {
await Permission.storage.request();
}
android sdk 版本:31,我的代码:
Future<File> writeToFile(csv, String deviceName) async {
final file = await _getlocalFile(deviceName);
return file.writeAsString(csv, mode: FileMode.append);
}
Future<File> _getlocalFile(String deviceName) async {
final path = await _localPath;
return File("${path.path}/${deviceName.replaceAll(':', '')}.csv");
}
Future<String> get _localPath async {
var path = await ExternalPath.getExternalStoragePublicDirectory(
ExternalPath.DIRECTORY_DOWNLOADS);
String stringPath = path.toString();
return stringPath;
}