# File storage in custom directory?

**URL:** <https://forum.ionicframework.com/t/file-storage-in-custom-directory/208868>\
**Category:** Ionic Framework\
**Created:** [April 29, 2021, 3:40pm UTC](https://forum.ionicframework.com/t/file-storage-in-custom-directory/208868 "2021-04-29T15:40:13Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![cpper](https://avatars.discourse-cdn.com/v4/letter/c/f6c823/32.png) [@cpper](https://forum.ionicframework.com/u/cpper)\
**Post date:** [April 29, 2021, 3:40pm UTC](https://forum.ionicframework.com/t/file-storage-in-custom-directory/208868/1 "2021-04-29T15:40:13Z")

</div>

Using the Capacitor Filesystem API, can I save files in any directory on my device ?  
Something like:

```auto
Filesystem.writeFile({
        data: 'testcontent', 
        path: 'file://storage/emulated/0/anydir/testfile.txt',
        encoding: Encoding.UTF8,
        recursive: true
)}

```

I was only able to save files in `file:///storage/emulated/0/Android/data/io.ionic.starter/files` using the following code:

```auto
Filesystem.writeFile({
        data: 'testcontent',
        directory: Directory.External,
        path: 'anydir/testfile.txt',
        encoding: Encoding.UTF8,
        recursive: true
)}

```

with the file being saved in `file:///storage/emulated/0/Android/data/io.ionic.starter/files/anydir/testfile.txt`

---

<div class="post-metadata">

**Author:** ![rapropos](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/rapropos/32/100227_2.png) [@rapropos](https://forum.ionicframework.com/u/rapropos)\
**Post date:** [April 29, 2021, 5:28pm UTC](https://forum.ionicframework.com/t/file-storage-in-custom-directory/208868/2 "2021-04-29T17:28:43Z")

</div>

> [@cpper](#):
>
> can I save files in any directory on my device ?

No. It would be anarchy if a given application could stomp all over the files from other applications. The details of what directories are accessible how is OS-specific.

---

<div class="post-metadata">

**Author:** ![cpper](https://avatars.discourse-cdn.com/v4/letter/c/f6c823/32.png) [@cpper](https://forum.ionicframework.com/u/cpper)\
**Post date:** [April 30, 2021, 2:31pm UTC](https://forum.ionicframework.com/t/file-storage-in-custom-directory/208868/3 "2021-04-30T14:31:11Z")

</div>

But with the right Capacitor plugin (Java code) it should be possible, right ? I mean in most apps that create data, you can select the specific directory in which you want to save the files.

---

<div class="post-metadata">

**Author:** ![mhartington](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/mhartington/32/137757_2.png) [@mhartington](https://forum.ionicframework.com/u/mhartington)\
**Post date:** [April 30, 2021, 3:10pm UTC](https://forum.ionicframework.com/t/file-storage-in-custom-directory/208868/4 "2021-04-30T15:10:16Z")

</div>

Not 100% true. Apps these days create their own sandbox storage systems. So when you save something to the “documents” it’s typically a special directory that is isolated to inside the sandbox of that app.

Unlimited access to the file-system is …while not going to fly, especially in todays world of tighter security and needing permissions for things.

---

<div class="post-metadata">

**Author:** ![jcesarmobile](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/jcesarmobile/32/127095_2.png) [@jcesarmobile](https://forum.ionicframework.com/u/jcesarmobile)\
**Post date:** [May 4, 2021, 5:41pm UTC](https://forum.ionicframework.com/t/file-storage-in-custom-directory/208868/5 "2021-05-04T17:41:40Z")

</div>

Filesystem plugin allows to use absolute file urls, or relative if you pass the directory.

If you don’t pass the directory and use the absolute file url it might work depending on the path, it’s possible to read/write on the paths that “belong” to your app, and some other public “shared” folders.

But those public “shared” folders are no longer available on Android 10 unless you enable the legacy filesystem, and no longer available on Android 11+ even if you enabled the legacy filesystem.

On Android 11+, might be possible to write everywhere if you add (and request) the `MANAGE_EXTERNAL_STORAGE` permission, which Filesystem plugin doesn’t request.

All the above is just for Android, on iOS there are no public shared folder you can write to, you can only read/write inside your app sandbox. I think in Capacitor 2 the file urls won’t work on iOS, but they will in Capacitor 3 with @capacitor/filesystem plugin.

Your code is probably not working because you have used `file://`, while the path should start with `file:///`

---

<div class="post-metadata">

**Author:** ![arminzia](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/arminzia/32/96939_2.png) [@arminzia](https://forum.ionicframework.com/u/arminzia)\
**Post date:** [June 10, 2021, 2:00am UTC](https://forum.ionicframework.com/t/file-storage-in-custom-directory/208868/6 "2021-06-10T02:00:06Z")

</div>

How can I make this work? I’ve been developing Ionic apps since v1.0 and right now I’m working on a new project using Ionic 6.x, Angular 12, and Capacitor 3. Everything’s been great at first, but until I started to integrate some native access code.

I’m using the cordova-media-capture plugin for capturing audio/video files. The plugin works fine and returns the MediaFile array. But I’m unable to get access to those files to read their contents for uploads. I think the issue is exactly what you pointed out, the new security restrictions in Android 10+.

I’m using a Xiamo Redmi Note 10 which runs Android 11.  
The full path returned in the MediaFile response is something like this:

`file:///storage/emulated/0/Android/data/com.android.soundrecorder/files/Jun%2010%2C%2006.21.aac`  
I’ve tried everything I could think of, nothing works and I’m incredibly frustrated I need help.  
I tried reading the file using the cordova-file plugin, Capacitor Filesystem plugin, etc.

Any ideas? Thanks in advance.

---

<div class="post-metadata">

**Author:** ![cpper](https://avatars.discourse-cdn.com/v4/letter/c/f6c823/32.png) [@cpper](https://forum.ionicframework.com/u/cpper)\
**Post date:** [June 10, 2021, 9:21am UTC](https://forum.ionicframework.com/t/file-storage-in-custom-directory/208868/7 "2021-06-10T09:21:34Z")

</div>

@arminzia , you should be able to access files located in that folder, using the FileSystem API. The problem was accessing any arbitrary folder. I also ended up using the folder you are referring to (Android/data/package.name/files):

```auto
async getFileList() {
    const res = await Filesystem.readdir({
      path: '',
      directory: Directory.External,
    });
    return res.files;
}

async readFile(path: string) { //path = 'myfile.txt'
    const res = await Filesystem.readFile({
      path: path,
      directory: Directory.External,
      encoding: Encoding.UTF8
    });
    return res.data;
}

```

---

<div class="post-metadata">

**Author:** ![jcesarmobile](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/jcesarmobile/32/127095_2.png) [@jcesarmobile](https://forum.ionicframework.com/u/jcesarmobile)\
**Post date:** [June 10, 2021, 1:08pm UTC](https://forum.ionicframework.com/t/file-storage-in-custom-directory/208868/8 "2021-06-10T13:08:01Z")

</div>

the plugin needs to be updated to support Android 11+, those locations are no longer available from file urls, so the plugin will have to use newer APIs that allow media files choosing.

---

<div class="post-metadata">

**Author:** ![arminzia](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/arminzia/32/96939_2.png) [@arminzia](https://forum.ionicframework.com/u/arminzia)\
**Post date:** [June 10, 2021, 3:08pm UTC](https://forum.ionicframework.com/t/file-storage-in-custom-directory/208868/9 "2021-06-10T15:08:02Z")

</div>

Thank you, didn’t work. The output files from the media-capture plugin have paths like the one I mentioned. When I try to read the files using cordova-file or the capacitor’s Filesystem plugin, they both say the file doesn’t exist. I think it’s the Android 11 issue, the plugins need an update. How incredibly frustrating!

---

<div class="post-metadata">

**Author:** ![arminzia](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/arminzia/32/96939_2.png) [@arminzia](https://forum.ionicframework.com/u/arminzia)\
**Post date:** [June 10, 2021, 3:10pm UTC](https://forum.ionicframework.com/t/file-storage-in-custom-directory/208868/10 "2021-06-10T15:10:16Z")

</div>

Thank you. Is there a workaround or any other solution?

For our project, we only need some basic native access, capturing audio, video, and images.  
The Capacitor Camera plugin works very well for images. But I’ve been struggling with audio/video captures. The cordova plugins used to work fine before Android 10, now it’s a dead-end it seems.

I can only hope the Capacitor team roles out official plugins for the essentials eventually.

---

<div class="post-metadata">

**Author:** ![creatxr](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/creatxr/32/142834_2.png) [@creatxr](https://forum.ionicframework.com/u/creatxr)\
**Post date:** [November 2, 2022, 2:50pm UTC](https://forum.ionicframework.com/t/file-storage-in-custom-directory/208868/11 "2022-11-02T14:50:49Z")

</div>

I’ve a question: is it only for device not desktop? (@capacitor/filesystem)

I run my app with command:

```auto
ionic serve

```

getFileList() will return a empty array.

getUri() will return a path with with leading “/EXTERNAL/”

thanks.
