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> AsyncPublicStorage<'a, R>
impl<'a, R: Runtime> AsyncPublicStorage<'a, R>
Sourcepub async fn request_permission(&self) -> Result<bool>
pub async fn request_permission(&self) -> Result<bool>
Requests file access permission from the user if needed.
When this function returns true,
the app is allowed to create files in PublicStorage and read/write the files it creates.
Access to files created by other apps is not guaranteed.
Additionally, after the app is uninstalled and reinstalled,
previously created files may become inaccessible
§Version behavior
§Android 10 or higher
Requests no permission.
This function always returns true.
§Android 9 or lower
Requests WRITE_EXTERNAL_STORAGE and READ_EXTERNAL_STORAGE permissions.
To request the permissions, you must declare it in AndroidManifest.xml.
By enabling the legacy_storage_permission feature,
the permissions will be declared automatically only for Android 9 or lower.
§Support
All Android versions
Sourcepub async fn has_permission(&self) -> Result<bool>
pub async fn has_permission(&self) -> Result<bool>
Indicates whether the app has file access permission.
When this function returns true,
the app is allowed to create files in PublicStorage and read/write the files it creates.
Access to files created by other apps is not guaranteed.
Additionally, after the app is uninstalled and reinstalled,
previously created files may become inaccessible
§Version behavior
§Android 10 or higher
Always returns true.
§Android 9 or lower
Returns true if the app has been granted WRITE_EXTERNAL_STORAGE and READ_EXTERNAL_STORAGE permissions.
See PublicStorage::request_permission for requesting the permissions.
§Support
All Android versions.
Sourcepub async fn get_volumes(&self) -> Result<Vec<StorageVolume>>
pub async 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 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.
§Version behavior
For Android 9 (API level 28) or lower, this does not include any storage volumes other than the primary one.
§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 async fn get_primary_volume(&self) -> Result<Option<StorageVolume>>
pub async fn get_primary_volume(&self) -> Result<Option<StorageVolume>>
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.
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 async fn create_new_file(
&self,
volume_id: Option<&StorageVolumeId>,
base_dir: impl Into<PublicDir>,
relative_path: impl AsRef<Path>,
mime_type: Option<&str>,
) -> Result<FileUri>
pub async fn create_new_file( &self, volume_id: Option<&StorageVolumeId>, base_dir: impl Into<PublicDir>, 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 app can read/write it until the app is uninstalled. And it is not removed when the app itself is uninstalled.
§Version behavior
§Android 10 or higher.
No permission is required.
Files are automatically registered in the appropriate MediaStore as needed.
Scanning is triggered when the file descriptor is closed
or as part of the pending lifecycle.
§Android 9 or lower
WRITE_EXTERNAL_STORAGE and READ_EXTERNAL_STORAGE permissions are required.
This needs two steps:
-
Declare :
By enabling thelegacy_storage_permissionfeature,
you can declare the permissions only for Android 9 or lower automatically at build time. -
Runtime request :
By callingPublicStorage::request_permission, you can request the permissions from the user at runtime.
After writing content to the file, call PublicStorage::scan_file.
Until then, the file may not appear in the gallery or other apps.
§Args
-
volume_id :
The ID of the storage volume, such as internal storage or an SD card.
Usually, you don’t need to specify this unless there is a special reason.
IfNoneis provided,the primary storage volumewill be used. -
base_dir :
The base directory for the file.
When usingPublicImageDir, only image MIME types should be used for mime_type; using other types may cause errors.
Similarly,PublicVideoDirandPublicAudioDirshould only be used with their respective media types.
OnlyPublicGeneralPurposeDirsupports all MIME types. -
relative_path :
The file path relative to the base directory.
To keep files organized, it is recommended to place your app’s name directory at the top level.
Any missing parent directories will be created automatically.
If a file with the same name already exists, a sequential number is appended to ensure uniqueness.
If the file has no extension, one may be inferred from mime_type and appended to the file name.
Strings may also be sanitized as needed, so they may not be used exactly as provided. Note that append-exntesion and sanitize-path operation may vary depending on the device model and Android version. -
mime_type :
The MIME type of the file to be created.
IfNone, the MIME type will be inferred from the extension of relative_path.
If that also fails,application/octet-streamwill be used.
§Support
All Android version.
Note :
PublicAudioDir::Audiobooksis 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::Recordingsis 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 async fn create_new_file_with_pending(
&self,
volume_id: Option<&StorageVolumeId>,
base_dir: impl Into<PublicDir>,
relative_path: impl AsRef<Path>,
mime_type: Option<&str>,
) -> Result<FileUri>
pub async fn create_new_file_with_pending( &self, volume_id: Option<&StorageVolumeId>, base_dir: impl Into<PublicDir>, 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 app can read/write it until the app is uninstalled. And it is not removed when the app itself is uninstalled.
§Version behavior
§Android 10 or higher
No permission is required.
Files are automatically registered in the appropriate MediaStore as needed.
Scanning is triggered when the file descriptor is closed
or as part of the pending lifecycle.
Diffrences from PublicStorage::create_new_file are that
files are marked as pending and will not be visible to other apps until
PublicStorage::set_pending(..., false) is called.
§Android 9 or lower
This behavior is equal to PublicStorage::create_new_file.
So pending is ignored.
WRITE_EXTERNAL_STORAGE and READ_EXTERNAL_STORAGE permissions are required.
This needs two steps:
-
Declare :
By enabling thelegacy_storage_permissionfeature,
you can declare the permissions only for Android 9 or lower automatically at build time. -
Runtime request :
By callingPublicStorage::request_permission, you can request the permissions from the user at runtime.
After writing content to the file, call PublicStorage::scan_file.
Until then, the file may not appear in the gallery or other apps.
§Args
-
volume_id :
The ID of the storage volume, such as internal storage or an SD card.
Usually, you don’t need to specify this unless there is a special reason.
IfNoneis provided,the primary storage volumewill be used. -
base_dir :
The base directory for the file.
When usingPublicImageDir, only image MIME types should be used for mime_type; using other types may cause errors.
Similarly,PublicVideoDirandPublicAudioDirshould only be used with their respective media types.
OnlyPublicGeneralPurposeDirsupports all MIME types. -
relative_path :
The file path relative to the base directory.
To keep files organized, it is recommended to place your app’s name directory at the top level.
Any missing parent directories will be created automatically.
If a file with the same name already exists, a sequential number is appended to ensure uniqueness.
If the file has no extension, one may be inferred from mime_type and appended to the file name.
Strings may also be sanitized as needed, so they may not be used exactly as provided. Note that append-exntesion and sanitize-path operation may vary depending on the device model and Android version. -
mime_type :
The MIME type of the file to be created.
IfNone, the MIME type will be inferred from the extension of relative_path.
If that also fails,application/octet-streamwill be used.
§Support
All Android version.
Note :
PublicAudioDir::Audiobooksis 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::Recordingsis 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 async fn create_dir_all(
&self,
volume_id: Option<&StorageVolumeId>,
base_dir: impl Into<PublicDir>,
relative_path: impl AsRef<Path>,
) -> Result<()>
pub async fn create_dir_all( &self, volume_id: Option<&StorageVolumeId>, base_dir: impl Into<PublicDir>, 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 and PublicStorage::create_new_file_with_pending
do this automatically, so there is no need to use it together.
§Version behavior
§Android 10 or higher
No permission is required.
§Android 9 or lower
WRITE_EXTERNAL_STORAGE and READ_EXTERNAL_STORAGE permissions are required.
This needs two steps:
-
Declare :
By enabling thelegacy_storage_permissionfeature,
you can declare the permissions only for Android 9 or lower automatically at build time. -
Runtime request :
By callingPublicStorage::request_permission, you can request the permissions from the user at runtime.
§Args
-
volume_id :
ID of the storage volume, such as internal storage, SD card, etc.
IfNoneis provided,the primary storage volumewill be used. -
base_dir :
The base directory. -
relative_path :
The directory path relative to the base directory.
Strings may also be sanitized as needed, so they may not be used exactly as provided. Note that sanitize-path operation may vary depending on the device model and Android version.
§Support
All Android Version.
Note :
PublicAudioDir::Audiobooksis 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::Recordingsis 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 async fn write_new(
&self,
volume_id: Option<&StorageVolumeId>,
base_dir: impl Into<PublicDir>,
relative_path: impl AsRef<Path>,
mime_type: Option<&str>,
contents: impl AsRef<[u8]>,
) -> Result<FileUri>
pub async fn write_new( &self, volume_id: Option<&StorageVolumeId>, base_dir: impl Into<PublicDir>, relative_path: impl AsRef<Path>, mime_type: Option<&str>, contents: impl AsRef<[u8]>, ) -> Result<FileUri>
Writes contents to the new file in the specified public directory of the storage volume.
This returns a persistent read-write URI.
The app can read/write it until the app is uninstalled. And it is not removed when the app itself is uninstalled.
§Version behavior
§Android 10 or higher.
No permission is required.
§Android 9 or lower
WRITE_EXTERNAL_STORAGE and READ_EXTERNAL_STORAGE permissions are required.
This needs two steps:
-
Declare :
By enabling thelegacy_storage_permissionfeature,
you can declare the permissions only for Android 9 or lower automatically at build time. -
Runtime request :
By callingPublicStorage::request_permission, you can request the permissions from the user at runtime.
§Args
-
volume_id :
The ID of the storage volume, such as internal storage or an SD card.
Usually, you don’t need to specify this unless there is a special reason.
IfNoneis provided,the primary storage volumewill be used. -
base_dir :
The base directory for the file.
When usingPublicImageDir, only image MIME types should be used for mime_type; using other types may cause errors.
Similarly,PublicVideoDirandPublicAudioDirshould only be used with their respective media types.
OnlyPublicGeneralPurposeDirsupports all MIME types. -
relative_path :
The file path relative to the base directory.
To keep files organized, it is recommended to place your app’s name directory at the top level.
Any missing parent directories will be created automatically.
If a file with the same name already exists, a sequential number is appended to ensure uniqueness.
If the file has no extension, one may be inferred from mime_type and appended to the file name.
Strings may also be sanitized as needed, so they may not be used exactly as provided. Note that append-exntesion and sanitize-path operation may vary depending on the device model and Android version. -
mime_type :
The MIME type of the file to be created.
IfNone, the MIME type will be inferred from the extension of relative_path.
If that also fails,application/octet-streamwill be used. -
contents :
Contents.
§Support
All Android version.
Note :
PublicAudioDir::Audiobooksis 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::Recordingsis 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 async fn scan_file(&self, uri: &FileUri) -> Result<()>
pub async fn scan_file(&self, uri: &FileUri) -> Result<()>
Scans the specified file.
This ensures that the file will be visible in the Gallery and etc.
You don’t need to call this after PublicStorage::write_new.
§Version behavior
§Android 10 or higher
This function does nothing,
because files are automatically registered in the appropriate MediaStore as needed.
Scanning is triggered when the file descriptor is closed
or as part of the pending lifecycle.
§Android 9 or lower
Requests the specified file to be scanned by MediaStore.
This function returns when the scan request has been initiated.
§Args
- uri :
The target file URI. This must be a URI obtained from one of the following:
§Support
All Android versions.
Sourcepub async fn set_pending(&self, uri: &FileUri, is_pending: bool) -> Result<()>
pub async fn set_pending(&self, uri: &FileUri, is_pending: bool) -> Result<()>
Specifies whether the specified file on PublicStorage is marked as pending.
When set to true, the app has exclusive access to the file, and it becomes invisible to other apps.
If it remains true for more than seven days,
the system will automatically delete the file.
§Version behavior
This is available for Android 10 or higher.
On Android 9 or lower, this does nothing.
§Args
- uri :
Target file URI on PublicStorage. This must be read-writable.
§Support
All Android version.
§References
Sourcepub async fn resolve_path(
&self,
volume_id: Option<&StorageVolumeId>,
base_dir: impl Into<PublicDir>,
) -> Result<PathBuf>
👎Deprecated
pub async fn resolve_path( &self, volume_id: Option<&StorageVolumeId>, base_dir: impl Into<PublicDir>, ) -> 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.
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 debug logger, and etc.
This should not be used in production.
§Version behavior
§Android 11 or higher
You can create files and folders under this directory and read/write the files it creates.
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.
When using PublicImageDir, use only image type for file name extension,
using other type extension or none may cause errors.
Similarly, use only the corresponding extesions for PublicVideoDir and PublicAudioDir.
Only PublicGeneralPurposeDir supports all extensions and no extension.
§Android 10
You can do nothing with this path.
§Android 9 or lower
You can create/read/write files and folders under this directory.
WRITE_EXTERNAL_STORAGE and READ_EXTERNAL_STORAGE permissions are required.
This needs two steps:
-
Declare :
By enabling thelegacy_storage_permissionfeature,
you can declare the permissions only for Android 9 or lower automatically at build time. -
Runtime request :
By callingPublicStorage::request_permission, you can request the permissions from the user at runtime.
§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.
§Args
-
volume_id :
ID of the storage volume, such as internal storage, SD card, etc.
IfNoneis provided,the primary storage volumewill be used. -
base_dir :
The base directory.
§Support
All Android version.
Note :
PublicAudioDir::Audiobooksis 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::Recordingsis 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 async fn resolve_initial_location(
&self,
volume_id: Option<&StorageVolumeId>,
base_dir: impl Into<PublicDir>,
relative_path: impl AsRef<Path>,
create_dir_all: bool,
) -> Result<FileUri>
pub async fn resolve_initial_location( &self, volume_id: Option<&StorageVolumeId>, base_dir: impl Into<PublicDir>, 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_id :
ID of the storage volume, such as internal storage, SD card, etc.
IfNoneis provided,the primary storage volumewill be used. -
base_dir :
The base directory. -
relative_path :
The directory path relative to the base directory.
§Support
All Android version.
Note :
PublicAudioDir::Audiobooksis 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::Recordingsis 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 async fn resolve_initial_location_top(
&self,
volume_id: Option<&StorageVolumeId>,
) -> Result<FileUri>
pub async fn resolve_initial_location_top( &self, volume_id: Option<&StorageVolumeId>, ) -> 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_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.
Note :
PublicAudioDir::Audiobooksis 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::Recordingsis 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 10 (API level 29) or higher, this returns true.
If on Android 9 (API level 28) and lower, this returns false.
§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 12 (API level 31) or higher, this returns true.
If on Android 11 (API level 30) and lower, this returns false.
§Support
All Android version.
Sourcepub fn is_available(&self) -> Result<bool>
👎Deprecated
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.