tauri-plugin-android-fs-api
v27.1.0
Published
Android file system API for Tauri.
Maintainers
Readme
Note: I’m using a translation tool, so there may be some inappropriate expressions.
Overview
This plugin provides a unified file system API for all Android versions supported by Tauri.
Setup
First, install this plugin to your Tauri project:
src-tauri/Cargo.toml
[dependencies]
tauri-plugin-android-fs = { version = "=27.1.0", features = [
# For `AndroidFs.createNewPublicFile` and related APIs on Android 9 or lower
"legacy_storage_permission",
# For notification options
"notification_permission"
] }Next, register this plugin in your Tauri project:
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, set the APIs that can be called from the Javascript:
src-tauri/capabilities/*.json
{
"permissions": [
"android-fs:default"
]
}Finally, install the JavaScript Guest bindings using whichever JavaScript package manager you prefer:
pnpm add [email protected] -E
# or
npm install [email protected] --save-exact
# or
yarn add [email protected] --exactNOTE: Please make sure that the Rust-side tauri-plugin-android-fs and the JavaScript-side tauri-plugin-android-fs-api versions match exactly.
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, including app-specific directories. 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/**/*"]
}
]
}Examples
import {
AndroidFs,
AndroidPublicGeneralPurposeDir,
AndroidProgressNotificationIconType,
type AndroidProgressNotificationTemplate
} from 'tauri-plugin-android-fs-api';
/**
* Saves the 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(
AndroidPublicGeneralPurposeDir.Download,
`MyApp/${fileName}`,
mimeType,
{ isPending: true }
);
// Configures the system status bar notification (optional)
const notification: AndroidProgressNotificationTemplate | undefined = {
icon: AndroidProgressNotificationIconType.Download,
title: "{{fileName}}",
textProgress: "Downloading...",
textCompletion: "Download complete",
subText: "{{progress}}"
};
// Writes data to the file
if (data instanceof Uint8Array) {
await AndroidFs.writeFile(uri, data, { notification });
}
else if (data instanceof ReadableStream) {
const writer = await AndroidFs.openWriteFileStream(uri, { notification });
await data.pipeTo(writer);
}
else {
throw new TypeError("Unsupported data type");
}
// Makes the file visible in other apps and gallery
await AndroidFs.setPublicFilePending(uri, false);
await AndroidFs.scanPublicFile(uri);
}
// Handles error and cleanup
catch (e) {
if (data instanceof ReadableStream) {
await data.cancel(e).catch(() => { });
}
if (uri != null) {
await AndroidFs.removeFile(uri).catch(() => { });
}
throw e;
}
}APIs
This plugin provides following APIs:
1. APIs to obtain entries such as files and directories
AndroidFs.showOpenFilePickerAndroidFs.showOpenDirPickerAndroidFs.showSaveFilePickerAndroidFs.readDirAndroidFs.createNewFileAndroidFs.createDirAndroidFs.createNewPublicFileAndroidFs.createNewPublicImageFileAndroidFs.createNewPublicVideoFileAndroidFs.createNewPublicAudioFile
2. APIs to retrieve entry data
AndroidFs.getFsPathAndroidFs.getMetadataAndroidFs.getNameAndroidFs.getTypeAndroidFs.getMimeTypeAndroidFs.getByteLengthAndroidFs.getThumbnailAndroidFs.getThumbnailAsBytesAndroidFs.getThumbnailAsBase64AndroidFs.getThumbnailAsDataURL
3. APIs to operate entries
AndroidFs.copyFileAndroidFs.truncateFileAndroidFs.renameFileAndroidFs.renameDirAndroidFs.removeFileAndroidFs.removeEmptyDirAndroidFs.removeDirAllAndroidFs.scanPublicFileAndroidFs.setPublicFilePending
4. APIs to read files
AndroidFs.openReadFileStreamAndroidFs.openReadTextFileLinesStreamAndroidFs.readFileAndroidFs.readFileAsBase64AndroidFs.readFileAsDataURLAndroidFs.readTextFile
5. APIs to write to files
AndroidFs.openWriteFileStreamAndroidFs.writeFileAndroidFs.writeTextFile
6. APIs to manage permissions
AndroidFs.checkPickerUriPermissionAndroidFs.persistPickerUriPermissionAndroidFs.checkPersistedPickerUriPermissionAndroidFs.releasePersistedPickerUriPermissionAndroidFs.releaseAllPersistedPickerUriPermissionsAndroidFs.checkPublicFilesPermissionAndroidFs.requestPublicFilesPermission
7. APIs to send entries to other apps
AndroidFs.showViewFileDialogAndroidFs.showViewDirDialogAndroidFs.showShareFileDialog
8. Helper
isAndroidgetAndroidApiLevel
For simplicity, some features and detailed options of the API have been omitted. If you need them, please consider using the tauri-plugin-android-fs on the Rust side.
License
This project is licensed under either of
- MIT license
- Apache License (Version 2.0)
at your option.
