1# Selecting User Files 2<!--Kit: Core File Kit--> 3<!--Subsystem: FileManagement--> 4<!--Owner: @wang_zhangjun; @zhuangzhuang--> 5<!--Designer: @wang_zhangjun; @zhuangzhuang; @renguang1116--> 6<!--Tester: @liuhonggang123; @yue-ye2; @juxiaopang--> 7<!--Adviser: @foryourself--> 8 9You can use [FilePicker](../reference/apis-core-file-kit/js-apis-file-picker.md) to implement the capabilities required for sharing user files and saving images and videos. When Picker is used to access a file, the related application will be started and guide the user to complete related operation on the UI. The caller does not require any permission. The permission on the file URI granted by Picker, however, is temporary. If required, you can persist the permission on the URI. For details, see [Persisting a Temporary Permission Granted by Picker](file-persistPermission.md#persisting-a-temporary-permission-granted-by-picker). 10 11**FilePicker** provides the following types of Pickers by file type: 12 13- [PhotoViewPicker](../reference/apis-core-file-kit/js-apis-file-picker.md#photoviewpickerdeprecated): used to select and save images and videos. However, the APIs of this Picker will not be maintained in later versions. Use [PhotoViewPicker of PhotoAccessHelper](../reference/apis-media-library-kit/arkts-apis-photoAccessHelper-PhotoViewPicker.md) to select image files. and the [security component to save them](../media/medialibrary/photoAccessHelper-savebutton.md). 14 15- [DocumentViewPicker](../reference/apis-core-file-kit/js-apis-file-picker.md#documentviewpicker): used to select and save documents. The **DocumentViewPicker** API triggers the **FilePicker** application. Documents are not distinguished by file name extensions. For example, the images and files downloaded from a browser are documents. 16 17- [AudioViewPicker](../reference/apis-core-file-kit/js-apis-file-picker.md#audioviewpicker): used to select and save audio clips. The **AudioViewPicker** API triggers the **AudioPicker** application. 18 19## Selecting Images or Videos 20 21[PhotoViewPicker](../reference/apis-core-file-kit/js-apis-file-picker.md#photoviewpickerdeprecated) will not be maintained in later versions. You are advised to use [PhotoViewPicker of PhotoAccessHelper](../reference/apis-media-library-kit/arkts-apis-photoAccessHelper-PhotoViewPicker.md) to select images. 22 23## Selecting Documents 24 251. Import modules. 26 27 ```ts 28 import { picker } from '@kit.CoreFileKit'; 29 import { fileIo as fs } from '@kit.CoreFileKit'; 30 import { common } from '@kit.AbilityKit'; 31 import { BusinessError } from '@kit.BasicServicesKit'; 32 ``` 33 342. Create a **DocumentSelectOptions** instance. 35 36 ```ts 37 const documentSelectOptions = new picker.DocumentSelectOptions(); 38 // Optional. Set the maximum number of documents that can be selected. 39 documentSelectOptions.maxSelectNumber = 5; 40 // Optional. Specify the path of the files or folder to select. 41 documentSelectOptions.defaultFilePathUri = "file://docs/storage/Users/currentUser/test"; 42 // (Optional. If this parameter is not transferred, all files are displayed by default.) Set the file name extension types ['File name extension description|File name extension type'] that can be selected. (Optional) Use a comma to separate multiple file name extensions, which cannot exceed 100. The wildcard ['All files (*.*)|.*'] is available for 2-in-1 devices (for mobile phones since API version 17). 43 documentSelectOptions.fileSuffixFilters = ['Image(.png, .jpg)|.png, .jpg', 'Document|.txt', 'Video|.mp4', '.pdf']; 44 // Whether to grant the permission for the specified files or directory. The value true means to grant the permission, and the value false (default) means the opposite. If this parameter is true, defaultFilePathUri is mandatory and the file management authorization page is displayed. If this parameter is false, a common file management page is displayed. This parameter is optional and only 2-in-1 devices are supported. 45 documentSelectOptions.authMode = false; 46 // Whether to enable the batch authorization mode. The value true means to enable the batch authorization mode, and the value false (default) means the opposite. When multAuthMode is set to true, only the multiUriArray parameter takes effect. Only mobile phones are supported. 47 documentSelectOptions.multiAuthMode = false; 48 // Whether to pass the URIs for batch authorization. (Only files are supported and folders are not supported.) This parameter does not take effect when multAuthMode is set to false. Only mobile phones are supported. 49 documentSelectOptions.multiUriArray = ["file://docs/storage/Users/currentUser/test", "file://docs/storage/Users/currentUser/2test"]; 50 // Whether to enable the aggregation view mode to launch the file management application. The value DEFAULT means that this parameter does not take effect and the aggregation view mode is disabled. Values other than DEFAULT indicate that other parameters do not take effect. Only mobile phones are supported. 51 documentSelectOptions.mergeMode = picker.MergeTypeMode.DEFAULT; 52 // Whether to support encryption (only files are supported). The default value is false. If this parameter is set to true, files can be encrypted on the Picker page. Note that this parameter is supported since API version 19. 53 documentSelectOptions.isEncryptionSupported = false; 54 ``` 55 563. Create a [DocumentViewPicker](../reference/apis-core-file-kit/js-apis-file-picker.md#documentviewpicker) instance, and call [select()](../reference/apis-core-file-kit/js-apis-file-picker.md#select-3) to start the FilePicker application page for the user to select documents. 57 58 ```ts 59 let uris: Array<string> = []; 60 // Obtain the context from the component and ensure that the return value of this.getUIContext().getHostContext() is UIAbilityContext. 61 let context = this.getUIContext().getHostContext() as common.UIAbilityContext; 62 // Create a DocumentViewPicker instance. 63 const documentViewPicker = new picker.DocumentViewPicker(context); 64 documentViewPicker.select(documentSelectOptions).then((documentSelectResult: Array<string>) => { 65 // After the files are selected, a result set containing the file URIs is returned. 66 uris = documentSelectResult; 67 console.info('documentViewPicker.select to file succeed and uris are:' + uris); 68 }).catch((err: BusinessError) => { 69 console.error(`Invoke documentViewPicker.select failed, code is ${err.code}, message is ${err.message}`); 70 }) 71 ``` 72 73 > **NOTE** 74 > 75 > - The permission for the URI returned by [select()](../reference/apis-core-file-kit/js-apis-file-picker.md#select-3) of Picker is a temporary read-only permission. The temporary permission will be invalidated once the application exits.<br> 76 > - You can persist the temporary permission for a URI. For details, see [Persisting a Temporary Permission Granted by Picker](file-persistPermission.md#persisting-a-temporary-permission-granted-by-picker).<br> 77 > - Further operations can be performed on the documents based on the file URIs returned in the result set. You are advised to define a global variable to save the URI.<br> 78 > - If metadata needs to be obtained, you can use the [@ohos.file.fs](../reference/apis-core-file-kit/js-apis-file-fs.md) and [@ohos.file.fileuri](../reference/apis-core-file-kit/js-apis-file-fileuri.md) APIs to obtain document attribute information, such as the document name, size, access time, modification time, and path, based on the URI. 79 804. After the application UI is returned from FilePicker, call [fs.openSync](../reference/apis-core-file-kit/js-apis-file-fs.md#fsopensync) to open a document based on the URI. The file descriptor (FD) is returned after the document is opened. 81 82 ```ts 83 let uri: string = ''; 84 // Note that the mode parameter of fs.openSync() is fs.OpenMode.READ_ONLY. 85 let file = fs.openSync(uri, fs.OpenMode.READ_ONLY); 86 console.info('file fd: ' + file.fd); 87 ``` 88 895. Call [fs.readSync](../reference/apis-core-file-kit/js-apis-file-fs.md#readsync) to read data from the document based on the FD. 90 91 ```ts 92 let buffer = new ArrayBuffer(4096); 93 let readLen = fs.readSync(file.fd, buffer); 94 console.info('readSync data to file succeed and buffer size is:' + readLen); 95 // Close the FD after the data is read. 96 fs.closeSync(file); 97 ``` 98 99## Selecting Audio Clips 100 1011. Import modules. 102 103 ```ts 104 import { picker } from '@kit.CoreFileKit'; 105 import { fileIo as fs } from '@kit.CoreFileKit'; 106 import { BusinessError } from '@kit.BasicServicesKit'; 107 import { common } from '@kit.AbilityKit'; 108 ``` 109 1102. Create an **AudioSelectOptions** instance. 111 112 > **NOTE** 113 > 114 > Currently, **AudioSelectOptions** is not configurable. By default, all types of user files are selected. 115 116 ```ts 117 const audioSelectOptions = new picker.AudioSelectOptions(); 118 ``` 119 1203. Create an [AudioViewPicker](../reference/apis-core-file-kit/js-apis-file-picker.md#audioviewpicker) instance, and call [select()](../reference/apis-core-file-kit/js-apis-file-picker.md#select-5) to start the AudioPicker application page for the user to select audio clips. 121 122 ```ts 123 let uris: string = ''; 124 // Obtain the context from the component and ensure that the return value of this.getUIContext().getHostContext() is UIAbilityContext. 125 let context = this.getUIContext().getHostContext() as common.UIAbilityContext; 126 const audioViewPicker = new picker.AudioViewPicker(context); 127 audioViewPicker.select(audioSelectOptions).then((audioSelectResult: Array<string>) => { 128 // After the files are selected, a result set containing the URIs of the audio files selected is returned. 129 uris = audioSelectResult[0]; 130 console.info('audioViewPicker.select to file succeed and uri is:' + uris); 131 }).catch((err: BusinessError) => { 132 console.error(`Invoke audioViewPicker.select failed, code is ${err.code}, message is ${err.message}`); 133 }) 134 ``` 135 136 > **NOTE** 137 > 138 > - The permission for the URI returned by [select()](../reference/apis-core-file-kit/js-apis-file-picker.md#select-3) of Picker is a temporary read-only permission. The temporary permission will be invalidated once the application exits.<br> 139 > - You can persist the temporary permission for a URI. For details, see [Persisting a Temporary Permission Granted by Picker](file-persistPermission.md#persisting-a-temporary-permission-granted-by-picker).<br> 140 > - You can read file data based on the URI. You are advised to define a global variable to save the URI. For example, you can use the [@ohos.file.fs](../reference/apis-core-file-kit/js-apis-file-fs.md) API to obtain the FD of the audio clip based on the URI, and then develop the audio playback application with the media service. For details, see [Audio Playback Development](../media/audio/audio-playback-overview.md). 141 1424. After the application UI is returned from AudioPicker, call [fs.openSync](../reference/apis-core-file-kit/js-apis-file-fs.md#fsopensync) to open an audio clip based on the URI. The FD is returned after the audio clip is opened. 143 144 ```ts 145 let uri: string = ''; 146 // Note that the mode parameter of fs.openSync() is fs.OpenMode.READ_ONLY. 147 let file = fs.openSync(uri, fs.OpenMode.READ_ONLY); 148 console.info('file fd: ' + file.fd); 149 ``` 150 1515. Call [fs.readSync](../reference/apis-core-file-kit/js-apis-file-fs.md#readsync) to read data from the audio clip based on the FD. 152 153 ```ts 154 let buffer = new ArrayBuffer(4096); 155 let readLen = fs.readSync(file.fd, buffer); 156 console.info('readSync data to file succeed and buffer size is:' + readLen); 157 // Close the FD after the data is read. 158 fs.closeSync(file); 159 ``` 160 161<!--RP1--><!--RP1End--> 162