pub struct AppStorage<'a, R: Runtime> { /* private fields */ }Expand description
API of file storage intended for the app’s use.
§Examples
fn example(app: &tauri::AppHandle) {
use tauri_plugin_android_fs::AndroidFsExt as _;
let api = app.android_fs();
let app_storage = api.app_storage();
}Implementations§
Source§impl<'a, R: Runtime> SyncAppStorage<'a, R>
impl<'a, R: Runtime> SyncAppStorage<'a, R>
Sourcepub fn get_volumes(&self) -> Result<Vec<StorageVolume>>
pub fn get_volumes(&self) -> Result<Vec<StorageVolume>>
Gets a list of currently available storage volumes (internal storage, SD card, USB drive, etc.). Be aware of TOCTOU.
Since read-only SD cards and similar cases may be included,
please use StorageVolume { is_readonly, .. } for filtering as needed.
This function returns only storage volume that is considered stable by system. It includes device’s built-in storage and physical media slots under protective covers, but does not include storage volume considered temporary, such as USB flash drives connected to handheld devices.
This typically includes primary storage volume,
but it may occasionally be absent if primary torage volume is inaccessible
(e.g., mounted on a computer, removed, or another issue).
Primary storage volume is always listed first, if included. But the order of the others is not guaranteed.
§Note
The volume represents the logical view of a storage volume for an individual user: each user may have a different view for the same physical volume. In other words, it provides a separate area for each user in a multi-user environment.
§Support
All Android version.
Sourcepub fn get_primary_volume(&self) -> Result<Option<StorageVolume>>
pub fn get_primary_volume(&self) -> Result<Option<StorageVolume>>
Gets a primary storage volume.
In many cases, it is device’s built-in storage.
A device always has one (and one only) primary storage volume.
Primary volume may not currently be accessible
if it has been mounted by the user on their computer,
has been removed from the device, or some other problem has happened.
If so, this returns None.
§Note
The volume represents the logical view of a storage volume for an individual user: each user may have a different view for the same physical volume. In other words, it provides a separate area for each user in a multi-user environment.
§Support
All Android version.
Sourcepub fn resolve_path(
&self,
volume_id: Option<&StorageVolumeId>,
dir: AppDir,
) -> Result<PathBuf>
pub fn resolve_path( &self, volume_id: Option<&StorageVolumeId>, dir: AppDir, ) -> Result<PathBuf>
Gets the absolute path of the app directory on the specified storage volume.
App can fully manage entries within this directory via std::fs and etc.
This function does not create any directories; it only constructs the path.
These entries will be deleted when the app is uninstalled and may also be deleted at the user’s initialising request.
Since storage volume id and returned paths can change, only relative paths should be stored.
§Note
§About AppDir::Data and AppDir::Cache
Since these locations may contain files created by other Tauri plugins or webview systems, it is recommended to add a subdirectory with a unique name.
If you are unsure between this function and PrivateStorage::resolve_path with PrivateDir::Data or PrivateDir::Cache,
you don’t need to use this one.
The difference from it is that these files may be accessed by user or other apps that have permissions,
and it cannot always be available since removable storage can be ejected.
One advantage of using this is that it allows storing large app-specific data/cache on SD cards or other supplementary storage, which can be useful on older devices with limited built-in storage capacity. However on modern devices, the built-in storage capacity is relatively large, and there is little advantage in using this.
By using StorageVolume { is_emulated, .. },
you can determine whether this belongs to the same storage volume as PrivateStorage::resolve_path.
If it is true, there is no advantage in using this instead of PrivateStorage::resolve_path.
It only reduces security.
§About AppDir::PublicMedia
This is a location for storing media files shared with other apps nad user on older versions of Android.
For Android 11 (API level 30) or higher,
it has been marked as deprecated.
It still works, but you should consider migrating to PublicStorage.
This is a location that is unfamiliar to the user,
but calling AppStorage::scan_public_media_by_path will make it
displayed in a more user-friendly way in gallery apps and file managers.
For file in this directory, do not use operations such as rename or remove that rely on paths
(including URIs obtained via FileUri::from_path with this paths),
as they may break consistency with the MediaStore on old version.
Instead, use the URI obtained through AppStorage::scan_public_media_by_path together with methods
such as AndroidFs::rename or AndroidFs::remove_file.
§Args
- volume_id :
ID of the storage volume, such as internal storage, SD card, etc.
IfNoneis provided,the primary storage volumewill be used.
§Support
All Android version.
Sourcepub fn resolve_uri(
&self,
volume_id: Option<&StorageVolumeId>,
dir: AppDir,
relative_path: impl AsRef<Path>,
) -> Result<FileUri>
pub fn resolve_uri( &self, volume_id: Option<&StorageVolumeId>, dir: AppDir, relative_path: impl AsRef<Path>, ) -> Result<FileUri>
Sourcepub fn scan_public_media_by_path(
&self,
path: impl AsRef<Path>,
mime_type: Option<&str>,
) -> Result<FileUri>
pub fn scan_public_media_by_path( &self, path: impl AsRef<Path>, mime_type: Option<&str>, ) -> Result<FileUri>
Scans the specified file in MediaStore and returns it’s URI if success.
By doing this, the file will be visible in the Gallery and etc.
§Args
-
uri :
Absolute path of the target file.
This must be a path obtained fromAppStorage::resolve_pathwithAppDir::PublicMediaand it’s descendants path. -
mime_type :
The MIME type of the file.
IfNone, the MIME type will be inferred from the extension of the path.
If that also fails,application/octet-streamwill be used.
§Support
All Android version.
Sourcepub fn get_public_media_path(&self, uri: &FileUri) -> Result<PathBuf>
pub fn get_public_media_path(&self, uri: &FileUri) -> Result<PathBuf>
Gets the absolute path of the specified file.
§Args
- uri :
Target file URI. This must be a URI obtained fromAppStorage::scan_public_media_by_path.
§Support
All Android version.