A module to help simplify usage of scoped storages on android.
intended to use when targeting Android API level 30+
Minimum supported API level is 24
npm install react-native-saf-x
or
yarn add react-native-saf-x
After receiving a content uri using openDocumentTree
function,
you can use this as if like it's a normal path like we are used to.
This is the intended behaviour,
if you find any problems or if something does not behave as intended,
please create an issue.
Note that each method can reject when there's an unexpected error or when the permission is revoked.
Example:
import { openDocumentTree, mkdir } from "react-native-saf-x";
// somewhere in the app .....
async function testIt() {
const doc = await openDocumentTree(true);
if (doc && doc.uri) {
// user has selected a directory and uri is available
// you can save this uri as base directory in your app and reuse it anywhere you want
await mkdir(doc.uri + '/foo/bar'); // creates foo/bar folder and subfolder at selected directory
}
}
For more examples look at example folder in the source.
- copyFile
- createDocument
- createFile
- exists
- getPersistedUriPermissions
- hasPermission
- listFiles
- mkdir
- moveFile
- openDocument
- openDocumentTree
- readFile
- releasePersistableUriPermission
- rename
- stat
- unlink
- writeFile
Ƭ CreateDocumentOptions: FileOperationOptions
& { initialName?
: string
}
Ƭ DocumentFileDetail: Object
Name | Type |
---|---|
lastModified |
number |
mime |
string |
name |
string |
size |
number |
type |
"directory" | "file" |
uri |
string |
Ƭ Encoding: "utf8"
| "base64"
| "ascii"
Ƭ FileOperationOptions: Object
Name | Type | Description |
---|---|---|
append? |
boolean |
Append data to the file. If not set file content will be overwritten. |
encoding? |
Encoding |
Defaults to 'utf8' |
mimeType? |
string |
mime type of the file being saved. Defaults to '*/*' |
Ƭ OpenDocumentOptions: Object
Name | Type | Description |
---|---|---|
multiple? |
boolean |
should the file picker allow multiple documents ? |
persist? |
boolean |
should the permission of returned document(s) be persisted ? |
• default: Object
Name | Type |
---|---|
copyFile |
(srcUri : string , destUri : string , options? : FileTransferOptions ) => Promise <null | DocumentFileDetail > |
createDocument |
(data : string , options? : CreateDocumentOptions ) => Promise <null | DocumentFileDetail > |
createFile |
(uriString : string , options? : Pick <FileOperationOptions , "mimeType" >) => Promise <DocumentFileDetail > |
exists |
(uriString : string ) => Promise <boolean > |
getPersistedUriPermissions |
() => Promise <string []> |
hasPermission |
(uriString : string ) => Promise <boolean > |
listFiles |
(uriString : string ) => Promise <DocumentFileDetail []> |
mkdir |
(uriString : string ) => Promise <DocumentFileDetail > |
moveFile |
(srcUri : string , destUri : string , options? : FileTransferOptions ) => Promise <null | DocumentFileDetail > |
openDocument |
(options : OpenDocumentOptions ) => Promise <null | DocumentFileDetail []> |
openDocumentTree |
(persist : boolean ) => Promise <null | DocumentFileDetail > |
readFile |
(uriString : string , options? : Pick <FileOperationOptions , "encoding" >) => Promise <string > |
releasePersistableUriPermission |
(uriString : string ) => Promise <void > |
rename |
(uriString : string , newName : string ) => Promise <DocumentFileDetail > |
stat |
(uriString : string ) => Promise <DocumentFileDetail > |
unlink |
(uriString : string ) => Promise <boolean > |
writeFile |
(uriString : string , data : string , options? : FileOperationOptions ) => Promise <void > |
▸ copyFile(srcUri
, destUri
, options?
): Promise
<null
| DocumentFileDetail
>
Copy file from source uri to destination uri.
promise Rejects if destination already exists and replaceIfDestinationExists
option is not set to true.
Does not support moving directories.
Name | Type |
---|---|
srcUri |
string |
destUri |
string |
options? |
FileTransferOptions |
Promise
<null
| DocumentFileDetail
>
▸ createDocument(data
, options?
): Promise
<null
| DocumentFileDetail
>
Open the Document Picker to save a file.
Returns an object of type DocumentFileDetail
or null
if user did not select a file.
Name | Type |
---|---|
data |
string |
options? |
CreateDocumentOptions |
Promise
<null
| DocumentFileDetail
>
▸ createFile(uriString
, options?
): Promise
<DocumentFileDetail
>
Creates an empty file at given uri. Rejects if a file or directory exist at given uri.
Name | Type |
---|---|
uriString |
string |
options? |
Pick <FileOperationOptions , "mimeType" > |
Promise
<DocumentFileDetail
>
▸ exists(uriString
): Promise
<boolean
>
Check if there's a document located at the given uri.
Name | Type |
---|---|
uriString |
string |
Promise
<boolean
>
▸ getPersistedUriPermissions(): Promise
<string
[]>
Returns a list of all the persisted uri permissions.
Promise
<string
[]>
▸ hasPermission(uriString
): Promise
<boolean
>
Check if you have permission to access the uri.
Name | Type |
---|---|
uriString |
string |
Promise
<boolean
>
▸ listFiles(uriString
): Promise
<DocumentFileDetail
[]>
List all files and folders in a directory uri.
Name | Type |
---|---|
uriString |
string |
Promise
<DocumentFileDetail
[]>
▸ mkdir(uriString
): Promise
<DocumentFileDetail
>
Create a directory at given uri. Automatically creates folders in path if needed. You can use it to create nested directories easily. Rejects if it fails.
Name | Type |
---|---|
uriString |
string |
Promise
<DocumentFileDetail
>
▸ moveFile(srcUri
, destUri
, options?
): Promise
<null
| DocumentFileDetail
>
Move file from source uri to destination uri.
promise Rejects if destination already exists and replaceIfDestinationExists
option is not set to true.
Does not support moving directories.
Name | Type |
---|---|
srcUri |
string |
destUri |
string |
options? |
FileTransferOptions |
Promise
<null
| DocumentFileDetail
>
▸ openDocument(options
): Promise
<null
| DocumentFileDetail
[]>
Open the Document Picker to select a file. DocumentFileDetail is always an array.
Name | Type |
---|---|
options |
OpenDocumentOptions |
Promise
<null
| DocumentFileDetail
[]>
DocumentFileDetail[]
or null
if user did not select a file.
▸ openDocumentTree(persist
): Promise
<null
| DocumentFileDetail
>
Open the Document Picker to select a folder. Read/Write Permission will be granted to the selected folder.
Returns an object of type DocumentFileDetail
or null
if user did not select a folder.
Name | Type |
---|---|
persist |
boolean |
Promise
<null
| DocumentFileDetail
>
▸ readFile(uriString
, options?
): Promise
<string
>
Read contents of the given uri. uri must point to a file.
Name | Type |
---|---|
uriString |
string |
options? |
Pick <FileOperationOptions , "encoding" > |
Promise
<string
>
▸ releasePersistableUriPermission(uriString
): Promise
<void
>
Remove a uri from persisted uri permissions list.
Name | Type |
---|---|
uriString |
string |
Promise
<void
>
▸ rename(uriString
, newName
): Promise
<DocumentFileDetail
>
Renames the document at given uri.
uri can be file or folder.
Resolves with true
if successful and false
otherwise.
Name | Type |
---|---|
uriString |
string |
newName |
string |
Promise
<DocumentFileDetail
>
▸ stat(uriString
): Promise
<DocumentFileDetail
>
Get details for a file/directory at given uri.
Name | Type |
---|---|
uriString |
string |
Promise
<DocumentFileDetail
>
▸ unlink(uriString
): Promise
<boolean
>
Removes the file or directory at given uri.
Resolves with true
if delete is successful, throws otherwise.
Name | Type |
---|---|
uriString |
string |
Promise
<boolean
>
▸ writeFile(uriString
, data
, options?
): Promise
<void
>
Writes the given data to the file at given uri. Tries to create the file if does not already exist before writing to it. Resolves with given uriString if successful.
Name | Type |
---|---|
uriString |
string |
data |
string |
options? |
FileOperationOptions |
Promise
<void
>
Due to simplyifing the uri structure usage, file and directories should not have the same name at a given uri. doing so can cause unexpected results. for example in a folder named "foo", do not create "bar" file and "bar" directory making the uri being "foo/bar" for both cases.
- ammarahm-ed for his work on react-native-scoped-storage which helped me jump start this module.
See the contributing guide to learn how to contribute to the repository and the development workflow.
MIT