pub struct PrivateStorage<'a, R: Runtime>(/* private fields */);
Expand description
API of file storage intended for the app’s use only.
§Examples
fn example(app: &tauri::AppHandle) {
use tauri_plugin_android_fs::AndroidFsExt;
let api = app.android_fs();
let private_storage = api.private_storage();
}
Implementations§
Source§impl<'a, R: Runtime> PrivateStorage<'a, R>
impl<'a, R: Runtime> PrivateStorage<'a, R>
Sourcepub fn resolve_path(&self, dir: PrivateDir) -> Result<PathBuf>
pub 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.
Sourcepub fn resolve_path_with(
&self,
dir: PrivateDir,
relative_path: impl AsRef<str>,
) -> Result<PathBuf>
pub 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.
pub fn resolve_uri(&self, dir: PrivateDir) -> Result<FileUri>
pub fn resolve_uri_with( &self, dir: PrivateDir, relative_path: impl AsRef<str>, ) -> Result<FileUri>
Sourcepub fn write(
&self,
base_dir: PrivateDir,
relative_path: impl AsRef<str>,
contents: impl AsRef<[u8]>,
) -> Result<()>
pub 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.
Sourcepub fn open_file(
&self,
base_dir: PrivateDir,
relative_path: impl AsRef<str>,
) -> Result<File>
pub 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.
Sourcepub fn create_file(
&self,
base_dir: PrivateDir,
relative_path: impl AsRef<str>,
) -> Result<File>
pub 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.
Sourcepub fn create_new_file(
&self,
base_dir: PrivateDir,
relative_path: impl AsRef<str>,
) -> Result<File>
pub 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.
Sourcepub fn read(
&self,
base_dir: PrivateDir,
relative_path: impl AsRef<str>,
) -> Result<Vec<u8>>
pub 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.
Sourcepub fn read_to_string(
&self,
base_dir: PrivateDir,
relative_path: impl AsRef<str>,
) -> Result<String>
pub 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.
Sourcepub fn read_dir(
&self,
base_dir: PrivateDir,
relative_path: Option<&str>,
) -> Result<ReadDir>
pub 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.
Sourcepub fn remove_file(
&self,
base_dir: PrivateDir,
relative_path: impl AsRef<str>,
) -> Result<()>
pub 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.
Sourcepub fn remove_dir(
&self,
base_dir: PrivateDir,
relative_path: Option<&str>,
) -> Result<()>
pub 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.
Sourcepub fn remove_dir_all(
&self,
base_dir: PrivateDir,
relative_path: Option<&str>,
) -> Result<()>
pub 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.
Sourcepub fn exists(
&self,
base_dir: PrivateDir,
relative_path: impl AsRef<str>,
) -> Result<bool>
pub 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.
Sourcepub fn metadata(
&self,
base_dir: PrivateDir,
relative_path: Option<&str>,
) -> Result<Metadata>
pub 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.