• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Saving User Files
2
3When a user needs to download a file from the network to a local directory or save a user file into another directory, use **FilePicker** to save the file.
4
5The operations for saving images, audio or video clips, and documents are similar. Call **save()** of the corresponding picker instance and pass in **saveOptions**. No permission is required if your application uses **FilePicker** to access files.
6
7Currently, all the **save()** APIs of **FilePicker** can be perceived by users. Specifically, **FilePicker** is started to save the files to a directory managed by **FileManager**. The files are isolated from the assets managed by **Gallery** and cannot be viewed in **Gallery**.
8
9To enable the saved image or video to be viewed in **Gallery**, [create the media asset using a security component](./photoAccessHelper-resource-guidelines.md#creating-a-media-asset-using-a-security-component).
10
11
12## Saving Images or Videos
13
14For example, select an image from **Gallery** and save it.
15
161. Import the [picker](../reference/apis/js-apis-file-picker.md), [fs](../reference/apis/js-apis-file-fs.md), [photoAccessHelper](../reference/apis/js-apis-photoAccessHelper.md), and [dataSharePredicates](../reference/apis/js-apis-data-dataSharePredicates.md) modules.
17
18   ```ts
19   import picker from '@ohos.file.picker';
20   import fs from '@ohos.file.fs';
21   import photoAccessHelper from '@ohos.file.photoAccessHelper';
22   import dataSharePredicates from '@ohos.data.dataSharePredicates';
23   ```
24
252. Obtain the thumbnail of the first image in the device. Before performing this operation, ensure that the device has at least one image.
26
27   ```ts
28   import common from '@ohos.app.ability.common';
29   import image from '@ohos.multimedia.image';
30   import { BusinessError } from '@ohos.base';
31
32   let context = getContext(this) as common.UIAbilityContext;
33   let phAccessHelper = photoAccessHelper.getPhotoAccessHelper(context);
34
35   let pixelmapArrayBuffer: ArrayBuffer;
36   async function getPixelmap(): Promise<void> {
37      try {
38         let predicates = new dataSharePredicates.DataSharePredicates();
39         let fetchOption: photoAccessHelper.FetchOptions = {
40            fetchColumns: [],
41            predicates: predicates
42         };
43         let fetchResult = await phAccessHelper.getAssets(fetchOption);
44         console.info('[picker] getThumbnail fetchResult: ' + fetchResult);
45         const asset = await fetchResult.getFirstObject();
46         console.info('[picker] getThumbnail asset displayName = ', asset.displayName);
47         asset.getThumbnail().then((pixelMap: image.PixelMap) => {
48            let pixelBytesNumber = pixelMap.getPixelBytesNumber();
49            const readBuffer = new ArrayBuffer(pixelBytesNumber);
50            pixelMap.readPixelsToBuffer(readBuffer).then(() => {
51               pixelmapArrayBuffer = readBuffer;
52            })
53         }).catch((err: BusinessError) => {
54            console.error('[picker] getThumbnail failed with error: ' + JSON.stringify(err));
55         });
56      } catch (error) {
57         let err: BusinessError = error as BusinessError;
58         console.error('[picker] getThumbnail error = ' + JSON.stringify(err));
59      }
60   }
61   ```
62
633. Create a **photoViewPicker** instance and call [save()](../reference/apis/js-apis-file-picker.md#save) to start the **FilePicker** page to save the image. After the user selects the destination folder, the image is saved and the URI of the saved image is returned.
64
65   The permission on the URI returned by **save()** is read/write. Further file operations can be performed based on the URI. 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.
66
67   ```ts
68   import { BusinessError } from '@ohos.base';
69
70   let uris: Array<string> = [];
71   async function photoViewPickerSave(): Promise<void>{
72      try {
73         const photoSaveOptions = new picker.PhotoSaveOptions(); // Create a photoSaveOptions instance.
74         photoSaveOptions.newFileNames = ["PhotoViewPicker01.png"]; // (Optional) Name of the file to be saved. The file name in the square brackets can be customized and must be unique. If the file name already exists in the device, change the file name. Otherwise, an error will be returned.
75
76         const photoViewPicker = new picker.PhotoViewPicker();
77         try {
78            let photoSaveResult = await photoViewPicker.save(photoSaveOptions);
79            if (photoSaveResult != undefined) {
80               uris = photoSaveResult;
81               console.info('photoViewPicker.save to file succeed and uris are:' + uris);
82            }
83         } catch (error) {
84            let err: BusinessError = error as BusinessError;
85            console.error(`[picker] Invoke photoViewPicker.save failed, code is ${err.code}, message is ${err.message}`);
86         }
87      } catch (error) {
88         let err: BusinessError = error as BusinessError;
89         console.info("[picker] photoViewPickerSave error = " + JSON.stringify(err));
90      }
91   }
92   ```
93
944. After the application UI is returned from **FilePicker**, use a button to trigger the application's API. Use [fs.openSync()](../reference/apis/js-apis-file-fs.md#fsopensync) to open the image based on the URI and obtain the file descriptor (FD). Note that the **mode** parameter of **fs.openSync()** must be **fs.OpenMode.READ_WRITE**.
95
96   Then, use [fs.write](../reference/apis/js-apis-file-fs.md#fswrite) to edit the image based on the FD. After that, use **fs.closeSync** to close the FD.
97
98   ```ts
99   import { BusinessError } from '@ohos.base';
100
101   async function writeOnly(uri: string): Promise<void> {
102      try {
103         let file = fs.openSync(uri, fs.OpenMode.WRITE_ONLY);
104         let writeLen = await fs.write(file.fd, pixelmapArrayBuffer);
105         fs.closeSync(file);
106         console.info("[picker] writeOnly writeLen = " + writeLen);
107      } catch (error) {
108         let err: BusinessError = error as BusinessError;
109         console.info("[picker] writeOnly error: " + JSON.stringify(err));
110      }
111   }
112   ```
113
114## Saving Documents
115
1161. Import the **picker** and **fs** modules.
117
118   ```ts
119   import picker from '@ohos.file.picker';
120   import fs from '@ohos.file.fs';
121   import { BusinessError } from '@ohos.base';
122   ```
123
1242. Create a **documentSaveOptions** instance.
125
126   ```ts
127   const documentSaveOptions = new picker.DocumentSaveOptions(); // Create a documentSaveOptions instance.
128   documentSaveOptions.newFileNames = ["DocumentViewPicker01.txt"]; // (Optional) Set the name of the document to save.
129   documentSaveOptions.fileSuffixChoices = ['.png', '.txt', '.mp4']; // (Optional) Types of the documents to save.
130   ```
131
1323. Create a **documentViewPicker** instance, and call [save()](../reference/apis/js-apis-file-picker.md#save-3) to open the **FilePicker** page to save the document. After the user selects the destination folder, the document is saved and the URI of the document saved is returned.
133
134   The permission on the URI returned by **save()** is read/write. Further file operations can be performed based on the URI. 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.
135
136   ```ts
137   import { BusinessError } from '@ohos.base';
138
139   let uris: Array<string> = [];
140   const documentViewPicker = new picker.DocumentViewPicker(); // Create a documentViewPicker instance.
141   documentViewPicker.save(documentSaveOptions).then((documentSaveResult: Array<string>) => {
142     uris = documentSaveResult;
143     console.info('documentViewPicker.save to file succeed and uris are:' + uris);
144   }).catch((err: BusinessError) => {
145     console.error(`Invoke documentViewPicker.save failed, code is ${err.code}, message is ${err.message}`);
146   })
147   ```
148
1494. After the application UI is returned from **FilePicker**, use a button to trigger the application's API. Use [fs.openSync()](../reference/apis/js-apis-file-fs.md#fsopensync) to open the document based on the URI and obtain the FD. Note that the **mode** parameter of **fs.openSync()** must be **fs.OpenMode.READ_WRITE**.
150
151   ```ts
152   import fs from '@ohos.file.fs';
153
154   const uri = '';
155   let file = fs.openSync(uri, fs.OpenMode.READ_WRITE);
156   console.info('file fd: ' + file.fd);
157   ```
158
1595. Use [fs.writeSync()](../reference/apis/js-apis-file-fs.md#writesync) to edit the document based on the FD, and then use **fs.closeSync** to close the FD.
160
161   ```ts
162   import fs from '@ohos.file.fs';
163
164   let writeLen: number = fs.writeSync(file.fd, 'hello, world');
165   console.info('write data to file succeed and size is:' + writeLen);
166   fs.closeSync(file);
167   ```
168
169## Saving Audio Clips
170
1711. Import the **picker** and **fs** modules.
172
173   ```ts
174   import picker from '@ohos.file.picker';
175   import fs from '@ohos.file.fs';
176   import { BusinessError } from '@ohos.base';
177   ```
178
1792. Create an **audioSaveOptions** instance.
180
181   ```ts
182   const audioSaveOptions = new picker.AudioSaveOptions(); // Create an audioSaveOptions instance.
183   audioSaveOptions.newFileNames = ['AudioViewPicker01.mp3']; // (Optional) Set the name of the audio file to save.
184   ```
185
1863. Create an **audioViewPicker** instance, and call [save()](../reference/apis/js-apis-file-picker.md#save-6) to open the **FilePicker** page to save the audio clip. After the user selects the destination folder, the audio clip is saved and the URI of the audio clip saved is returned.
187
188   The permission on the URI returned by **save()** is read/write. Further file operations can be performed based on the URI. Note that the URI cannot be directly used in the **picker** callback to open an audio clip. You need to define a global variable to save the URI and use a button to trigger the operation for opening the audio clip.
189
190   ```ts
191   let uri: string = '';
192   const audioViewPicker = new picker.AudioViewPicker();
193   audioViewPicker.save(audioSaveOptions).then((audioSelectResult: Array<string>) => {
194     uri = audioSelectResult[0];
195     console.info('audioViewPicker.save to file succeed and uri is:' + uri);
196   }).catch((err: BusinessError) => {
197     console.error(`Invoke audioViewPicker.save failed, code is ${err.code}, message is ${err.message}`);
198   })
199   ```
200
2014. After the application UI is returned from **FilePicker**, use a button to trigger the application's API. Use [fs.openSync()](../reference/apis/js-apis-file-fs.md#fsopensync) to open the audio clip based on the URI and obtain the FD. Note that the **mode** parameter of **fs.openSync()** must be **fs.OpenMode.READ_WRITE**.
202
203   ```ts
204   let file = fs.openSync(uri, fs.OpenMode.READ_WRITE);
205   console.info('file fd: ' + file.fd);
206   ```
207
2085. Use [fs.writeSync()](../reference/apis/js-apis-file-fs.md#writesync) to edit the audio clip based on the FD, and then use **fs.closeSync** to close the FD.
209
210   ```ts
211   let writeLen = fs.writeSync(file.fd, 'hello, world');
212   console.info('write data to file succeed and size is:' + writeLen);
213   fs.closeSync(file);
214   ```
215
216