Error on Download pdf with flutter only with android sdk +30

  Kiến thức lập trình

I followed a lot of tutorials and threads but I am not sure where my problem

I could download the pdf with old Android or with ios but with the new Android version I got a problem

class GenerationService {
  static Future<bool> _requestPermissions() async {
    if (await Permission.storage.request().isGranted) {
      return true;
    } else {
      await Permission.photos.request();
      return await Permission.manageExternalStorage.request().isGranted;
    }
  }

  static Future<Map<String, dynamic>> generate(Statement statement) async {
    if (await _requestPermissions()) {
      Directory? dir;
      File? file;

      try {
        if (Platform.isIOS) {
          dir = await getApplicationDocumentsDirectory();
        } else {
          final dirDownloadExists = await Directory("/storage/emulated/0/Download/").exists();

          if (dirDownloadExists) {
            dir = Directory("/storage/emulated/0/Download/Knoon");
          } else {
            dir = Directory("/storage/emulated/0/Downloads/Knoon");
          }
        }
      } catch (err) {
        debugPrint("error : Cannot get download folder path $err");
        return {
          'status': false,
          'message': 'Cannot get download folder path',
        };
      }

      ...
      final savePath = "${dir.path}/$saveName";

      try {
        ...

        if (response.statusCode == 200) {
          file = File(savePath);
          if (!file.existsSync()) {
            await file.create();
          }
          await file.writeAsBytes(response.bodyBytes);
          debugPrint("File downloaded to : $savePath");
          await OpenFile.open(savePath, type: 'application/pdf');
          return {
            'status': true,
            'filePath': savePath,
            'message': 'File Downloaded',
          };
        } else {
          return ...
        }
      } catch (e) {
        if (file!.existsSync()) {
          file.deleteSync();
        }
        return ...
      }
    } else {
      return ...
    }
  }
}

and this is android manifest

<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="xx.xx.xx">
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.INTERNET" />

    <uses-permission android:name="android.permission.READ_MEDIA_IMAGES" />


    <application
        android:label="K-noon"
        android:name="${applicationName}"
        android:icon="@mipmap/icon"
        android:requestLegacyExternalStorage="true"
        android:enableOnBackInvokedCallback="true">
        <activity
            android:name=".MainActivity"
            android:exported="true"
...

and finally when I download the function writebytes does not work and I have

I/flutter (21216): Error: PathNotFoundException: Cannot create file, path = ‘/storage/emulated/0/Download/Knoon/2024-Q1-statement-2024-06-11.pdf’ (OS Error: No such file or directory, errno = 2)

LEAVE A COMMENT