GithubHelp home page GithubHelp logo

penghouho / react-native-scoped-storage Goto Github PK

View Code? Open in Web Editor NEW

This project forked from ammarahm-ed/react-native-scoped-storage

0.0 0.0 0.0 332 KB

License: MIT License

Java 77.67% TypeScript 11.24% JavaScript 1.30% Starlark 1.01% Ruby 1.23% Objective-C 7.55%

react-native-scoped-storage's Introduction

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

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

Defined in: index.tsx:5

Functions

createDirectory

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

Create a new directory under a parent uri.

Parameters

Name Type Description
path string Uri of the parent directory
dirName string Name 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

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.

Returns: Promise<FileType>

Defined in: index.tsx:37


deleteFile

deleteFile(uri: string): Promise<boolean>

Delete a file or directory at the given path.

Parameters

Name Type Description
uri string Path 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

Name Type Description
uri string Path 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

Name Type Description
readData boolean Do 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

Name Type Description
uri string Path 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

Name Type Description
uri string The 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

Name Type Description
uri string Path to the file or directory to rename
name string New 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

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?

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

react-native-scoped-storage's People

Contributors

ammarahm-ed avatar eduardofrausto avatar rembo10 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.