Implementing PDF generation and then From Local Notification Open the Pdf that is Saved to Download

21 Views Asked by At

I am implementing the pdf generation then while I hit save in android 7 device its working fine but as soon as I move to any upper versions I got this error Cannot open file, path = '/storage/emulated/0/Download/BitesVilla-invoice.pdf' (OS Error: Permission denied, errno = 13) Have tried Using permission Handler that DoesNot work also As am testing in a android 13 device. the permission status is always Denied. Tried changing the manifest.xml that does not help also. am attaching my code snippet and manifest setup . Some one please help me fix the issue.

This is my save file snippet where the problem Occurs

  Future<void> _saveAsFile(
    BuildContext context,
    LayoutCallback build,
    PdfPageFormat pageFormat,
  ) async {
    // DeviceInfoPlugin deviceInfo = DeviceInfoPlugin();
    // AndroidDeviceInfo androidInfo = await deviceInfo.androidInfo;
    // try {
    final bytes = await build(pageFormat);

    Directory dir = Directory('/storage/emulated/0/Download');
    logger.w("Executing");
    File savepath = File("${dir.path}/BitesVilla-invoice.pdf");
    logger.w("Executing this much ");
    await savepath.writeAsBytes(bytes);
    logger.w("Failure");

    ScaffoldMessenger.of(context).showSnackBar(
      SnackBar(
        content: Text('PDF saved to Downloads'),
        duration: Duration(seconds: 2),
      ),
    );
    await showNotification(context, savepath);
    // } catch (e) {
    //   logger.e(e);
    //   print("error in this: $e");
    // }
  }

this is my manifest


    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.CAMERA" />
    <uses-permission android:name="android.permission.POST_NOTIFICATIONS"/>

This is my appLevel Build.gradle

android {
    compileSdkVersion 33

    sourceSets {
        main.java.srcDirs += 'src/main/kotlin'
    }

    defaultConfig {
        applicationId "appID"
        minSdkVersion 20
        targetSdkVersion 33
        versionCode 9
        versionName '2.0.0'
        multiDexEnabled true
    }

the pdf is not saving . please help

I have tried Using Permission Handler by this code also its not letting me enable the storage permission but as per android 13 it should not ask me storage permission as I am specifying Download Directory

 // final value = await Permission.manageExternalStorage.request();
      // logger.w("External Storage Value: $value");
      // final status = await Permission.storage.status;
      // logger.w("Permission: $status");
      // if (Platform.isAndroid && androidInfo.version.sdkInt > 29) {
      //   final val = await Permission.manageExternalStorage.request();
      //   logger.w("Above 11: $val");
      // } else {
      //   final val = await Permission.storage.request();
      //   logger.w("Below 11: $val");
      // }

I have modified some of manifest.xml code from various stack overflow solution but nothing helps

0

There are 0 best solutions below