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 create_file_in_app_dir(
&self,
dir: impl Into<PublicDir>,
relative_path: impl AsRef<str>,
mime_type: Option<&str>,
) -> Result<FileUri>
pub fn create_file_in_app_dir( &self, dir: impl Into<PublicDir>, relative_path: impl AsRef<str>, mime_type: Option<&str>, ) -> Result<FileUri>
Creates a new empty file in the app dir of specified public directory and returns a persistent read-write URI.
The created file has following features :
- Will be registered with the corresponding MediaStore as needed.
- Always supports remove and rename by this app until the app uninstalled.
- Not removed when the app is uninstalled.
This is the same as following:
let app_name = public_storage.app_dir_name()?;
public_storage.create_file(
dir,
format!("{app_name}/{relative_path}"),
mime_type
)?;
§Args
-
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. -
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. -
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_file(
&self,
dir: impl Into<PublicDir>,
relative_path_with_subdir: impl AsRef<str>,
mime_type: Option<&str>,
) -> Result<FileUri>
pub fn create_file( &self, dir: impl Into<PublicDir>, relative_path_with_subdir: impl AsRef<str>, mime_type: Option<&str>, ) -> Result<FileUri>
Creates a new empty file in the specified public directory and returns a persistent read-write URI.
The created file has following features :
- Will be registered with the corresponding MediaStore as needed.
- Always supports remove and rename by this app until the app uninstalled.
- Not removed when the app is uninstalled.
§Args
-
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. -
relative_path_with_subdir :
The file path relative to the base directory.
Please specify a subdirectory in this, such asMyApp/file.txt
orMyApp/2025-2-11/file.txt
. Do not usefile.txt
.
As shown above, it is customary to specify the app name at the beginning of the subdirectory, and in this case, usingPublicStorage::create_file_in_app_dir
is recommended.
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. -
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_with_subdir 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,
dir: impl Into<PublicDir>,
relative_path: impl AsRef<str>,
) -> Result<()>
pub fn create_dir_all( &self, dir: impl Into<PublicDir>, relative_path: impl AsRef<str>, ) -> Result<()>
Recursively create a directory and all of its parent components if they are missing.
If it already exists, do nothing.
PublicStorage::create_file
does this automatically, so there is no need to use it together.
§Args
-
dir :
The base directory. -
relative_path :
The directory path relative to the base directory.
§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_in_app_dir(
&self,
dir: impl Into<PublicDir>,
relative_path: impl AsRef<str>,
) -> Result<()>
pub fn create_dir_all_in_app_dir( &self, dir: impl Into<PublicDir>, relative_path: impl AsRef<str>, ) -> Result<()>
Recursively create a directory and all of its parent components if they are missing.
If it already exists, do nothing.
PublicStorage::create_file_in_app_dir
does this automatically, so there is no need to use it together.
This is the same as following:
let app_name = public_storage.app_dir_name()?;
public_storage.create_dir_all(
dir,
format!("{app_name}/{relative_path}"),
)?;
§Args
-
dir :
The base directory. -
relative_path :
The directory path relative to the base directory.
§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 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.
§Support
All.
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.
§Support
All.