tauri-plugin-android-fs-api
v28.2.2
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 = "=28.2.2",
features = [
# To access public files on older Android versions
"legacy_storage_permission",
# To enable 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/**/*"]
}
]
}Example
import {
AndroidFs,
AndroidPublicGeneralPurposeDir,
AndroidProgressNotificationIconType,
type AndroidProgressNotificationTemplate
} 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(
AndroidPublicGeneralPurposeDir.Download,
`MyApp/${fileName}`,
mimeType,
{ isPending: true }
);
// Configures a 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 ReadableStream) {
const writer = await AndroidFs.openWriteFileStream(uri, { notification });
await data.pipeTo(writer);
}
else {
await AndroidFs.writeFile(uri, data, { notification });
}
// 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
AndroidFs.showOpenFilePickerAndroidFs.showOpenDirPickerAndroidFs.showSaveFilePickerAndroidFs.readDirAndroidFs.createNewFileAndroidFs.createNewDirAndroidFs.createDirAndroidFs.createNewPublicFileAndroidFs.createNewPublicImageFileAndroidFs.createNewPublicVideoFileAndroidFs.createNewPublicAudioFileAndroidFs.listVolumes
2. APIs to operate entries
AndroidFs.copyFileAndroidFs.truncateFileAndroidFs.renameFileAndroidFs.renameDirAndroidFs.removeFileAndroidFs.removeEmptyDirAndroidFs.removeDirAllAndroidFs.scanPublicFileAndroidFs.setPublicFilePending
3. APIs to get entry data
AndroidFs.getFsPathAndroidFs.getMetadataAndroidFs.getNameAndroidFs.getTypeAndroidFs.getMimeTypeAndroidFs.getByteLengthAndroidFs.getThumbnailAndroidFs.getThumbnailAsBytesAndroidFs.getThumbnailAsBase64AndroidFs.getThumbnailAsDataURL
4. APIs to get source URLs
AndroidFs.convertFileSrcAndroidFs.convertThumbnailSrc
5. APIs to read/write files
AndroidFs.readFileAndroidFs.readFileAsBase64AndroidFs.readFileAsDataURLAndroidFs.readTextFileAndroidFs.writeFileAndroidFs.writeTextFile
6. APIs to stream files
AndroidFs.openReadFileStreamAndroidFs.openReadTextFileLinesStreamAndroidFs.openWriteFileStreamAndroidFs.closeAllFileStreamsAndroidFs.countAllFileStreams
7. APIs to send entries to other apps
AndroidFs.showViewFileDialogAndroidFs.showViewDirDialogAndroidFs.showShareFileDialog
8. APIs to manage permissions
AndroidFs.checkPickerUriPermissionAndroidFs.persistPickerUriPermissionAndroidFs.checkPersistedPickerUriPermissionAndroidFs.releasePersistedPickerUriPermissionAndroidFs.releaseAllPersistedPickerUriPermissionsAndroidFs.checkPublicFilesPermissionAndroidFs.requestPublicFilesPermission
9. Helper
isAndroidgetAndroidApiLevel
License
This project is licensed under either of
- MIT license
- Apache License (Version 2.0)
at your option.
