简体   繁体   中英

ionic capacitor filesystem api write a personal folder in internal storage

I need an information.

On my phone several apps creates a folder with the package name on internal storage. Those folders are browsable from pc.

I would like to create a folder in internal storage for my app to store some generated images and pdf.

Actually the file are stored in file:///data/user/0/[packageName]/files/temp.pdf but this folder is not accessible from pc.

If I use Documents folder I get this error Error: /storage/emulated/0/Documents/temp.pdf: open failed: EACCES (Permission denied) why? I've granted read and write on external storage

So... is it possible to create a personal folder on internal storage? I cannot find anything on the web...

this is my simple code

  const savedFile = await Filesystem.writeFile({
      path: fileName,
      data: base64Data,

      directory: FilesystemDirectory.Data
    });

thanks

With Capacitor FileSystem, you can choose where to write your file with the directory enum.

If you take a look at the docs here https://capacitorjs.com/docs/apis/filesystem#directory

You can see there are Directory.Data to hold applications file that will remove if you uninstall the app.

The Data directory On iOS it will use the Documents directory. On Android it's the directory holding application files. Files will be deleted when the application is uninstalled.

You can also use Directory.Cache to allow the system to erase them if user clears the cache in the settings. The following is working for me:

    const writeSecretFile = async () => {
    await Filesystem.writeFile({
        path: 'text.txt',
        data: "This is a test",
        directory: Directory.Cache,
        encoding: Encoding.UTF8,
    });
    };

    const readSecretFile = async () => {
    const contents = await Filesystem.readFile({
        path: 'text.txt',
        directory: Directory.Cache,
        encoding: Encoding.UTF8,
    });

    console.log('secrets:', JSON.stringify(contents));
    };

    writeSecretFile();
    readSecretFile();

I hope it helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM