Trait PrivateStorage

Source
pub trait PrivateStorage<R: Runtime> {
Show 17 methods // Required methods fn resolve_path(&self, dir: PrivateDir) -> Result<PathBuf>; fn app_handle(&self) -> &AppHandle<R>; // Provided methods fn resolve_path_with( &self, dir: PrivateDir, relative_path: impl AsRef<str>, ) -> Result<PathBuf> { ... } fn resolve_uri(&self, dir: PrivateDir) -> Result<FileUri> { ... } fn resolve_uri_with( &self, dir: PrivateDir, relative_path: impl AsRef<str>, ) -> Result<FileUri> { ... } fn write( &self, base_dir: PrivateDir, relative_path: impl AsRef<str>, contents: impl AsRef<[u8]>, ) -> Result<()> { ... } fn open_file( &self, base_dir: PrivateDir, relative_path: impl AsRef<str>, ) -> Result<File> { ... } fn create_file( &self, base_dir: PrivateDir, relative_path: impl AsRef<str>, ) -> Result<File> { ... } fn create_new_file( &self, base_dir: PrivateDir, relative_path: impl AsRef<str>, ) -> Result<File> { ... } fn read( &self, base_dir: PrivateDir, relative_path: impl AsRef<str>, ) -> Result<Vec<u8>> { ... } fn read_to_string( &self, base_dir: PrivateDir, relative_path: impl AsRef<str>, ) -> Result<String> { ... } fn read_dir( &self, base_dir: PrivateDir, relative_path: Option<&str>, ) -> Result<ReadDir> { ... } fn remove_file( &self, base_dir: PrivateDir, relative_path: impl AsRef<str>, ) -> Result<()> { ... } fn remove_dir( &self, base_dir: PrivateDir, relative_path: Option<&str>, ) -> Result<()> { ... } fn remove_dir_all( &self, base_dir: PrivateDir, relative_path: Option<&str>, ) -> Result<()> { ... } fn exists( &self, base_dir: PrivateDir, relative_path: impl AsRef<str>, ) -> Result<bool> { ... } fn metadata( &self, base_dir: PrivateDir, relative_path: Option<&str>, ) -> Result<Metadata> { ... }
}
Expand description

File storage intended for the app’s use only.

Required Methods§

Source

fn resolve_path(&self, dir: PrivateDir) -> Result<PathBuf>

Get the absolute path of the specified directory.
App can fully manage entries within this directory without any permission via std::fs.

These files will be deleted when the app is uninstalled and may also be deleted at the user’s initialising request.
When using PrivateDir::Cache, the system will automatically delete files in this directory as disk space is needed elsewhere on the device.

The returned path may change over time if the calling app is moved to an adopted storage device, so only relative paths should be persisted.

§Examples
use tauri_plugin_android_fs::{AndroidFs, AndroidFsExt, PrivateDir, PrivateStorage};
 
fn example(app: tauri::AppHandle) {
    let api = app.android_fs().private_storage();
 
    let dir_path = api.resolve_path(PrivateDir::Data).unwrap();
    let file_path = dir_path.join("2025-2-12/data.txt");
     
    // Write file
    std::fs::create_dir_all(file_path.parent().unwrap()).unwrap();
    std::fs::write(&file_path, "aaaa").unwrap();
 
    // Read file
    let _ = std::fs::read_to_string(&file_path).unwrap();
 
    // Remove file
    std::fs::remove_file(&file_path).unwrap();
}
§Support

All Android version.

Source

fn app_handle(&self) -> &AppHandle<R>

Provided Methods§

Source

fn resolve_path_with( &self, dir: PrivateDir, relative_path: impl AsRef<str>, ) -> Result<PathBuf>

Get the absolute path of the specified relative path and base directory.
App can fully manage entries of this path without any permission via std::fs.

See PrivateStorage::resolve_path for details.

§Support

All Android version.

Source

fn resolve_uri(&self, dir: PrivateDir) -> Result<FileUri>

Source

fn resolve_uri_with( &self, dir: PrivateDir, relative_path: impl AsRef<str>, ) -> Result<FileUri>

Source

fn write( &self, base_dir: PrivateDir, relative_path: impl AsRef<str>, contents: impl AsRef<[u8]>, ) -> Result<()>

