rns

react-native-scoped-storage

react-native-scoped-storage

Showing:

Popularity

Downloads/wk

86

GitHub Stars

20

Maintenance

Last Commit

5d ago

Contributors

3

Package

Dependencies

0

License

MIT

Type Definitions

Tree-Shakeable

No?

Categories

Readme

react-native-scoped-storage

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.

Getting started

Install the library

yarn add react-native-scoped-storage

or

npm install react-native-scoped-storage

Table of contents

Type aliases

Functions

Type aliases

FileType

Ƭ FileType: object

Type declaration

NameTypeDescription
datastringdata read from the file
lastModifiednumberLast modified date of the file or directory
mimestringmime type of the file
namestringName of the file or directory
pathstringStorage path for the file
type"file" | "directory"-
uristringDocument Tree Uri for the file or directory

Defined in: index.tsx:5

Functions

createDirectory

createDirectory(path: string, dirName: string): Promise<FileType>

Create a new directory under a parent uri.

Parameters

NameTypeDescription
pathstringUri of the parent directory
dirNamestringName of the directory to create

Returns: Promise<FileType>

Defined in: index.tsx:111


createDocument

createDocument(fileName: string, mime: string, data: string, encoding: "utf8" | "base64" | "ascii"): Promise<FileType>

Open Document picker to create a file at the user specified location.

Parameters

NameTypeDescription
fileNamestringName of the file to create.
mimestringmime of the file to create. eg image/jpeg
datastringData to write to the file once it is created.
encoding"utf8" | "base64" | "ascii"Encoding of the dat you are writing.

Returns: Promise<FileType>

Defined in: index.tsx:37


deleteFile

deleteFile(uri: string): Promise<boolean>

Delete a file or directory at the given path.

Parameters

NameTypeDescription
uristringPath to the file or directory to delete

Returns: Promise<boolean>

Defined in: index.tsx:99


getPersistedUriPermissions

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.

Returns: Promise<string[]>

Defined in: index.tsx:54


listFiles

listFiles(uri: string): Promise<FileType[]>

List all files and folders in a directory uri

Parameters

NameTypeDescription
uristringPath to a directory.

Returns: Promise<FileType[]>

Defined in: index.tsx:72


openDocument

openDocument(readData: boolean): Promise<FileType>

Open Document picker for the user to select a file.

Parameters

NameTypeDescription
readDatabooleanDo you want to read data from the user specified file?

Returns: Promise<FileType>

Defined in: index.tsx:46


openDocumentTree

openDocumentTree(): Promise<FileType>

Open the Document Picker to select a folder. Read/Write Permission will be granted to the selected folder.

Returns: Promise<FileType>

Defined in: index.tsx:25


readFile

readFile(uri: string): Promise<string>

Read file at a given path. The path of the file must be a document tree uri.

Parameters

NameTypeDescription
uristringPath to the file you want to read.

Returns: Promise<string>

Defined in: index.tsx:81


releasePersistableUriPermission

releasePersistableUriPermission(uri: string): Promise<void>

Remove a uri from persisted uri list.

Parameters

NameTypeDescription
uristringThe uri you want to remove from persisted uri permissions.

Returns: Promise<void>

Defined in: index.tsx:63


rename

rename(uri: string, name: string): Promise<string>

Rename a file or directory at the given path.

Parameters

NameTypeDescription
uristringPath to the file or directory to rename
namestringNew name for the file or directory

Returns: Promise<string>

Defined in: index.tsx:91


writeFile

writeFile(path: string, fileName: string, mime: string, data: string, encoding: "utf8" | "base64" | "ascii", append: boolean): Promise<boolean>

Write to a file at the given directory. If the file does not exist, it will be created.

Parameters

NameTypeDescription
pathstringUri of the directory
fileNamestringName of the file
mimestringMime of the file. eg image/jpeg
datastringData you want to write
encoding"utf8" | "base64" | "ascii"Encoding of the data you are writing.
appendbooleanShould the data be appended to the existing data in file?

Returns: Promise<boolean>

Defined in: index.tsx:112

Thanks to

  • rn-fetch-blob for the amazing library. Some part of code is taken from there.

I want to contribute

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

License

This library is licensed under the MIT license.

Copyright © Ammar Ahmed (@ammarahm-ed)

Notesnook Logo

Rate & Review

Great Documentation0
Easy to Use0
Performant0
Highly Customizable0
Bleeding Edge0
Responsive Maintainers0
Poor Documentation0
Hard to Use0
Slow0
Buggy0
Abandoned0
Unwelcoming Community0
100