tauri-plugin-android-fs-api
v24.0.0
Published
Android file system API for Tauri.
Maintainers
Readme
Note: I’m using a translation tool, so there may be some inappropriate expressions.
Setup
First, install this plugin to your Tauri project:
src-tauri/Cargo.toml
[dependencies]
tauri-plugin-android-fs = { version = "24", features = ["legacy_storage_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()
// Add following
.plugin(tauri_plugin_android_fs::init())
//
.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:all-without-delete"
]
}Finally, install the JavaScript Guest bindings using whichever JavaScript package manager you prefer:
pnpm add tauri-plugin-android-fs-api
# or
npm add tauri-plugin-android-fs-api
# or
yarn add tauri-plugin-android-fs-apiUsage
This plugin operates on files and directories via URIs rather than paths.
By using AndroidFs.getFsPath, you can obtain a path from a URI and use it with the functions provided by Tauri's file system, @tauri-apps/plugin-fs, to read and write files. For those paths, there is no need for you to set the scope configuration of Tauri's file system.
import { AndroidFs } from 'tauri-plugin-android-fs-api'
import { writeTextFile } from '@tauri-apps/plugin-fs';
/**
* Save the text to '~/Download/MyApp/{fileName}'
*/
async function saveText(fileName: string, data: string): Promise<void> {
const baseDir = "Download";
const relativePath = "MyApp/" + fileName;
const mimeType = "text/plain";
const uri = await AndroidFs.createNewPublicFile(baseDir, relativePath, mimeType);
try {
const path = await AndroidFs.getFsPath(uri);
await writeTextFile(path, data);
await AndroidFs.scanPublicFile(uri);
}
catch (e) {
await AndroidFs.removeFile(uri).catch(() => {});
throw e;
}
}Some functions accept not only URIs but also absolute paths.
In this case, you need to set the scope configuration in the same way as @tauri-apps/plugin-fs.
src-tauri/capabilities/*.json
{
"permissions": [
{
"identifier": "android-fs:scope",
"allow": [
"$APPDATA/**/*"
],
"deny": [
"$APPDATA/secret.txt"
]
}
]
}And you can also assign a specific scope to a particular command.
src-tauri/capabilities/*.json
{
"permissions": [
{
"identifier": "android-fs:allow-copy-file",
"allow": [
"$APPDATA/**/*"
],
"deny": [
"$APPDATA/secret.txt"
]
}
]
}Then, this plugin provides following APIs:
1. APIs to obtain entries such as files and directories.
AndroidFs.showOpenFilePickerAndroidFs.showOpenDirPickerAndroidFs.showSaveFilePickerAndroidFs.readDirAndroidFs.createNewFileAndroidFs.createDirAllAndroidFs.createNewPublicFileAndroidFs.createNewPublicImageFileAndroidFs.createNewPublicVideoFileAndroidFs.createNewPublicAudioFile
3. APIs to retrieve data from entries.
AndroidFs.getThumbnailAndroidFs.getThumbnailBase64AndroidFs.getThumbnailDataUrlAndroidFs.getFsPathAndroidFs.getNameAndroidFs.getByteLengthAndroidFs.getTypeAndroidFs.getMimeTypeAndroidFs.getMetadata
3. APIs to operate entries.
AndroidFs.scanPublicFileAndroidFs.copyFileAndroidFs.truncateFileAndroidFs.removeFileAndroidFs.removeEmptyDirAndroidFs.removeDirAll
4. APIs to manage entry permissions
AndroidFs.persistUriPermissionAndroidFs.checkPersistedUriPermissionAndroidFs.releasePersistedUriPermissionAndroidFs.releaseAllPersistedUriPermissionsAndoridFs.hasPublicFilesPermissionAndroidFs.requestPublicFilesPermission
5. APIs to send entries to other apps.
AndroidFs.showViewFileDialogAndroidFs.showViewDirDialogAndroidFs.showShareFileDialog
6. Helper
isAndroid
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.