Writes a slice as the entire contents of a file.

This function will create a file if it does not exist, and will entirely replace its contents if it does.
Recursively create parent directories if they are missing.

This internally uses PrivateStorage::resolve_path , std::fs::create_dir_all, and std::fs::write.
See PrivateStorage::resolve_path for details.

§Support

All Android version.

Source

fn open_file( &self, base_dir: PrivateDir, relative_path: impl AsRef<str>, ) -> Result<File>

Open a file in read-only mode.

If you only need to read the entire file contents, consider using PrivateStorage::read or PrivateStorage::read_to_string instead.

This internally uses PrivateStorage::resolve_path and std::fs::File::open.
See PrivateStorage::resolve_path for details.

§Support

All Android version.

Source

fn create_file( &self, base_dir: PrivateDir, relative_path: impl AsRef<str>, ) -> Result<File>

Opens a file in write-only mode.
This function will create a file if it does not exist, and will truncate it if it does.

If you only need to write the contents, consider using PrivateStorage::write instead.

This internally uses PrivateStorage::resolve_path and std::fs::File::create.
See PrivateStorage::resolve_path for details.

§Support

All Android version.

Source

fn create_new_file( &self, base_dir: PrivateDir, relative_path: impl AsRef<str>, ) -> Result<File>

Creates a new file in read-write mode; error if the file exists.

This internally uses PrivateStorage::resolve_path and std::fs::File::create_new.
See PrivateStorage::resolve_path for details.

§Support

All Android version.

Source

fn read( &self, base_dir: PrivateDir, relative_path: impl AsRef<str>, ) -> Result<Vec<u8>>

Reads the entire contents of a file into a bytes vector.

If you need std::fs::File, use PrivateStorage::open_file insted.

This internally uses PrivateStorage::resolve_path and std::fs::read.
See PrivateStorage::resolve_path for details.

§Support

All Android version.

Source

fn read_to_string( &self, base_dir: PrivateDir, relative_path: impl AsRef<str>, ) -> Result<String>

Reads the entire contents of a file into a string.

If you need std::fs::File, use PrivateStorage::open_file insted.

This internally uses PrivateStorage::resolve_path and std::fs::read_to_string.
See PrivateStorage::resolve_path for details.

§Support

All Android version.

Source

fn read_dir( &self, base_dir: PrivateDir, relative_path: Option<&str>, ) -> Result<ReadDir>

Returns an iterator over the entries within a directory.

This internally uses PrivateStorage::resolve_path and std::fs::read_dir.
See PrivateStorage::resolve_path for details.

§Support

All Android version.

Source

fn remove_file( &self, base_dir: PrivateDir, relative_path: impl AsRef<str>, ) -> Result<()>

Removes a file from the filesystem.

This internally uses PrivateStorage::resolve_path and std::fs::remove_file.
See PrivateStorage::resolve_path for details.

§Support

All Android version.

Source

fn remove_dir( &self, base_dir: PrivateDir, relative_path: Option<&str>, ) -> Result<()>

Removes an empty directory.
If you want to remove a directory that is not empty, as well as all of its contents recursively, consider using PrivateStorage::remove_dir_all instead.

This internally uses PrivateStorage::resolve_path and std::fs::remove_dir.
See PrivateStorage::resolve_path for details.

§Support

All Android version.

Source

fn remove_dir_all( &self, base_dir: PrivateDir, relative_path: Option<&str>, ) -> Result<()>

Removes a directory at this path, after removing all its contents. Use carefully!

This internally uses PrivateStorage::resolve_path and std::fs::remove_dir_all.
See PrivateStorage::resolve_path for details.

§Support

All Android version.

Source

fn exists( &self, base_dir: PrivateDir, relative_path: impl AsRef<str>, ) -> Result<bool>

Returns Ok(true) if the path points at an existing entity.

This internally uses PrivateStorage::resolve_path and std::fs::exists.
See PrivateStorage::resolve_path for details.

§Support

All Android version.

Source

fn metadata( &self, base_dir: PrivateDir, relative_path: Option<&str>, ) -> Result<Metadata>

Queries the file system to get information about a file, directory.

This internally uses PrivateStorage::resolve_path and std::fs::metadata.
See PrivateStorage::resolve_path for details.

§Support

All Android version.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§