1e41f4b71Sopenharmony_ci# User File URI
2e41f4b71Sopenharmony_ci
3e41f4b71Sopenharmony_ciAs a unique identifier of a user file, the uniform resource identifier (URI) is usually used to specify the user file to be accessed or modified. Avoid using part of an URI for service code development.
4e41f4b71Sopenharmony_ci
5e41f4b71Sopenharmony_ci## URI Types
6e41f4b71Sopenharmony_ci
7e41f4b71Sopenharmony_ciThe URIs in the system can be classified into the following types:
8e41f4b71Sopenharmony_ci
9e41f4b71Sopenharmony_ci- Document URI: URI of a file selected or saved by the file manager started by picker, or obtained via the **fileAccess** module. For details, see [Obtaining a Document URI](#obtaining-a-document-uri).
10e41f4b71Sopenharmony_ci- Media file URI: URI of an image or video selected from **Gallery** by picker ; URI of an image or video obtained via the **photoAccessHelper** module; URI of an image, video, or audio file obtained via the **userFileManager** module. For details, see [Obtaining a Media File URI](#obtaining-a-media-file-uri).
11e41f4b71Sopenharmony_ci
12e41f4b71Sopenharmony_ci![user-file-uri-intro](figures/user-file-uri-intro.png)
13e41f4b71Sopenharmony_ci
14e41f4b71Sopenharmony_ci## Document URI
15e41f4b71Sopenharmony_ci
16e41f4b71Sopenharmony_ci### Document URI Overview
17e41f4b71Sopenharmony_ci
18e41f4b71Sopenharmony_ciThe document URIs are in the following format:
19e41f4b71Sopenharmony_ci
20e41f4b71Sopenharmony_ci'file://docs/storage/Users/currentUser/\<relative_path\>/test.txt'
21e41f4b71Sopenharmony_ci
22e41f4b71Sopenharmony_ciThe following table describes the fields in a document URI.
23e41f4b71Sopenharmony_ci
24e41f4b71Sopenharmony_ci| URI Field         | Description       |
25e41f4b71Sopenharmony_ci| ------------- | ------------------- |
26e41f4b71Sopenharmony_ci| 'file://docs/storage/Users/currentUser/' | Indicates the root directory of the file manager.|
27e41f4b71Sopenharmony_ci| '\<relative_path\>/' | Indicates the relative path of the file, for example, **Download/** and **Documents/**.|
28e41f4b71Sopenharmony_ci| 'test.txt' | Indicates the name of the file in the user file system. The supported file types vary with the file manager, for example, TXT, JPG, MP4, and MP3.|
29e41f4b71Sopenharmony_ci
30e41f4b71Sopenharmony_ci### Obtaining a Document URI
31e41f4b71Sopenharmony_ci
32e41f4b71Sopenharmony_ci- Use **select()** or **save()** of [DocumentViewPicker](../reference/apis-core-file-kit/js-apis-file-picker.md#documentviewpicker) to select or save a document.
33e41f4b71Sopenharmony_ci- Use **select()** or **save()** of [AudioViewPicker](../reference/apis-core-file-kit/js-apis-file-picker.md#audioviewpicker) to select or save an audio file.
34e41f4b71Sopenharmony_ci- Use [PhotoViewPicker.save](../reference/apis-core-file-kit/js-apis-file-picker.md#photoviewpickerdeprecated) to save an image or video. The URI of the image or video saved is returned.<!--Del-->
35e41f4b71Sopenharmony_ci- Use [@ohos.file.fileAccess](../reference/apis-core-file-kit/js-apis-fileAccess-sys.md). The [FileInfo](../reference/apis-core-file-kit/js-apis-fileAccess-sys.md#fileinfo) object contains the URI of the file or directory. Note that the APIs of [@ohos.file.fileAccess](../reference/apis-core-file-kit/js-apis-fileAccess-sys.md) can be called only by a system application. 
36e41f4b71Sopenharmony_ci
37e41f4b71Sopenharmony_ciYou can obtain the document URIs of the files and folders in the following directories:
38e41f4b71Sopenharmony_ci
39e41f4b71Sopenharmony_ci- External storage directory
40e41f4b71Sopenharmony_ci- **Docs** directory
41e41f4b71Sopenharmony_ci- **Download** directory
42e41f4b71Sopenharmony_ci- **Desktop** directory
43e41f4b71Sopenharmony_ci- **Documents** directory
44e41f4b71Sopenharmony_ci- **Share** directory of the shared disk
45e41f4b71Sopenharmony_ci<!--DelEnd-->
46e41f4b71Sopenharmony_ci
47e41f4b71Sopenharmony_ci### Using a Document URI
48e41f4b71Sopenharmony_ci
49e41f4b71Sopenharmony_ciApplications of the normal APL can call [@ohos.file.fs](../reference/apis-core-file-kit/js-apis-file-fs.md) APIs only to access files based on document URIs. "Permission denied" will be reported if an API of other modules is used. For details about the sample code, see [Selecting Documents](./select-user-file.md#selecting-documents) and [Saving Documents](./save-user-file.md#saving-documents).<!--Del-->
50e41f4b71Sopenharmony_ci
51e41f4b71Sopenharmony_ciApplications of the system_basic or system_core APL can call **@ohos.file.fs** and [@ohos.file.fileAccess](../reference/apis-core-file-kit/js-apis-fileAccess-sys.md) APIs to access files based on the URIs. To call **@ohos.file.fileAccess** APIs, the application must have the ohos.permission.FILE_ACCESS_MANAGER and ohos.permission.GET_BUNDLE_INFO_PRIVILEGED permissions declared in **module.json5** file. "Permission denied" will be reported if an API of other modules is used. The following example walks you through on how to use **@ohos.file.fileAccess** APIs to create a document and rename the document based on the URI.
52e41f4b71Sopenharmony_ci
53e41f4b71Sopenharmony_ci1. Use [@ohos.file.fileAccess](../reference/apis-core-file-kit/js-apis-fileAccess-sys.md) to create a document. The document URI is returned.
54e41f4b71Sopenharmony_ci2. Rename the document based on its URI.
55e41f4b71Sopenharmony_ci
56e41f4b71Sopenharmony_ci   ```ts
57e41f4b71Sopenharmony_ci   import { BusinessError } from '@kit.BasicServicesKit';
58e41f4b71Sopenharmony_ci   import { Want } from '@kit.AbilityKit';
59e41f4b71Sopenharmony_ci   import { common } from '@kit.AbilityKit';
60e41f4b71Sopenharmony_ci   import { fileAccess } from '@kit.CoreFileKit';
61e41f4b71Sopenharmony_ci   // context is passed by EntryAbility.
62e41f4b71Sopenharmony_ci   let context = getContext(this) as common.UIAbilityContext;
63e41f4b71Sopenharmony_ci   
64e41f4b71Sopenharmony_ci   async function example() {
65e41f4b71Sopenharmony_ci       let fileAccessHelper: fileAccess.FileAccessHelper;
66e41f4b71Sopenharmony_ci       // Obtain wantInfos by using getFileAccessAbilityInfo().
67e41f4b71Sopenharmony_ci       let wantInfos: Array<Want> = [
68e41f4b71Sopenharmony_ci         {
69e41f4b71Sopenharmony_ci           bundleName: "com.ohos.UserFile.ExternalFileManager",
70e41f4b71Sopenharmony_ci           abilityName: "FileExtensionAbility",
71e41f4b71Sopenharmony_ci         },
72e41f4b71Sopenharmony_ci       ]
73e41f4b71Sopenharmony_ci       try {
74e41f4b71Sopenharmony_ci         fileAccessHelper = fileAccess.createFileAccessHelper(context, wantInfos);
75e41f4b71Sopenharmony_ci         if (!fileAccessHelper) {
76e41f4b71Sopenharmony_ci           console.error("createFileAccessHelper interface returns an undefined object");
77e41f4b71Sopenharmony_ci         }
78e41f4b71Sopenharmony_ci         // A built-in storage directory is used as an example.
79e41f4b71Sopenharmony_ci         // In the sample code, sourceUri indicates the Download directory. The URI is the URI in fileInfo.
80e41f4b71Sopenharmony_ci         // Use the URI obtained.
81e41f4b71Sopenharmony_ci         let sourceUri: string = "file://docs/storage/Users/currentUser/Download";
82e41f4b71Sopenharmony_ci         let displayName: string = "file1.txt";
83e41f4b71Sopenharmony_ci         let fileUri: string;
84e41f4b71Sopenharmony_ci         try {
85e41f4b71Sopenharmony_ci           // Create a document. The URI of the document created is returned.
86e41f4b71Sopenharmony_ci           fileUri = await fileAccessHelper.createFile(sourceUri, displayName);
87e41f4b71Sopenharmony_ci           if (!fileUri) {
88e41f4b71Sopenharmony_ci             console.error("createFile return undefined object");
89e41f4b71Sopenharmony_ci           }
90e41f4b71Sopenharmony_ci           console.log("createFile success, fileUri: " + JSON.stringify(fileUri));
91e41f4b71Sopenharmony_ci           // Rename the document. The URI of the renamed document is returned.
92e41f4b71Sopenharmony_ci           let renameUri = await fileAccessHelper.rename(fileUri, "renameFile.txt");
93e41f4b71Sopenharmony_ci           console.log("rename success, renameUri: " + JSON.stringify(renameUri));
94e41f4b71Sopenharmony_ci         } catch (err) {
95e41f4b71Sopenharmony_ci           let error: BusinessError = err as BusinessError;
96e41f4b71Sopenharmony_ci           console.error("createFile failed, errCode:" + error.code + ", errMessage:" + error.message);
97e41f4b71Sopenharmony_ci         }
98e41f4b71Sopenharmony_ci       } catch (err) {
99e41f4b71Sopenharmony_ci         let error: BusinessError = err as BusinessError;
100e41f4b71Sopenharmony_ci         console.error("createFileAccessHelper failed, errCode:" + error.code + ", errMessage:" + error.message);
101e41f4b71Sopenharmony_ci       }
102e41f4b71Sopenharmony_ci     }
103e41f4b71Sopenharmony_ci   ```
104e41f4b71Sopenharmony_ci
105e41f4b71Sopenharmony_ci<!--DelEnd-->
106e41f4b71Sopenharmony_ci
107e41f4b71Sopenharmony_ci## Media File URI
108e41f4b71Sopenharmony_ci
109e41f4b71Sopenharmony_ci### Media File URI Overview
110e41f4b71Sopenharmony_ci
111e41f4b71Sopenharmony_ciThe URI format varies depending on the media file type.
112e41f4b71Sopenharmony_ci
113e41f4b71Sopenharmony_ci- Image URI format:
114e41f4b71Sopenharmony_ci
115e41f4b71Sopenharmony_ci  'file://media/Photo/\<id\>/IMG_datetime_0001/displayName.jpg'
116e41f4b71Sopenharmony_ci
117e41f4b71Sopenharmony_ci- Video URI format:
118e41f4b71Sopenharmony_ci
119e41f4b71Sopenharmony_ci  'file://media/Photo/\<id>/VID_datetime_0001/displayName.mp4'
120e41f4b71Sopenharmony_ci
121e41f4b71Sopenharmony_ci- Audio file URI format:
122e41f4b71Sopenharmony_ci
123e41f4b71Sopenharmony_ci  'file://media/Audio/\<id>/AUD_datetime_0001/displayName.mp3'
124e41f4b71Sopenharmony_ci
125e41f4b71Sopenharmony_ciThe following table describes the fields in a media file URI.
126e41f4b71Sopenharmony_ci
127e41f4b71Sopenharmony_ci| URI Field         | Description       |
128e41f4b71Sopenharmony_ci| ------------- | ------------------- |
129e41f4b71Sopenharmony_ci| 'file://media' | Indicates a URI of a media file.|
130e41f4b71Sopenharmony_ci| 'Photo' | Indicates a URI of an image or video.|
131e41f4b71Sopenharmony_ci| 'Audio' | Indicates a URI of an audio file.|
132e41f4b71Sopenharmony_ci| '\<id>' | Indicates the ID of the file after being processed in multiple tables in the database. It is not the value in the **file_id** column in the table. Do not use this ID to query a file in the database.|
133e41f4b71Sopenharmony_ci| 'IMG_datetime_0001' | Indicates the name of the image stored in the user file system without the file name extension.|
134e41f4b71Sopenharmony_ci| 'VID_datetime_0001' | Indicates the name of the video stored in the user file system without the file name extension.|
135e41f4b71Sopenharmony_ci| 'AUD_datetime_0001' | Indicates the name of the audio file stored in the user file system without the file name extension.|
136e41f4b71Sopenharmony_ci|<!--DelRow--> 'displayName.jpg' | Indicates the image name displayed. You can use [userFileManager.commitModify](../reference/apis-core-file-kit/js-apis-userFileManager-sys.md#commitmodify) to rename it. Note that the URI of the new image name is also changed.|
137e41f4b71Sopenharmony_ci|<!--DelRow--> 'displayName.mp4' | Indicates the video name displayed. You can use [userFileManager.commitModify](../reference/apis-core-file-kit/js-apis-userFileManager-sys.md#commitmodify) to rename it. Note that the URI of the new video name is also changed.|
138e41f4b71Sopenharmony_ci|<!--DelRow--> 'displayName.mp3' | Indicates the audio file name displayed. You can use [userFileManager.commitModify](../reference/apis-core-file-kit/js-apis-userFileManager-sys.md#commitmodify) to rename it. Note that the URI of the new audio name is also changed.|
139e41f4b71Sopenharmony_ci
140e41f4b71Sopenharmony_ci### Obtaining a Media File URI
141e41f4b71Sopenharmony_ci
142e41f4b71Sopenharmony_ci- Use [PhotoViewPicker.select](../reference/apis-core-file-kit/js-apis-file-picker.md#select) to select a media file.
143e41f4b71Sopenharmony_ci
144e41f4b71Sopenharmony_ci- Use [getAssets](../reference/apis-media-library-kit/js-apis-photoAccessHelper.md#getassets) or [createAsset](../reference/apis-media-library-kit/js-apis-photoAccessHelper.md#createasset) of [photoAccessHelper](../reference/apis-media-library-kit/js-apis-photoAccessHelper.md).<!--Del-->
145e41f4b71Sopenharmony_ci
146e41f4b71Sopenharmony_ci- Use [getPhotoAssets](../reference/apis-core-file-kit/js-apis-userFileManager-sys.md#getphotoassets), [getAudioAssets](../reference/apis-core-file-kit/js-apis-userFileManager-sys.md#getaudioassets), [createAudioAsset](../reference/apis-core-file-kit/js-apis-userFileManager-sys.md#createaudioasset10), or [createPhotoAsset](../reference/apis-core-file-kit/js-apis-userFileManager-sys.md#createphotoasset) of [userFileManager](../reference/apis-core-file-kit/js-apis-userFileManager-sys.md).
147e41f4b71Sopenharmony_ci  <!--DelEnd-->
148e41f4b71Sopenharmony_ci
149e41f4b71Sopenharmony_ci### Using a Media File URI
150e41f4b71Sopenharmony_ci
151e41f4b71Sopenharmony_ciApplications of the normal APL can call [photoAccessHelper](../reference/apis-media-library-kit/js-apis-photoAccessHelper.md) APIs to process media files based on their URI. For details about the sample code, see [Obtaining an Image or Video by URI](../media/medialibrary/photoAccessHelper-photoviewpicker.md#obtaining-an-image-or-video-by-uri). To call the APIs, the application must have the ohos.permission.READ_IMAGEVIDEO permission.<!--Del-->
152e41f4b71Sopenharmony_ci
153e41f4b71Sopenharmony_ciApplications of the system_basic or system_core APL can call **photoAccessHelper** and [userFileManager](../reference/apis-core-file-kit/js-apis-userFileManager-sys.md) APIs to process media files based on their URI. For details about how to use the APIs, see the API reference document.
154e41f4b71Sopenharmony_ci<!--DelEnd-->
155e41f4b71Sopenharmony_ci
156e41f4b71Sopenharmony_ciWithout the ohos.permission.READ_IMAGEVIDEO permission, the application of the normal APL can use [PhotoViewPicker.select](../reference/apis-core-file-kit/js-apis-file-picker.md#selectdeprecated-1) to obtain the URI, and use [photoAccessHelper.getAssets](../reference/apis-media-library-kit/js-apis-photoAccessHelper.md#getassets) to obtain the **PhotoAsset** object corresponding to the URI. The **PhotoAsset** object can be used to call [getThumbnail](../reference/apis-media-library-kit/js-apis-photoAccessHelper.md#getthumbnail) to obtain the thumbnail and call [get](../reference/apis-media-library-kit/js-apis-photoAccessHelper.md#get) to read certain information in [PhotoKeys](../reference/apis-media-library-kit/js-apis-photoAccessHelper.md#photokeys).
157e41f4b71Sopenharmony_ci
158e41f4b71Sopenharmony_ciThe following information can be obtained from **PhotoKeys** through temporary authorization:
159e41f4b71Sopenharmony_ci
160e41f4b71Sopenharmony_ci| Name         | Value             | Description                                                      |
161e41f4b71Sopenharmony_ci| ------------- | ------------------- | ---------------------------------------------------------- |
162e41f4b71Sopenharmony_ci| URI           | 'uri'                 | URI of the file.                                                  |
163e41f4b71Sopenharmony_ci| PHOTO_TYPE    | 'media_type'           | Type of the media file.                                             |
164e41f4b71Sopenharmony_ci| DISPLAY_NAME  | 'display_name'        | File name displayed.                                                  |
165e41f4b71Sopenharmony_ci| SIZE          | 'size'                | Size of the file.                                                  |
166e41f4b71Sopenharmony_ci| DATE_ADDED    | 'date_added'          | Date when the file was added. The value is the number of seconds elapsed since the Epoch time.            |
167e41f4b71Sopenharmony_ci| DATE_MODIFIED | 'date_modified'       | Date when the file content (not the file name) was last modified. The value is the number of seconds elapsed since the Epoch time.|
168e41f4b71Sopenharmony_ci| DURATION      | 'duration'            | Duration, in ms.                                   |
169e41f4b71Sopenharmony_ci| WIDTH         | 'width'               | Image width, in pixels.                                   |
170e41f4b71Sopenharmony_ci| HEIGHT        | 'height'              | Image height, in pixels.                                     |
171e41f4b71Sopenharmony_ci| DATE_TAKEN    | 'date_taken'          | Date when the photo was taken. The value is the number of seconds elapsed since the Epoch time.               |
172e41f4b71Sopenharmony_ci| ORIENTATION   | 'orientation'         | Orientation of the image file.                                            |
173e41f4b71Sopenharmony_ci| TITLE         | 'title'               | Title in the file.                                                  |
174e41f4b71Sopenharmony_ci
175e41f4b71Sopenharmony_ciThe following example shows how to obtain the thumbnail and file information based on the media file URI with temporary authorization.
176e41f4b71Sopenharmony_ci
177e41f4b71Sopenharmony_ci```ts
178e41f4b71Sopenharmony_ciimport { picker } from '@kit.CoreFileKit';
179e41f4b71Sopenharmony_ciimport { photoAccessHelper } from '@kit.MediaLibraryKit';
180e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit';
181e41f4b71Sopenharmony_ciimport { dataSharePredicates } from '@kit.ArkData';
182e41f4b71Sopenharmony_ci
183e41f4b71Sopenharmony_ci// Define an array of URIs to hold the URIs returned by PhotoViewPicker.select.
184e41f4b71Sopenharmony_cilet uris: Array<string> = [];
185e41f4b71Sopenharmony_ciconst context = getContext(this);
186e41f4b71Sopenharmony_ci
187e41f4b71Sopenharmony_ci// Call PhotoViewPicker.select to select an image.
188e41f4b71Sopenharmony_ciasync function photoPickerGetUri() {
189e41f4b71Sopenharmony_ci  try {  
190e41f4b71Sopenharmony_ci    let PhotoSelectOptions = new picker.PhotoSelectOptions();
191e41f4b71Sopenharmony_ci    PhotoSelectOptions.MIMEType = picker.PhotoViewMIMETypes.IMAGE_TYPE;
192e41f4b71Sopenharmony_ci    PhotoSelectOptions.maxSelectNumber = 1;
193e41f4b71Sopenharmony_ci    let photoPicker = new picker.PhotoViewPicker();
194e41f4b71Sopenharmony_ci    photoPicker.select(PhotoSelectOptions).then((PhotoSelectResult: picker.PhotoSelectResult) => {
195e41f4b71Sopenharmony_ci      console.info('PhotoViewPicker.select successfully, PhotoSelectResult uri: ' + JSON.stringify(PhotoSelectResult));
196e41f4b71Sopenharmony_ci      uris = PhotoSelectResult.photoUris;
197e41f4b71Sopenharmony_ci    }).catch((err: BusinessError) => {
198e41f4b71Sopenharmony_ci      console.error('PhotoViewPicker.select failed with err: ' + JSON.stringify(err));
199e41f4b71Sopenharmony_ci    });
200e41f4b71Sopenharmony_ci  } catch (error) {
201e41f4b71Sopenharmony_ci    let err: BusinessError = error as BusinessError;
202e41f4b71Sopenharmony_ci    console.error('PhotoViewPicker failed with err: ' + JSON.stringify(err));
203e41f4b71Sopenharmony_ci  }
204e41f4b71Sopenharmony_ci}
205e41f4b71Sopenharmony_ci
206e41f4b71Sopenharmony_ciasync function uriGetAssets() {
207e41f4b71Sopenharmony_citry {
208e41f4b71Sopenharmony_ci    let phAccessHelper = photoAccessHelper.getPhotoAccessHelper(context);
209e41f4b71Sopenharmony_ci    let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates();
210e41f4b71Sopenharmony_ci    // Configure search criteria to query the image based on the URI returned by PhotoViewPicker.select.
211e41f4b71Sopenharmony_ci    predicates.equalTo('uri', uris[0]);
212e41f4b71Sopenharmony_ci    let fetchOption: photoAccessHelper.FetchOptions = {
213e41f4b71Sopenharmony_ci      fetchColumns: [photoAccessHelper.PhotoKeys.WIDTH, photoAccessHelper.PhotoKeys.HEIGHT, photoAccessHelper.PhotoKeys.TITLE, photoAccessHelper.PhotoKeys.DURATION],
214e41f4b71Sopenharmony_ci      predicates: predicates
215e41f4b71Sopenharmony_ci    };
216e41f4b71Sopenharmony_ci    let fetchResult: photoAccessHelper.FetchResult<photoAccessHelper.PhotoAsset> = await phAccessHelper.getAssets(fetchOption);
217e41f4b71Sopenharmony_ci    // Obtain the PhotoAsset object corresponding to the URI. The file information is obtained from the PhotoAsset object.
218e41f4b71Sopenharmony_ci    const asset: photoAccessHelper.PhotoAsset = await fetchResult.getFirstObject();
219e41f4b71Sopenharmony_ci    console.info('asset displayName: ', asset.displayName);
220e41f4b71Sopenharmony_ci    console.info('asset uri: ', asset.uri);
221e41f4b71Sopenharmony_ci    console.info('asset photoType: ', asset.photoType);
222e41f4b71Sopenharmony_ci    console.info('asset width: ', asset.get(photoAccessHelper.PhotoKeys.WIDTH));
223e41f4b71Sopenharmony_ci    console.info('asset height: ', asset.get(photoAccessHelper.PhotoKeys.HEIGHT));
224e41f4b71Sopenharmony_ci    console.info('asset title: ' + asset.get(photoAccessHelper.PhotoKeys.TITLE));
225e41f4b71Sopenharmony_ci    // Obtain the thumbnail.
226e41f4b71Sopenharmony_ci    asset.getThumbnail((err, pixelMap) => {
227e41f4b71Sopenharmony_ci      if (err == undefined) {
228e41f4b71Sopenharmony_ci        console.info('getThumbnail successful ' + JSON.stringify(pixelMap));
229e41f4b71Sopenharmony_ci      } else {
230e41f4b71Sopenharmony_ci        console.error('getThumbnail fail', err);
231e41f4b71Sopenharmony_ci      }
232e41f4b71Sopenharmony_ci    });
233e41f4b71Sopenharmony_ci  } catch (error){
234e41f4b71Sopenharmony_ci    console.error('uriGetAssets failed with err: ' + JSON.stringify(error));
235e41f4b71Sopenharmony_ci  }
236e41f4b71Sopenharmony_ci}
237e41f4b71Sopenharmony_ci```
238e41f4b71Sopenharmony_ci<!--Del-->
239e41f4b71Sopenharmony_ci## Copying A File by URI (for System Applications Only)
240e41f4b71Sopenharmony_ci
241e41f4b71Sopenharmony_ciTo copy a file to the specified directory based on the URI, perform the following:
242e41f4b71Sopenharmony_ci
243e41f4b71Sopenharmony_ci1. Use [createFileAccessHelper](../reference/apis-core-file-kit/js-apis-fileAccess-sys.md#fileaccesscreatefileaccesshelper) to create a **fileAccessHelper** instance.
244e41f4b71Sopenharmony_ci
245e41f4b71Sopenharmony_ci2. Obtain **srcUri** of the file to copy.
246e41f4b71Sopenharmony_ci
247e41f4b71Sopenharmony_ci3. Obtain **destUri** of the file.
248e41f4b71Sopenharmony_ci
249e41f4b71Sopenharmony_ci4. Obtain the alternative file name **fileName**.
250e41f4b71Sopenharmony_ci
251e41f4b71Sopenharmony_ci5. Use helper.[copyFile](../reference/apis-core-file-kit/js-apis-fileAccess-sys.md#copyfile11)(srcUri, destUri, fileName) to copy the file to the specified directory.
252e41f4b71Sopenharmony_ci
253e41f4b71Sopenharmony_ciSample code:
254e41f4b71Sopenharmony_ci
255e41f4b71Sopenharmony_ci```
256e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit';
257e41f4b71Sopenharmony_ciimport { Want } from '@kit.AbilityKit';
258e41f4b71Sopenharmony_ciimport { common } from '@kit.AbilityKit';
259e41f4b71Sopenharmony_ciimport { fileAccess } from '@kit.CoreFileKit';
260e41f4b71Sopenharmony_ci
261e41f4b71Sopenharmony_ci// context is passed by EntryAbility.
262e41f4b71Sopenharmony_cilet context = getContext(this) as common.UIAbilityContext;
263e41f4b71Sopenharmony_ciasync function example() {
264e41f4b71Sopenharmony_ci    let fileAccessHelper: fileAccess.FileAccessHelper;
265e41f4b71Sopenharmony_ci    // Obtain wantInfos by using getFileAccessAbilityInfo().
266e41f4b71Sopenharmony_ci    let wantInfos: Array<Want> = [
267e41f4b71Sopenharmony_ci      {
268e41f4b71Sopenharmony_ci        bundleName: "com.ohos.UserFile.ExternalFileManager",
269e41f4b71Sopenharmony_ci        abilityName: "FileExtensionAbility",
270e41f4b71Sopenharmony_ci      },
271e41f4b71Sopenharmony_ci    ]
272e41f4b71Sopenharmony_ci    try {
273e41f4b71Sopenharmony_ci      fileAccessHelper = fileAccess.createFileAccessHelper(context, wantInfos);
274e41f4b71Sopenharmony_ci      if (!fileAccessHelper) {
275e41f4b71Sopenharmony_ci        console.error("createFileAccessHelper interface returns an undefined object");
276e41f4b71Sopenharmony_ci      }
277e41f4b71Sopenharmony_ci      // A built-in storage directory is used as an example.
278e41f4b71Sopenharmony_ci      // In the sample code, sourceUri indicates the Download directory. The URI is the URI in fileInfo.
279e41f4b71Sopenharmony_ci      // Use the URI obtained.
280e41f4b71Sopenharmony_ci      let sourceUri: string = "file://docs/storage/Users/currentUser/Download/one.txt";
281e41f4b71Sopenharmony_ci      // URI of the directory to which the file is copied.
282e41f4b71Sopenharmony_ci      let destUri: string = "file://docs/storage/Users/currentUser/Documents";
283e41f4b71Sopenharmony_ci      // File name to use if a file name conflict occurs.
284e41f4b71Sopenharmony_ci      let displayName: string = "file1.txt";
285e41f4b71Sopenharmony_ci      // URI of the file to return. 
286e41f4b71Sopenharmony_ci      let fileUri: string;
287e41f4b71Sopenharmony_ci      try {
288e41f4b71Sopenharmony_ci        // Copy a file and return the URI of the file generated.
289e41f4b71Sopenharmony_ci        fileUri = await fileAccessHelper.copyFile(sourceUri, destUri, displayName);
290e41f4b71Sopenharmony_ci        if (!fileUri) {
291e41f4b71Sopenharmony_ci          console.error("copyFile return undefined object");
292e41f4b71Sopenharmony_ci        }
293e41f4b71Sopenharmony_ci        console.log("copyFile success, fileUri: " + JSON.stringify(fileUri));
294e41f4b71Sopenharmony_ci      } catch (err) {
295e41f4b71Sopenharmony_ci        let error: BusinessError = err as BusinessError;
296e41f4b71Sopenharmony_ci        console.error("copyFile failed, errCode:" + error.code + ", errMessage:" + error.message);
297e41f4b71Sopenharmony_ci      }
298e41f4b71Sopenharmony_ci    } catch (err) {
299e41f4b71Sopenharmony_ci      let error: BusinessError = err as BusinessError;
300e41f4b71Sopenharmony_ci      console.error("createFileAccessHelper failed, errCode:" + error.code + ", errMessage:" + error.message);
301e41f4b71Sopenharmony_ci    }
302e41f4b71Sopenharmony_ci  }
303e41f4b71Sopenharmony_ci```
304e41f4b71Sopenharmony_ci<!--DelEnd-->
305