[英]Error when using File writeAsBytes in flutter android 11 Is there any other way it could work?
I am currently using flutter to save a photo, but I am getting an error.我目前正在使用颤振保存照片,但出现错误。
E/flutter (14379): [ERROR:flutter/lib/ui/ui_dart_state.cc(199)] Unhandled Exception: FileSystemException: Cannot open file, path = '/storage/emulated/0/Pictures/FolderName/2021-10-14T22:02:34.9847821.jpg' (OS Error: Operation not permitted, errno = 1)
E/flutter (14379): #0 _File.open.<anonymous closure> (dart:io/file_impl.dart:356:9)
E/flutter (14379): #1 _rootRunUnary (dart:async/zone.dart:1362:47)
E/flutter (14379): #2 _CustomZone.runUnary (dart:async/zone.dart:1265:19)
E/flutter (14379): <asynchronous suspension>
E/flutter (14379): #3 AddGenuineDetailController.saveFileAndroid (package:projectName/controllers/my/addGenuineDetail_controller.dart:55:7)
E/flutter (14379): <asynchronous suspension>
E/flutter (14379):
I want to save it to the path over there and use it.我想把它保存到那边的路径并使用它。 That's because it appears in the Gallery app.那是因为它出现在图库应用程序中。 However, in Android 11 or later, the file is not saved to that path.但是,在 Android 11 或更高版本中,文件不会保存到该路径。 However, when saving to the internal storage where the app is installed, the file is saved.但是,当保存到安装应用程序的内部存储时,文件会被保存。
The current build.gradle当前的 build.gradle
android {
compileSdkVersion 30
sourceSets {
main.java.srcDirs += 'src/main/kotlin'
}
defaultConfig {
// TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
applicationId "com.company.projectName"
// minSdkVersion 16
minSdkVersion 20
targetSdkVersion 29
versionCode flutterVersionCode.toInteger()
versionName flutterVersionName
multiDexEnabled true
}
Setting the target sdk to 28 had no effect.将目标 sdk 设置为 28 无效。
AndroidManifest.xml Application AndroidManifest.xml 应用程序
<application
android:label="projectName"
android:icon="@mipmap/ic_launcher"
android:usesCleartextTraffic="true"
android:requestLegacyExternalStorage="true"
android:preserveLegacyExternalStorage="true"
>
AndroidManifest.xml permission AndroidManifest.xml 权限
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_MEDIA_LOCATION" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.USE_FULL_SCREEN_INTENT" />
<uses-permission android:name="android.permission.REQUEST_COMPANION_USE_DATA_IN_BACKGROUND" />
Flutter code颤振代码
void saveFileAndroid() async {
final savePath = "/storage/emulated/0/Pictures/ProjectName";
final Directory directory = Directory("$savePath");
print("저장소 유무 : ${await directory.exists()}");
if(await directory.exists() != true){
final Directory _appDocDirNewFolder = await directory.create(recursive: true);
print("저장소 유무 : ${await directory.exists()}");
print(_appDocDirNewFolder.path);
}
int i = 0;
for(var file in certificateList){
i += 1;
var url = Uri.parse(GlobalApiService.getImage(file.path!));
var res = await http.get(url);
String imgName = "${model!.product.createdDate}$i.jpg";
File saveImg = new File('${directory.path}/$imgName');
await saveImg.writeAsBytes(Uint8List.fromList(res.bodyBytes));
}
}
Is there any way to save on Android 11 or higher?有没有办法在 Android 11 或更高版本上保存?
I think this happening in Android 11 due to their new changes.我认为这发生在 Android 11 中是由于它们的新变化。 So you have to add one more permission (manageExternalStorage).所以你必须再添加一个权限(manageExternalStorage)。 Like this像这样
if (Platform.isAndroid) {
Map<Permission, PermissionStatus> statuses = await [
Permission.manageExternalStorage
].request();//Permission.manageExternalStorage
}
Even you have to add read & write permission as well.即使您也必须添加读写权限。
2021-10-14T22:02:34.9847821.jpg 2021-10-14T22:02:34.9847821.jpg
Do not use forbidden characters like :
in file names.不要在文件名中使用禁止字符,例如:
。
我有同样的问题,我通过删除解决了它:
从代码路径
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.