简体   繁体   English

在flutter android 11中使用File writeAsBytes时出错有没有其他方法可以工作?

[英]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.

相关问题 使用Flutter,在Android版本中运行谷歌地图少于20时,它不起作用..有什么想法吗? - Using Flutter, when running Google map in Android version less than 20, it does not work .. Is there any idea? Android 错误:二进制 XML 文件第 11 行:使用地图片段时错误膨胀类片段 - Android Error: Binary XML file line #11: Error inflating class fragment when using map fragment 获取 Android 中文件的文件路径时出错 11 - Error when get file path of file in Android 11 有什么方法可以让文件选择在Android 4.4上的Webview中工作? - Is There Any Way to Get a File Select To Work in Webview on Android 4.4? Android 版本 11 上的 flutter 面临错误 - facing error in flutter on Android version 11 Flutter 是否适用于任何 Android 平台版本? - Does Flutter work on any Android platform version? 除了使用trafficstats API之外,是否还有其他方法可以在android中查找数据使用情况 - Is there any other way to find data usage in android other than using trafficstats API's 我尝试在 Android 11 中读取文件时出现电容器文件系统错误 - Capacitor Filesystem error when I try to read a file in Android 11 有没有办法旋转在Android中使用PdfDocument画布创建的PDF文件 - Is there any way to rotate a PDF file created using PdfDocument canvas in android 使用IntelliJ IDEA时,是否有任何简便的方法来混淆Android应用程序? - Is there any easy way to obfuscate an Android app when using IntelliJ IDEA?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM