Starting in Android 11, apps that use the scoped storage model can access only their own app-specific cache files. As soon as React Native targets API 30, all of us will have to use Scoped Storage to access any files stored in the sdcard/phone storage. This library provides an API for react-native devs to start testing out scoped storage in their apps. Remember, you do not need WRITE_EXTERNAL_STORAGE
permission in you AndroidManifest.xml
file using this library.
Scoped storage allows you to prompt the user that you need access to some file/folders. The user can the allow access to their desired location. It is your responsibility to store the uri you recieve for later use.
Important note: Until React Native targets API 29, you do not need this library. However you should start testing it out in your apps/projects because soon, we will have to move to this new API for file access.
Install the library
yarn add react-native-scoped-storage
or
npm install react-native-scoped-storage
- createDirectory
- createDocument
- createFile
- deleteFile
- getPersistedUriPermissions
- listFiles
- openDocument
- openDocumentTree
- readFile
- releasePersistableUriPermission
- rename
- stat
- writeFile
Ƭ FileType: Object
Name | Type | Description |
---|---|---|
data |
string |
data read from the file |
lastModified |
number |
Last modified date of the file or directory |
mime |
string |
mime type of the file |
name |
string |
Name of the file or directory |
path |
string |
Storage path for the file |
type |
"file" | "directory" |
- |
uri |
string |
Document Tree Uri for the file or directory |
▸ createDirectory(path
, dirName
): Promise
<FileType
>
Create a directory at the given path.
Name | Type | Description |
---|---|---|
path |
string |
Uri of the parent directory |
dirName |
string |
Name of the new directory |
Promise
<FileType
>
▸ createDocument(fileName
, mime
, data
, encoding
): Promise
<FileType
>
Open Document picker to create a file at the user specified location.
Name | Type | Description |
---|---|---|
fileName |
string |
Name of the file to create. |
mime |
string |
mime of the file to create. eg image/jpeg |
data |
string |
Data to write to the file once it is created. |
encoding |
"utf8" | "base64" | "ascii" |
Encoding of the dat you are writing. |
Promise
<FileType
>
▸ createFile(path
, fileName
, mime
): Promise
<FileType
>
Create a new file at the given directory.
Name | Type | Description |
---|---|---|
path |
string |
Uri of the parent directory |
fileName |
string |
Name of the new file. |
mime |
string |
Mime type of the file, e.g. image/jpeg |
Promise
<FileType
>
▸ deleteFile(uri
): Promise
<boolean
>
Delete a file or directory at the given path.
Name | Type | Description |
---|---|---|
uri |
string |
Path to the file or directory to delete |
Promise
<boolean
>
▸ getPersistedUriPermissions(): Promise
<string
[]>
There is a limit to the number of uri permissions your app can persist. Get a list of all the persisted document tree uris so you are remove the ones you are not using or perform other operations.
Promise
<string
[]>
▸ listFiles(uri
): Promise
<FileType
[]>
List all files and folders in a directory uri
Name | Type | Description |
---|---|---|
uri |
string |
Path to a directory. |
Promise
<FileType
[]>
▸ openDocument(readData
, encoding
): Promise
<FileType
>
Open Document picker for the user to select a file.
Name | Type | Description |
---|---|---|
readData |
boolean |
Do you want to read data from the user specified file? |
encoding |
"utf8" | "base64" | "ascii" |
Encoding for the file you are reading. |
Promise
<FileType
>
▸ openDocumentTree(persist
): Promise
<FileType
>
Open the Document Picker to select a folder. Read/Write Permission will be granted to the selected folder.
Name | Type |
---|---|
persist |
boolean |
Promise
<FileType
>
▸ readFile(uri
, encoding
): Promise
<string
>
Read file at a given path. The path of the file must be a document tree uri.
Name | Type | Description |
---|---|---|
uri |
string |
Path to the file you want to read. |
encoding |
"utf8" | "base64" | "ascii" |
Encoding for the file you are reading. |
Promise
<string
>
▸ releasePersistableUriPermission(uri
): Promise
<void
>
Remove a uri from persisted uri list.
Name | Type | Description |
---|---|---|
uri |
string |
The uri you want to remove from persisted uri permissions. |
Promise
<void
>
▸ rename(uri
, name
): Promise
<string
>
Rename a file or directory at the given path.
Name | Type | Description |
---|---|---|
uri |
string |
Path to the file or directory to rename |
name |
string |
New name for the file or directory |
Promise
<string
>
▸ stat(path
): Promise
<any
>
Get details for a file/directory at a given uri.
Name | Type | Description |
---|---|---|
path |
string |
Uri of the parent directory |
Promise
<any
>
▸ writeFile(path
, fileName
, mime
, data
, encoding
, append
): Promise
<string
>
Write to a file at the give directory. If the file does not exist, it will be created.
Name | Type | Description |
---|---|---|
path |
string |
Uri of the directory |
fileName |
string |
Name of the file |
mime |
string |
Mime of the file. eg image/jpeg |
data |
string |
Data you want to write |
encoding |
"utf8" | "base64" | "ascii" |
Encoding of the data you are writing. |
append |
boolean |
Should the data be appended to the existing data in file? |
Promise
<string
>
- rn-fetch-blob for the amazing library. Some part of code is taken from there.
That is awesome news! There is alot happening at a very fast pace in this library right now. Every little help is precious. You can contribute in many ways:
- Suggest code improvements on native iOS and Android
- If you have suggestion or idea you want to discuss, open an issue.
- Open an issue if you want to make a pull request, and tell me what you want to improve or add so we can discuss
This library is licensed under the MIT license.
Copyright © Ammar Ahmed (@ammarahm-ed)