skip to Main Content

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. However, when saving to the internal storage where the app is installed, the file is saved.

The current 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.

AndroidManifest.xml Application

<application
        android:label="projectName"
        android:icon="@mipmap/ic_launcher"
        android:usesCleartextTraffic="true"
        android:requestLegacyExternalStorage="true"
        android:preserveLegacyExternalStorage="true"
        >

AndroidManifest.xml permission

    <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?

3

Answers


  1. I think this happening in Android 11 due to their new changes. So you have to add one more permission (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.

    Login or Signup to reply.
  2. 2021-10-14T22:02:34.9847821.jpg

    Do not use forbidden characters like : in file names.

    Login or Signup to reply.
  3. I had the same problem and I solved it by delete
    : from code path

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search