harmony 鸿蒙Saving User Files
Saving User Files
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.
- You can persist the permissions on the URI. For details, see Persisting a Temporary Permission Granted by Picker. (This operation is available only for 2-in-1 devices.)
- No permission is required if your application uses Picker to save audio clips, images, videos, and document files.
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 a security component.
Saving Images or Videos
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.
Saving Documents
- Import modules.
import { picker } from '@kit.CoreFileKit';
import { fileIo as fs } from '@kit.CoreFileKit';
import { BusinessError } from '@kit.BasicServicesKit';
import { common } from '@kit.AbilityKit';
- Configure the save options.
// 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"];
// (Optional) Type of the document to save. The value is in ['Description|File name extensions'] format. To save all files, use 'All files (*.*)|.*'. If there are multiple file name extensions (a maximum of 100 extensions can be filtered), the first one is used by default. If this parameter is not specified, no extension is filtered by default.
documentSaveOptions.fileSuffixChoices = ['Document|.txt', '.pdf'];
- Create a DocumentViewPicker instance, and call save() to start the FilePicker page to save the document.
let uris: Array<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);
documentViewPicker.save(documentSaveOptions).then((documentSaveResult: Array<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}`);
})
NOTE
URI storage:
You should avoid directly using a URI in the Picker callback.
You are advised to define a global variable to save the URI for future use.
Quick saving:
You can directly access the download directory in DOWNLOAD mode.
- After the application UI is returned from FilePicker, you can call fs.openSync to open a document based on the URI. The FD is returned after the document is opened.
const uri = '';
// Note that the permission specified by the mode parameter of fs.openSync() is fs.OpenMode.READ_WRITE.
let file = fs.openSync(uri, fs.OpenMode.READ_WRITE);
console.info('file fd: ' + file.fd);
- Call fs.writeSync to modify the document based on the FD, and call fs.closeSync() to close the FD.
let writeLen: number = fs.writeSync(file.fd, 'hello, world');
console.info('write data to file succeed and size is:' + writeLen);
fs.closeSync(file);
Saving Audio Clips
- Import modules.
import { picker } from '@kit.CoreFileKit';
import { fileIo as fs } 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 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 audioViewPicker = new picker.AudioViewPicker(context);
audioViewPicker.save(audioSaveOptions).then((audioSelectResult: Array<string>) => {
uri = audioSelectResult[0];
console.info('audioViewPicker.save to file succeed and uri is:' + uri);
}).catch((err: BusinessError) => {
console.error(`Invoke audioViewPicker.save failed, code is ${err.code}, message is ${err.message}`);
})
NOTE
URI storage:
You should avoid directly using a URI in the Picker callback.
You are advised to define a global variable to save the URI for future use.
Quick saving:
You can directly access the download directory in DOWNLOAD mode.
- After the application UI is returned from FilePicker, call fs.openSync to open an audio clip based on the URI. The FD is returned after the audio clip is opened.
// Note that the permission specified by the mode parameter of fs.openSync() is fileIo.OpenMode.READ_WRITE.
let file = fs.openSync(uri, fs.OpenMode.READ_WRITE);
console.info('file fd: ' + file.fd);
- Call fs.writeSync to modify the document based on the FD, and call fs.closeSync() to close the FD.
let writeLen = fs.writeSync(file.fd, 'hello, world');
console.info('write data to file succeed and size is:' + writeLen);
fs.closeSync(file);
Saving Files to the Download Directory
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.
Import modules.
import { fileUri, picker } from '@kit.CoreFileKit';
import { fileIo as fs } 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 = fs.openSync(testFilePath, fs.OpenMode.CREATE|fs.OpenMode.READ_WRITE);
fs.writeSync(file.fd, 'Hello World!');
fs.closeSync(file.fd);
}).catch((err: BusinessError) => {
console.error(`Invoke documentViewPicker.save failed, code is ${err.code}, message is ${err.message}`);
})
你可能感兴趣的鸿蒙文章
harmony 鸿蒙Accessing Application Files (ArkTS)
harmony 鸿蒙Accessing Backup and Restore
harmony 鸿蒙Application Data Backup and Restore Overview
harmony 鸿蒙Backup and Restore Triggered by System Applications
harmony 鸿蒙Application File Overview
harmony 鸿蒙Obtaining Application and File System Space Statistics
harmony 鸿蒙Introduction to Core File Kit
harmony 鸿蒙Developing a File Manager Application (for System Applications Only)
- 所属分类: 后端技术
- 本文标签:
热门推荐
-
2、 - 优质文章
-
3、 gate.io
-
8、 golang
-
9、 openharmony
-
10、 Vue中input框自动聚焦