1# Selecting User Files
2
3You 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).
4
5**FilePicker** provides the following types of Pickers by file type:
6
7- [PhotoViewPicker](../reference/apis-core-file-kit/js-apis-file-picker.md#photoviewpicker): used to select and save images and videos. However, the APIs of this Picker will not be maintained in later versions. You are advised to use [PhotoViewPicker of PhotoAccessHelper](../media/medialibrary/photoAccessHelper-photoviewpicker.md) to select images, and use [security components to create media assets](../media/medialibrary/photoAccessHelper-savebutton.md).
8
9- [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.
10
11- [AudioViewPicker](../reference/apis-core-file-kit/js-apis-file-picker.md#audioviewpicker): used to select and save audio clips. The **AudioViewPicker** API triggers the **FilePicker** application.
12
13## Selecting Images or Videos
14
15[PhotoViewPicker](../reference/apis-core-file-kit/js-apis-file-picker.md#photoviewpicker) will not be maintained in later versions. You are advised to use [PhotoViewPicker of PhotoAccessHelper](../media/medialibrary/photoAccessHelper-photoviewpicker.md) to select images.
16
17## Selecting Documents
18
191. Import modules.
20
21   ```ts
22   import  { picker } from '@kit.CoreFileKit';
23   import { fileIo as fs } from '@kit.CoreFileKit';
24   import { common } from '@kit.AbilityKit';
25   import { BusinessError } from '@kit.BasicServicesKit';
26   ```
27
282. Create a **DocumentSelectOptions** instance.
29
30   ```ts
31   const documentSelectOptions = new picker.DocumentSelectOptions();
32   // (Optional) Set the maximum number of documents that can be selected.
33   documentSelectOptions.maxSelectNumber = 5;
34   // (Optional) Specify the path of the files or folder to select.
35   documentSelectOptions.defaultFilePathUri = "file://docs/storage/Users/currentUser/test";
36   // (Optional) Set the file name extension types ['File name extension description|File name extension type'] that can be selected. Use a comma to separate multiple file name extensions, which cannot exceed 100. To select all files, use 'All files(*.*)|.*'.
37    documentSelectOptions.fileSuffixFilters = ['Image(.png, .jpg)|.png, .jpg', 'Document|.txt', 'Video|.mp4', '.pdf'];
38   // Whether to grant the permission for the specified files or folder. The value true means to grant the permission, the value false 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.
39   documentSelectOptions.authMode = true;
40   ```
41
423. 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.
43
44   ```ts
45   let uris: Array<string> = [];
46   let context = getContext (this) as common.Context; // Ensure that getContext (this) returns UIAbilityContext.
47   // Create a DocumentViewPicker instance.
48   const documentViewPicker = new picker.DocumentViewPicker(context);
49   documentViewPicker.select(documentSelectOptions).then((documentSelectResult: Array<string>) => {
50     // After the user selects documents, a result set containing the document URIs is returned.
51     uris = documentSelectResult;
52     console.info('documentViewPicker.select to file succeed and uris are:' + uris);
53   }).catch((err: BusinessError) => {
54     console.error(`Invoke documentViewPicker.select failed, code is ${err.code}, message is ${err.message}`);
55   })
56   ```
57
58  > **NOTE**
59  >
60  > - 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.
61  >
62  > - You can persist the temporary permission for a URI. This operation is available only for 2-in-1 devices. For details, see [Persisting a Temporary Permission Granted by Picker](file-persistPermission.md#persisting-a-temporary-permission-granted-by-picker).
63  >
64  > - 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.
65  >
66  > - 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.
67
684. 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 obtained.
69
70   ```ts
71   let uri: string = '';
72   // Note that the mode parameter of fs.openSync() is fs.OpenMode.READ_ONLY.
73   let file = fs.openSync(uri, fs.OpenMode.READ_ONLY);
74   console.info('file fd: ' + file.fd);
75   ```
76
775. Call [fs.readSync](../reference/apis-core-file-kit/js-apis-file-fs.md#readsync) to read data from the document based on the FD.
78
79   ```ts
80   let buffer = new ArrayBuffer(4096);
81   let readLen = fs.readSync(file.fd, buffer);
82   console.info('readSync data to file succeed and buffer size is:' + readLen);
83   // Close the FD after the data is read.
84   fs.closeSync(file);
85   ```
86
87## Selecting Audio Clips
88
891. Import modules.
90
91   ```ts
92   import  { picker } from '@kit.CoreFileKit';
93   import { fileIo as fs } from '@kit.CoreFileKit';
94   import { BusinessError } from '@kit.BasicServicesKit';
95   import { common } from '@kit.AbilityKit';
96   ```
97
982. Create an **AudioSelectOptions** instance.
99
100   > **NOTE**
101   >
102   > Currently, **AudioSelectOptions** is not configurable. By default, all types of user files are selected.
103
104   ```ts
105   const audioSelectOptions = new picker.AudioSelectOptions();
106   ```
107
1083. 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-6) to start the FilePicker application page for the user to select audio clips.
109
110   ```ts
111   let uris: string = '';
112   // Ensure that getContext(this) returns UIAbilityContext.
113   let context = getContext(this) as common.Context;
114   const audioViewPicker = new picker.AudioViewPicker(context);
115   audioViewPicker.select(audioSelectOptions).then((audioSelectResult: Array<string>) => {
116     // After the user selects audio clips, a result set containing the URIs of the audio clips selected is returned.
117     uris = audioSelectResult[0];
118     console.info('audioViewPicker.select to file succeed and uri is:' + uris);
119   }).catch((err: BusinessError) => {
120     console.error(`Invoke audioViewPicker.select failed, code is ${err.code}, message is ${err.message}`);
121   })
122   ```
123
124  > **NOTE**
125  >
126  > - 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.
127  >
128  > - You can persist the temporary permission for a URI. This operation is available only for 2-in-1 devices. For details, see [Persisting a Temporary Permission Granted by Picker](file-persistPermission.md#persisting-a-temporary-permission-granted-by-picker).
129  >
130  > - 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).
131
1324. After the application UI is returned from FilePicker, 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 obtained.
133
134   ```ts
135   let uri: string = '';
136   // Note that the mode parameter of fs.openSync() is fs.OpenMode.READ_ONLY.
137   let file = fs.openSync(uri, fs.OpenMode.READ_ONLY);
138   console.info('file fd: ' + file.fd);
139   ```
140
1415. 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.
142
143   ```ts
144   let buffer = new ArrayBuffer(4096);
145   let readLen = fs.readSync(file.fd, buffer);
146   console.info('readSync data to file succeed and buffer size is:' + readLen);
147   // Close the FD after the data is read.
148   fs.closeSync(file);
149   ```
150
151<!--RP1--><!--RP1End-->
152