pub struct PublicStorage<'a, R: Runtime>(/* private fields */);
Expand description
API of file storage that is available to other applications and users.
§Examples
fn example(app: &tauri::AppHandle) {
use tauri_plugin_android_fs::AndroidFsExt as _;
let api = app.android_fs();
let public_storage = api.public_storage();
}
Implementations§
Source§impl<'a, R: Runtime> PublicStorage<'a, R>
impl<'a, R: Runtime> PublicStorage<'a, R>
Sourcepub fn get_available_volumes(&self) -> Result<Vec<PublicStorageVolume>>
pub fn get_available_volumes(&self) -> Result<Vec<PublicStorageVolume>>
Gets a list of currently available storage volumes (internal storage, SD card, USB drive, etc.). Be aware of TOCTOU.
This typically includes primary storage volume
,
but it may occasionally be absent if the primary 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 (e.g. when the volume is a built-in emulated storage).
§Support
Android 10 (API level 29) or higher.
Sourcepub fn get_primary_volume(&self) -> Result<PublicStorageVolume>
pub fn get_primary_volume(&self) -> Result<PublicStorageVolume>
Gets a primary storage volume.
This is the most common and recommended storage volume for placing files that can be accessed by other apps or user.
A device always has one (and one only) primary storage volume.
The returned 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.
You can find a list of all currently available volumes using PublicStorage::get_available_volumes
.
§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 (e.g. when the volume is a built-in emulated storage).
The primary volume provides a separate area for each user in a multi-user environment.
§Support
Android 10 (API level 29) or higher.
§References
https://developer.android.com/reference/android/provider/MediaStore#VOLUME_EXTERNAL_PRIMARY
Sourcepub fn create_new_file(
&self,
volume: Option<&PublicStorageVolumeId>,
base_dir: impl Into<PublicDir>,
use_app_dir: bool,
relative_path: impl AsRef<Path>,
mime_type: Option<&str>,
) -> Result<FileUri>
pub fn create_new_file( &self, volume: Option<&PublicStorageVolumeId>, base_dir: impl Into<PublicDir>, use_app_dir: bool, relative_path: impl AsRef<Path>, mime_type: Option<&str>, ) -> Result<FileUri>
Creates a new empty file in the specified public directory of the storage volume.
This returns a persistent read-write URI.
The created file has the following features:
- It is registered with the appropriate MediaStore as needed.
- The app can fully manage it until the app is uninstalled.
- It is not removed when the app itself is uninstalled.
§Args
-
volume :
The storage volume, such as internal storage, SD card, etc.
Usually, you don’t need to specify this unless there is a special reason.
IfNone
is provided,the primary storage volume
will be used. -
base_dir :
The base directory.
When usingPublicImageDir
, use only image MIME types for mime_type, which is discussed below.; using other types may cause errors. Similarly, use only the corresponding media types forPublicVideoDir
andPublicAudioDir
. OnlyPublicGeneralPurposeDir
supports all MIME types. -
use_app_dir :
Determines whether to insert a directory named after the application name specified in Tauri’s configuration between base_dir and relative_path. It is recommended to enable this unless there is a special reason not to.
SeePublicStorage::app_dir_name
-
relative_path :
The file path relative to the base directory.
Any missing subdirectories in the specified path will be created automatically.
If a file with the same name already exists, the system append a sequential number to ensure uniqueness.
If no extension is present, the system may infer one from mime_type and may append it to the file name. But this append-extension operation depends on the model and version.
The system may sanitize these strings as needed, so those strings may not be used as it is. -
mime_type :
The MIME type of the file to be created.
If this is None, MIME type is inferred from the extension of relative_path and if that fails,application/octet-stream
is used.
§Support
Android 10 (API level 29) or higher.
Note :
PublicAudioDir::Audiobooks
is not available on Android 9 (API level 28) and lower. Availability on a given device can be verified by callingPublicStorage::is_audiobooks_dir_available
.PublicAudioDir::Recordings
is not available on Android 11 (API level 30) and lower. Availability on a given device can be verified by callingPublicStorage::is_recordings_dir_available
.- Others dirs are available in all Android versions.
Sourcepub fn create_dir_all(
&self,
volume: Option<&PublicStorageVolumeId>,
base_dir: impl Into<PublicDir>,
use_app_dir: bool,
relative_path: impl AsRef<Path>,
) -> Result<()>
pub fn create_dir_all( &self, volume: Option<&PublicStorageVolumeId>, base_dir: impl Into<PublicDir>, use_app_dir: bool, relative_path: impl AsRef<Path>, ) -> Result<()>
Recursively create a directory and all of its parent components if they are missing.
If it already exists, do nothing.
PublicStorage::create_new_file
does this automatically, so there is no need to use it together.
§Args
-
volume :
The storage volume, such as internal storage, SD card, etc.
IfNone
is provided,the primary storage volume
will be used. -
base_dir :
The base directory. -
use_app_dir :
Determines whether to insert a directory named after the application name specified in Tauri’s configuration between base_dir and relative_path. -
relative_path :
The directory path relative to the base directory.
The system may sanitize these strings as needed, so those strings may not be used as it is.
§Support
Android 10 (API level 29) or higher.
Note :
PublicAudioDir::Audiobooks
is not available on Android 9 (API level 28) and lower. Availability on a given device can be verified by callingPublicStorage::is_audiobooks_dir_available
.PublicAudioDir::Recordings
is not available on Android 11 (API level 30) and lower. Availability on a given device can be verified by callingPublicStorage::is_recordings_dir_available
.- Others dirs are available in all Android versions.
Sourcepub fn resolve_path(
&self,
volume: Option<&PublicStorageVolumeId>,
base_dir: impl Into<PublicDir>,
use_app_dir: bool,
) -> Result<PathBuf>
pub fn resolve_path( &self, volume: Option<&PublicStorageVolumeId>, base_dir: impl Into<PublicDir>, use_app_dir: bool, ) -> Result<PathBuf>
Retrieves the absolute path for a specified public directory within the given storage volume.
This function does not create any directories; it only constructs the path.
You can create files and folders under this directory and read or write only them.
Please avoid using this whenever possible.
Use it only in cases that cannot be handled by PublicStorage::create_new_file
or PrivateStorage::resolve_path
,
such as when you need to pass the absolute path of a user-accessible file as an argument to any database library.
§Note
Filesystem access via this path may be heavily impacted by emulation overhead. And those files will not be registered in MediaStore. It might eventually be registered over time, but this should not be expected. As a result, it may not appear in gallery apps or photo picker tools.
You cannot access files created by other apps.
Additionally, if the app is uninstalled,
you will no longer be able to access the files you created,
even if the app is reinstalled.
Android tends to restrict public file access using paths, so this may stop working in the future.
§Args
-
volume :
The storage volume, such as internal storage, SD card, etc.
IfNone
is provided,the primary storage volume
will be used. -
base_dir :
The base directory. -
use_app_dir :
Determines whether to insert a directory named after the application name specified in Tauri’s configuration under base_dir.
§Support
Android 10 (API level 29) or higher.
Note :
PublicAudioDir::Audiobooks
is not available on Android 9 (API level 28) and lower. Availability on a given device can be verified by callingPublicStorage::is_audiobooks_dir_available
.PublicAudioDir::Recordings
is not available on Android 11 (API level 30) and lower. Availability on a given device can be verified by callingPublicStorage::is_recordings_dir_available
.- Others dirs are available in all Android versions.
Sourcepub fn resolve_initial_location(
&self,
volume: Option<&PublicStorageVolumeId>,
base_dir: impl Into<PublicDir>,
use_app_dir: bool,
relative_path: impl AsRef<Path>,
create_dir_all: bool,
) -> Result<FileUri>
pub fn resolve_initial_location( &self, volume: Option<&PublicStorageVolumeId>, base_dir: impl Into<PublicDir>, use_app_dir: bool, relative_path: impl AsRef<Path>, create_dir_all: bool, ) -> Result<FileUri>
Create the specified directory URI that has no permissions.
This should only be used as initial_location
in the file picker.
It must not be used for any other purpose.
This is useful when selecting save location,
but when selecting existing entries, initial_location
is often better with None.
§Args
-
volume :
The storage volume, such as internal storage, SD card, etc.
IfNone
is provided,the primary storage volume
will be used. -
base_dir :
The base directory. -
use_app_dir :
Determines whether to insert a directory named after the application name specified in Tauri’s configuration between base_dir and relative_path. -
relative_path :
The directory path relative to the base directory.
The system may sanitize these strings as needed, so those strings may not be used as it is.
§Support
If use None
to volume, all Android version:
otherwise: Android 10 (API level 29) or higher
Note :
PublicAudioDir::Audiobooks
is not available on Android 9 (API level 28) and lower. Availability on a given device can be verified by callingPublicStorage::is_audiobooks_dir_available
.PublicAudioDir::Recordings
is not available on Android 11 (API level 30) and lower. Availability on a given device can be verified by callingPublicStorage::is_recordings_dir_available
.- Others dirs are available in all Android versions.
Sourcepub fn resolve_initial_location_top(
&self,
volume: Option<&PublicStorageVolumeId>,
) -> Result<FileUri>
pub fn resolve_initial_location_top( &self, volume: Option<&PublicStorageVolumeId>, ) -> Result<FileUri>
Create the specified directory URI that has no permissions.
This should only be used as initial_location
in the file picker.
It must not be used for any other purpose.
This is useful when selecting save location,
but when selecting existing entries, initial_location
is often better with None.
§Args
- volume :
The storage volume, such as internal storage, SD card, etc.
IfNone
is provided,the primary storage volume
will be used.
§Support
If use None
to volume, all Android version:
otherwise: Android 10 (API level 29) or higher
Note :
PublicAudioDir::Audiobooks
is not available on Android 9 (API level 28) and lower. Availability on a given device can be verified by callingPublicStorage::is_audiobooks_dir_available
.PublicAudioDir::Recordings
is not available on Android 11 (API level 30) and lower. Availability on a given device can be verified by callingPublicStorage::is_recordings_dir_available
.- Others dirs are available in all Android versions.
Sourcepub fn is_available(&self) -> Result<bool>
pub fn is_available(&self) -> Result<bool>
Verify whether the basic functions of PublicStorage
(such as PublicStorage::create_new_file
) can be performed.
If on Android 9 (API level 28) and lower, this returns false.
If on Android 10 (API level 29) or higher, this returns true.
§Support
All Android version.
Sourcepub fn is_audiobooks_dir_available(&self) -> Result<bool>
pub fn is_audiobooks_dir_available(&self) -> Result<bool>
Verify whether PublicAudioDir::Audiobooks
is available on a given device.
If on Android 9 (API level 28) and lower, this returns false.
If on Android 10 (API level 29) or higher, this returns true.
§Support
All Android version.
Sourcepub fn is_recordings_dir_available(&self) -> Result<bool>
pub fn is_recordings_dir_available(&self) -> Result<bool>
Verify whether PublicAudioDir::Recordings
is available on a given device.
If on Android 11 (API level 30) and lower, this returns false.
If on Android 12 (API level 31) or higher, this returns true.
§Support
All Android version.
Sourcepub fn app_dir_name(&self) -> Result<&str>
pub fn app_dir_name(&self) -> Result<&str>
Resolve the app dir name from Tauri’s config.
This uses “productName” in src-tauri/tauri.conf.json
§Support
All Android version.