tauri-plugin-android-fs-api
v29.0.0
Published
Android file system API for Tauri
Maintainers
Readme
Note: I’m using a translation tool, so some expressions may be awkward or inaccurate.
Overview
This plugin provides a unified file system API across all Android versions supported by Tauri.
Setup
First, install the plugin to your Tauri project:
src-tauri/Cargo.toml
[dependencies]
tauri-plugin-android-fs = {
version = "=29.0.0",
features = [
# To access public files on older Android versions
"legacy_storage_permission",
# To use notification options
"notification_permission"
]
}Next, register the plugin:
src-tauri/src/lib.rs
#[cfg_attr(mobile, tauri::mobile_entry_point)]
pub fn run() {
tauri::Builder::default()
.plugin(tauri_plugin_android_fs::init()) // This
.run(tauri::generate_context!())
.expect("error while running tauri application");
}Then, configure the APIs that can be called from the frontend JavaScript bindings:
src-tauri/capabilities/*.json
{
"permissions": [
"android-fs:default"
]
}Finally, install the frontend JavaScript bindings:
pnpm add [email protected] -E
# or
npm install [email protected] --save-exact
# or
yarn add [email protected] --exactNOTE: Please ensure that the backend package, tauri-plugin-android-fs (crates io), and the frontend package, tauri-plugin-android-fs-api (npm), have exactly matching versions.
Usage
This plugin operates on files and directories via URIs rather than paths.
When passing URIs to this plugin's functions, no scope configuration is required.
This is because the plugin only provides and accepts URIs whose permissions are already managed by the Android system, such as those explicitly selected by the user through a file picker or files created by the app in public directories.
Some functions accept not only URIs but also absolute paths. In this case, you need to set the scope configuration for security, like in plugin-fs.
You can set a global scope for the plugin, or assign specific scopes to individual commands:
src-tauri/capabilities/*.json
{
"permissions": [
{
"identifier": "android-fs:scope",
"allow": ["$APPDATA/my-data/**/*"],
"deny": ["$APPDATA/my-data/secret.txt"]
},
{
"identifier": "android-fs:allow-copy-file",
"allow": ["$APPDATA/my-data/**/*"]
}
]
}Example
import * as AndroidFs from 'tauri-plugin-android-fs-api';
/**
* Saves data to '~/Download/MyApp/{fileName}'
*/
async function download(
fileName: string,
mimeType: string,
data: Uint8Array | ReadableStream<Uint8Array>,
): Promise<void> {
let uri;
try {
// Creates a new empty file
uri = await AndroidFs.createNewPublicFile(
AndroidFs.PublicGeneralPurposeDir.Download,
`MyApp/${fileName}`,
mimeType,
{ isPending: true }
);
// Writes data to the file
if (data instanceof ReadableStream) {
const writer = await AndroidFs.openWriteFileStream(uri);
await data.pipeTo(writer);
}
else {
await AndroidFs.writeFile(uri, data);
}
// Makes the file visible in other apps and gallery
await AndroidFs.setPublicFilePending(uri, false);
await AndroidFs.scanPublicFile(uri);
}
// Handles an error and cleanup
catch (e) {
if (data instanceof ReadableStream) {
await data.cancel(e).catch(() => { });
}
if (uri != null) {
await AndroidFs.removeFile(uri).catch(() => { });
}
throw e;
}
}{
"permissions": [
"android-fs:allow-create-new-public-file",
"android-fs:allow-open-write-file-stream",
"android-fs:allow-write-file",
"android-fs:allow-set-public-file-pending",
"android-fs:allow-scan-public-file",
"android-fs:allow-remove-file"
]
}API
This plugin provides following APIs:
1. APIs to get entries such as files and directories
showOpenFilePickershowOpenDirPickershowSaveFilePickerreadDircreateNewFilecreateNewDircreateDircreateNewPublicFilecreateNewPublicImageFilecreateNewPublicVideoFilecreateNewPublicAudioFilelistVolumes
2. APIs to operate entries
copyFiletruncateFilerenameFilerenameDirremoveFileremoveEmptyDirremoveDirAllscanPublicFilesetPublicFilePending
3. APIs to get entry data
getFsPathgetMetadatagetNamegetTypegetFileMimeTypegetFileByteLengthgetFileThumbnailgetFileThumbnailAsBase64getFileThumbnailAsDataURL
4. APIs to get source URLs
convertFileSrcconvertFileThumbnailSrc
5. APIs to read/write files
readFilereadFileAsBase64readFileAsDataURLreadTextFilewriteFilewriteTextFile
6. APIs to stream files
openReadFileStreamopenReadTextFileLinesStreamopenWriteFileStreamcloseAllFileStreamscountAllFileStreams
7. APIs to send entries to other apps
showViewFileAppChoosershowViewDirAppChoosershowEditFileAppChoosershowShareFileAppChooser
8. APIs to manage permissions
checkPickerUriPermissionpersistPickerUriPermissioncheckPersistedPickerUriPermissionreleasePersistedPickerUriPermissionreleaseAllPersistedPickerUriPermissionscheckPublicFilesPermissionrequestPublicFilesPermission
9. Helper
isFsUriisAndroidgetAndroidApiLevel
License
This project is licensed under either of
- MIT license
- Apache License (Version 2.0)
at your option.
