When a user needs to download a file from the Internet or save a file to another directory, use FilePicker to save the file. Pay attention to the following key points:
Permission Description
The read and write permissions on the file URI granted by Picker is temporary by default, and will be automatically invalidated once the application exits.
If the autoCreateEmptyFile parameter is set to false, the obtained URI has temporary create and delete permissions in addition to temporary read and write permissions.
You can persist the permissions on the URI. For details, see Persisting a Temporary Permission Granted by Picker.
No permission is required if your application uses Picker to save audio clips, images, videos, and document files.
Constraints
When using APIs that require the SystemCapability.FileManagement.UserFileService.FolderSelection system capability, you can call canIUse to check whether the device has this system capability:
if (!canIUse('SystemCapability.FileManagement.UserFileService.FolderSelection')) {
console.error('This API is not supported on this device');
return;
} System Isolation Description
The files saved by the Picker are stored in the specified directory. They are isolated from the assets managed by Gallery and cannot be viewed in Gallery.
To save images and videos to Gallery, use the SaveButton.
PhotoViewPicker will not be maintained in later versions. You are advised to use Media Library Kit to save media assets.
If the security component cannot be called to save images and videos in your development, use PhotoAccessHelper.showAssetsCreationDialog to save images and videos.
Import modules.
import { picker, fileIo } from '@kit.CoreFileKit';
import { BusinessError } from '@kit.BasicServicesKit';
import { common } from '@kit.AbilityKit'; Configure the file saving options DocumentSaveOptions based on your actual service requirements. The following code only lists the configuration reference for each option.
// Create a documentSaveOptions instance. const documentSaveOptions = new picker.DocumentSaveOptions(); // (Optional) Name of the file to save. The default value is empty. documentSaveOptions.newFileNames = ["DocumentViewPicker01.txt"]; // Specify the URI of the file or directory to save (optional). documentSaveOptions.defaultFilePathUri = "file://docs/storage/Users/currentUser/test"; // File type. The value is in ['Suffix type description|Suffix type'] format. To save all files, use 'All files (*.*)|.*' (optional). If multiple suffixes are selected (a maximum of 100 suffixes can be filtered), the first suffix is selected by default. If this parameter is not transferred, no suffix is filtered by default. documentSaveOptions.fileSuffixChoices = ['Document|.txt', '.pdf']; // When saving files, the application determines whether to pre-create empty files. The default value is true, where the Picker pre-creates empty files and returns an array of file URIs. When the value is set to false, no empty files are pre-created, and only an array of file URIs is returned. documentSaveOptions.autoCreateEmptyFile = false;
Create a DocumentViewPicker instance, and call save() to open the FilePicker interface for file saving.
let uris: string[] = [];
let context = this.getUIContext().getHostContext() as common.UIAbilityContext;
const documentViewPicker = new picker.DocumentViewPicker(context);
documentViewPicker.save(documentSaveOptions).then((documentSaveResult: string[]) => {
uris = documentSaveResult;
console.info('documentViewPicker.save to file succeed and uris are:' + uris);
// ···
}).catch((err: BusinessError) => {
console.error(`Invoke documentViewPicker.save failed, code is ${err.code}, message is ${err.message}`);
});
After the UI returns from FilePicker, use fileIo.openSync to open the file through the URI and obtain the file descriptor (FD).
if (uris.length > 0) {
let uri: string = uris[0];
// Note that the interface permission parameter is fileIo.OpenMode.READ_WRITE.
let file = fileIo.openSync(uri, fileIo.OpenMode.READ_WRITE);
console.info('file fd: ' + file.fd);
} Use fileIo.writeSync to edit and modify the file through the FD, and close the FD after the modification is complete.
let writeLen: number = fileIo.writeSync(file.fd, 'hello, world');
console.info('write data to file succeed and size is:' + writeLen);
fileIo.closeSync(file); Import modules.
import { picker, fileIo } from '@kit.CoreFileKit';
import { BusinessError } from '@kit.BasicServicesKit';
import { common } from '@kit.AbilityKit'; Configure the save options.
const audioSaveOptions = new picker.AudioSaveOptions(); // (Optional) Name of the document to save. audioSaveOptions.newFileNames = ['AudioViewPicker01.mp3'];
Create an AudioViewPicker instance and call save() to start the FilePicker page to save the audio clip.
let uris: string[] = [];
let context = this.getUIContext().getHostContext() as common.UIAbilityContext;
const audioViewPicker = new picker.AudioViewPicker(context);
audioViewPicker.save(audioSaveOptions).then((audioSelectResult: string[]) => {
uris = audioSelectResult;
console.info('audioViewPicker.save to file succeed and uri is:' + uris);
// ···
}).catch((err: BusinessError) => {
console.error(`Invoke audioViewPicker.save failed, code is ${err.code}, message is ${err.message}`);
});
After the UI returns from FilePicker, you can use fileIo.openSync to open the file through the URI and obtain the file descriptor (FD).
if (uris.length > 0) {
let uri: string = uris[0];
// Note that the permission specified by the mode parameter of fs.openSync() is fileIo.OpenMode.READ_WRITE.
let file = fileIo.openSync(uri, fileIo.OpenMode.READ_WRITE);
console.info('file fd: ' + file.fd);
} Use fileIo.writeSync to edit and modify the file through the FD, and close the FD after the modification is complete.
let writeLen = fileIo.writeSync(file.fd, 'hello, world');
console.info('write data to file succeed and size is:' + writeLen);
fileIo.closeSync(file); Characteristics
The directory is automatically created in Download/bundle name/.
Files can be directly saved without file selection.
You can create files under the returned URI that has persisting permissions.
Directories created in DOWNLOAD mode are used only to store files. Directories are not isolated from each other. You are not advised to store sensitive application data.
Import modules.
import { fileUri, picker, fileIo } from '@kit.CoreFileKit';
import { BusinessError } from '@kit.BasicServicesKit';
import { common } from '@kit.AbilityKit'; Configure the download mode.
const documentSaveOptions = new picker.DocumentSaveOptions(); // Set pickerMode to DOWNLOAD, which takes precedence over the settings in documentSaveOptions. documentSaveOptions.pickerMode = picker.DocumentPickerMode.DOWNLOAD;
Save the file to the Download directory.
let uri: string = '';
// Obtain the context from the component and ensure that the return value of this.getUIContext().getHostContext() is UIAbilityContext.
let context = this.getUIContext().getHostContext() as common.UIAbilityContext;
const documentViewPicker = new picker.DocumentViewPicker(context);
const documentSaveOptions = new picker.DocumentSaveOptions();
documentSaveOptions.pickerMode = picker.DocumentPickerMode.DOWNLOAD;
documentViewPicker.save(documentSaveOptions).then((documentSaveResult: Array<string>) => {
uri = documentSaveResult[0];
console.info('documentViewPicker.save succeed and uri is:' + uri);
const testFilePath = new fileUri.FileUri(uri + '/test.txt').path;
const file = fileIo.openSync(testFilePath, fileIo.OpenMode.CREATE | fileIo.OpenMode.READ_WRITE);
fileIo.writeSync(file.fd, 'Hello World!');
fileIo.closeSync(file.fd);
}).catch((err: BusinessError) => {
console.error(`Invoke documentViewPicker.save failed, code is ${err.code}, message is ${err.message}`);
})