@capacitor/filesystem android 13 storage permission issue

I am working on an Ionic React app where I need to download a certificate image. Recently, we encountered a bug where the certificate is not downloading and gives an error. Upon investigating, we found that this issue only occurs in Android 13 (compileSdkVersion 33). It could possibly be related to permissions in Android 13. I have read many forums and blogs on this topic, but I haven’t found any feasible solutions yet.

AndroidManifest.xml permissions

Permissions →

uses-permission android:name=“android.permission.READ_EXTERNAL_STORAGE”/>
uses-permission android:name=“android.permission.WRITE_EXTERNAL_STORAGE” />
uses-permission android:name=“android.permission.INTERNET” />

and Here is the code snippet

       const base64Data = await base64FromPath(res.data);
       const savedFile = await Filesystem.writeFile({
          path: `Report_of_authenticity_${orderDetail.jobId}.png`,
          data: base64Data,
          directory: Directory.Cache,
        });

        console.log("saved file cert", savedFile);

        const albumName = "TEST";
        let albumIdentifier: any = "";
        if (isPlatform("ios")) {
          // Handle albums
          let albums = await Media.getAlbums();
          albumIdentifier =
            albums.albums.find((a) => a.name === albumName) || null;

          if (!albumIdentifier) {
            // Doesn't exist, create new album
            await Media.createAlbum({ name: albumName });
            albums = await Media.getAlbums();
            albumIdentifier = albums.albums.find(
              (a) => a.name === albumName
            );
          }
        }
        // else if (isPlatform("android")) {
        //   await Media.createAlbum({ name: albumName });
        // }
        console.log("cert albumIdentifier", albumIdentifier);
        Media.savePhoto({
          path: savedFile.uri,
          album: isPlatform("ios")
            ? albumIdentifier
            : {
                name: albumName,
              },
        })
          .then(() => {
            console.log("cert Image has been saved");
            present("File has been downloaded", TOAST_TIMER);
            setIsDownloadingCert(false);
          })
          .catch((e) => {
            console.error("cert error", e);
            present("Error downloading file", TOAST_TIMER);
            setIsDownloadingCert(false);
          });

Error Is=>
Error: Unable to do file operation, user denied permission request

First off, please use proper code blocks.

Assuming you are on Capacitor 5?

I don’t see any code requesting permissions requestPermissions().

package.json capacitor version -
@capacitor-community/native-market”: “^4.0.0”,
@capacitor/android”: “^3.4.1”,
@capacitor/app”: “^1.1.0”,
@capacitor/core”: “^3.4.1”,
@capacitor/filesystem”: “^1.1.0”,
@capacitor/haptics”: “1.1.4”,
@capacitor/ios”: “^3.4.3”,
@capacitor/keyboard”: “1.2.2”,
@capacitor/push-notifications”: “^1.0.9”,
@capacitor/status-bar”: “1.0.8”,
as i am on a old project so i am not on a latest capacitor version

I also added Filesystem.requestPermisssions() but it doesn’t work

For properly supporting Android 13 you have to use capacitor 5 and version 5 of capacitor plugins

3 Likes

Can it break existing features

I upgraded to capacitor 5 now but still it is not working
Here is my package.json
@capacitor/android”: “^5.0.0”,
@capacitor/app”: “^5.0.0”,
@capacitor/core”: “^5.0.0”,
@capacitor/filesystem”: “^5.0.0”,
@capacitor/haptics”: “^5.0.0”,
@capacitor/ios”: “^5.0.0”,
@capacitor/keyboard”: “^5.0.0”,
@capacitor/push-notifications”: “^5.0.0”,
@capacitor/status-bar”: “^5.0.0”,

Looks like you are also using @capacitor-community/media plugin or other Media plugin by the code you’ve shown, but you don’t have it in the package.json you have shown.
Make sure that plugin is also in version 5.x.

1 Like

Thank you for your help Now its working.