select-user-file.md 9.3 KB
Newer Older
A
Annie_wang 已提交
1 2
# Selecting User Files

A
Annie_wang 已提交
3
If your application needs to support share and saving of user files (such as images and videos), you can use OpenHarmony [FilePicker](../reference/apis/js-apis-file-picker.md) to implement selection and saving of user files. No permission is required if your application uses **FilePicker** to access files.
A
Annie_wang 已提交
4 5 6

The **FilePicker** provides the following interfaces by file type:

A
Annie_wang 已提交
7
- [PhotoViewPicker](../reference/apis/js-apis-file-picker.md#photoviewpicker): used to select and save images or video files.
A
Annie_wang 已提交
8

A
Annie_wang 已提交
9
- [DocumentViewPicker](../reference/apis/js-apis-file-picker.md#documentviewpicker): used to select and save documents.
A
Annie_wang 已提交
10

A
Annie_wang 已提交
11
- [AudioViewPicker](../reference/apis/js-apis-file-picker.md#audioviewpicker): used to select and save audio files.
A
Annie_wang 已提交
12 13 14

## Selecting Images or Video Files

A
Annie_wang 已提交
15
1. Import the **picker** and **fs** modules.
A
Annie_wang 已提交
16 17 18

   ```ts
   import picker from '@ohos.file.picker';
A
Annie_wang 已提交
19
   import fs from '@ohos.file.fs';
A
Annie_wang 已提交
20 21 22 23 24 25 26 27
   ```

2. Create a **photoSelectOptions** instance.

   ```ts
   const photoSelectOptions = new picker.PhotoSelectOptions();
   ```

A
Annie_wang 已提交
28 29
3. Set the file type and the maximum number of media files to select.<br>
   For example, select a maximum of five images. For details about the media file types, see [PhotoViewMIMETypes](../reference/apis/js-apis-file-picker.md#photoviewmimetypes).
A
Annie_wang 已提交
30 31 32 33 34 35

   ```ts
   photoSelectOptions.MIMEType = picker.PhotoViewMIMETypes.IMAGE_TYPE; // Select images.
   photoSelectOptions.maxSelectNumber = 5; // Set the maximum number of images to select.
   ```

A
Annie_wang 已提交
36 37
4. Create a **photoPicker** instance and call [select()](../reference/apis/js-apis-file-picker.md#select) to open the **FilePicker** page for the user to select files. After the files are selected, [PhotoSelectResult](../reference/apis/js-apis-file-picker.md#photoselectresult) is returned.
   
A
Annie_wang 已提交
38
   The permission on the URIs returned by **select()** is read-only. Further file operations can be performed based on the URIs in the **PhotoSelectResult**. Note that the URI cannot be directly used in the **picker** callback to open a file. You need to define a global variable to save the URI and use a button to trigger file opening.
A
Annie_wang 已提交
39 40

   ```ts
A
Annie_wang 已提交
41
   let uri = null;
A
Annie_wang 已提交
42 43
   const photoViewPicker = new picker.PhotoViewPicker();
   photoViewPicker.select(photoSelectOptions).then((photoSelectResult) => {
A
Annie_wang 已提交
44 45
     uri = photoSelectResult.photoUris[0];
     console.info('photoViewPicker.select to file succeed and uri is:' + uri);
A
Annie_wang 已提交
46
   }).catch((err) => {
A
Annie_wang 已提交
47
     console.error(`Invoke photoViewPicker.select failed, code is ${err.code}, message is ${err.message}`);
A
Annie_wang 已提交
48 49 50
   })
   ```

A
Annie_wang 已提交
51
5. After the UI is returned from the **FilePicker** page, use a button to trigger API calling. Use [fs.openSync()](../reference/apis/js-apis-file-fs.md#fsopensync) to open the file based on the URI and obtain the file descriptor (FD). Note that the **mode** parameter of **fs.openSync()** must be **fs.OpenMode.READ_ONLY**.
A
Annie_wang 已提交
52 53

   ```ts
A
Annie_wang 已提交
54
   let file = fs.openSync(uri, fs.OpenMode.READ_ONLY);
A
Annie_wang 已提交
55 56 57
   console.info('file fd: ' + file.fd);
   ```

A
Annie_wang 已提交
58
6. Use [fs.readSync()](../reference/apis/js-apis-file-fs.md#readsync) to read the file based on the FD. After the data is read, close the FD.
A
Annie_wang 已提交
59 60

   ```ts
A
Annie_wang 已提交
61 62 63
   let buffer = new ArrayBuffer(4096);
   let readLen = fs.readSync(file.fd, buffer);
   console.info('readSync data to file succeed and buffer size is:' + readLen);
A
Annie_wang 已提交
64
   fs.closeSync(file);
A
Annie_wang 已提交
65 66 67 68
   ```

## Selecting Documents

A
Annie_wang 已提交
69
1. Import the **picker** and **fs** modules.
A
Annie_wang 已提交
70 71 72

   ```ts
   import picker from '@ohos.file.picker';
A
Annie_wang 已提交
73
   import fs from '@ohos.file.fs';
A
Annie_wang 已提交
74 75 76 77 78 79 80 81
   ```

2. Create a **documentSelectOptions** instance.

   ```ts
   const documentSelectOptions = new picker.DocumentSelectOptions(); 
   ```

A
Annie_wang 已提交
82 83
3. Create a **documentViewPicker** instance, and call [**select()**](../reference/apis/js-apis-file-picker.md#select-3) to open the **FilePicker** page for the user to select documents. After the documents are selected, a result set containing the file URIs is returned.
   
A
Annie_wang 已提交
84
   The permission on the URIs returned by **select()** is read-only. Further file operations can be performed based on the URIs in the result set. Note that the URI cannot be directly used in the **picker** callback to open a file. You need to define a global variable to save the URI and use a button to trigger file opening.
A
Annie_wang 已提交
85
   
A
Annie_wang 已提交
86
   For example, you can use [file management APIs](../reference/apis/js-apis-file-fs.md) to obtain file attributes, such as the file size, access time, and last modification time, based on the URI. If you need to obtain the file name, use [startAbilityForResult](../../application-dev/application-models/uiability-intra-device-interaction.md).
A
Annie_wang 已提交
87

A
Annie_wang 已提交
88 89 90 91 92
   > **NOTE**
   >
   > Currently, **DocumentSelectOptions** is not configurable. By default, all types of user files are selected.

   ```ts
A
Annie_wang 已提交
93
   let uri = null;
A
Annie_wang 已提交
94
   const documentViewPicker = new picker.DocumentViewPicker(); // Create a documentViewPicker instance.
A
Annie_wang 已提交
95
   documentViewPicker.select(documentSelectOptions).then((documentSelectResult) => {
A
Annie_wang 已提交
96 97
     uri = documentSelectResult[0];
     console.info('documentViewPicker.select to file succeed and uri is:' + uri);
A
Annie_wang 已提交
98
   }).catch((err) => {
A
Annie_wang 已提交
99
     console.error(`Invoke documentViewPicker.select failed, code is ${err.code}, message is ${err.message}`);
A
Annie_wang 已提交
100 101 102 103 104
   })
   ```

   > **NOTE**
   >
A
Annie_wang 已提交
105
   > Currently, **DocumentSelectOptions** cannot be used to obtain the file name. To obtain the file name, use **startAbilityForResult()**.
A
Annie_wang 已提交
106 107 108 109 110 111 112 113 114 115 116

   ```ts
   let config = {
     action: 'ohos.want.action.OPEN_FILE',
     parameters: {
       startMode: 'choose',
     }
   }
   try {
     let result = await context.startAbilityForResult(config, {windowMode: 1});
     if (result.resultCode !== 0) {
A
Annie_wang 已提交
117
       console.error(`documentViewPicker.select failed, code is ${result.resultCode}, message is ${result.want.parameters.message}`);
A
Annie_wang 已提交
118 119 120 121 122 123 124
       return;
     }
     // Obtain the URI of the document.
     let select_item_list = result.want.parameters.select_item_list;
     // Obtain the name of the document.
     let file_name_list = result.want.parameters.file_name_list;
   } catch (err) {
A
Annie_wang 已提交
125
     console.error(`Invoke documentViewPicker.select failed, code is ${err.code}, message is ${err.message}`);
A
Annie_wang 已提交
126
   }
A
Annie_wang 已提交
127 128
   ```

A
Annie_wang 已提交
129
4. After the UI is returned from the **FilePicker** page, use a button to trigger API calling. Use [fs.openSync()](../reference/apis/js-apis-file-fs.md#fsopensync) to open the file based on the URI and obtain the FD. Note that the **mode** parameter of **fs.openSync()** must be **fs.OpenMode.READ_ONLY**.
A
Annie_wang 已提交
130 131

   ```ts
A
Annie_wang 已提交
132
   let file = fs.openSync(uri, fs.OpenMode.READ_ONLY);
A
Annie_wang 已提交
133 134 135
   console.info('file fd: ' + file.fd);
   ```

A
Annie_wang 已提交
136
5. Use [fs.readSync()](../reference/apis/js-apis-file-fs.md#readsync) to read the file based on the FD. After the data is read, close the FD.
A
Annie_wang 已提交
137 138

   ```ts
A
Annie_wang 已提交
139 140 141
   let buffer = new ArrayBuffer(4096);
   let readLen = fs.readSync(file.fd, buffer);
   console.info('readSync data to file succeed and buffer size is:' + readLen);
A
Annie_wang 已提交
142 143 144 145
   fs.closeSync(file);
   ```


A
Annie_wang 已提交
146
## Selecting Audio Files
A
Annie_wang 已提交
147

A
Annie_wang 已提交
148
1. Import the **picker** and **fs** modules.
A
Annie_wang 已提交
149 150 151

   ```ts
   import picker from '@ohos.file.picker';
A
Annie_wang 已提交
152
   import fs from '@ohos.file.fs';
A
Annie_wang 已提交
153 154 155 156 157 158 159 160
   ```

2. Create an **audioSelectOptions** instance.

   ```ts
   const audioSelectOptions = new picker.AudioSelectOptions();
   ```

A
Annie_wang 已提交
161 162
3. Create an **audioViewPicker** instance, and call [**select()**](../reference/apis/js-apis-file-picker.md#select-6) to open the **FilePicker** page for the user to select audio files. After the files are selected, a result set containing the URIs of the audio files selected is returned.
   
A
Annie_wang 已提交
163
   The permission on the URIs returned by **select()** is read-only. Further file operations can be performed based on the URIs in the result set. Note that the URI cannot be directly used in the **picker** callback to open a file. You need to define a global variable to save the URI and use a button to trigger file opening.
A
Annie_wang 已提交
164
   
A
Annie_wang 已提交
165
   For example, use the [file management interface](../reference/apis/js-apis-file-fs.md) to obtain the file handle (FD) of the audio clip based on the URI, and then develop the audio playback function based on the media service. For details, see [Audio Playback Development](../media/audio-playback-overview.md).
A
Annie_wang 已提交
166 167 168 169 170 171

   > **NOTE**
   >
   > Currently, **AudioSelectOptions** is not configurable. By default, all types of user files are selected.

   ```ts
A
Annie_wang 已提交
172
   let uri = null;
A
Annie_wang 已提交
173
   const audioViewPicker = new picker.AudioViewPicker();
A
Annie_wang 已提交
174
   audioViewPicker.select(audioSelectOptions).then(audioSelectResult => {
A
Annie_wang 已提交
175 176
     uri = audioSelectOptions[0];
     console.info('audioViewPicker.select to file succeed and uri is:' + uri);
A
Annie_wang 已提交
177
   }).catch((err) => {
A
Annie_wang 已提交
178
     console.error(`Invoke audioViewPicker.select failed, code is ${err.code}, message is ${err.message}`);
A
Annie_wang 已提交
179 180 181
   })
   ```

A
Annie_wang 已提交
182
4. After the UI is returned from the **FilePicker** page, use a button to trigger API calling. Use [fs.openSync()](../reference/apis/js-apis-file-fs.md#fsopensync) to open the file based on the URI and obtain the FD. Note that the **mode** parameter of **fs.openSync()** must be **fs.OpenMode.READ_ONLY**.
A
Annie_wang 已提交
183 184

   ```ts
A
Annie_wang 已提交
185
   let file = fs.openSync(uri, fs.OpenMode.READ_ONLY);
A
Annie_wang 已提交
186 187 188
   console.info('file fd: ' + file.fd);
   ```

A
Annie_wang 已提交
189
5. Use [fs.readSync()](../reference/apis/js-apis-file-fs.md#readsync) to read the file based on the FD. After the data is read, close the FD.
A
Annie_wang 已提交
190 191

   ```ts
A
Annie_wang 已提交
192 193 194
   let buffer = new ArrayBuffer(4096);
   let readLen = fs.readSync(file.fd, buffer);
   console.info('readSync data to file succeed and buffer size is:' + readLen);
A
Annie_wang 已提交
195
   fs.closeSync(file);
A
Annie_wang 已提交
196
   ```