1e41f4b71Sopenharmony_ci# Selecting Media Assets Using Picker
2e41f4b71Sopenharmony_ci
3e41f4b71Sopenharmony_ciWhen a user needs to share files such as images and videos, use Picker to start **Gallery** for the user to select the files to share. No permission is required when Picker is used. Currently, a UIAbility is used to start **Gallery** with the window component. The procedure is as follows:
4e41f4b71Sopenharmony_ci
5e41f4b71Sopenharmony_ci1. Import modules.
6e41f4b71Sopenharmony_ci
7e41f4b71Sopenharmony_ci   ```ts
8e41f4b71Sopenharmony_ci   import { photoAccessHelper } from '@kit.MediaLibraryKit';
9e41f4b71Sopenharmony_ci   import { fileIo } from '@kit.CoreFileKit';
10e41f4b71Sopenharmony_ci   import { BusinessError } from '@kit.BasicServicesKit';
11e41f4b71Sopenharmony_ci   ```
12e41f4b71Sopenharmony_ci
13e41f4b71Sopenharmony_ci2. Create a **PhotoSelectOptions** instance.
14e41f4b71Sopenharmony_ci
15e41f4b71Sopenharmony_ci   ```ts
16e41f4b71Sopenharmony_ci   const photoSelectOptions = new photoAccessHelper.PhotoSelectOptions();
17e41f4b71Sopenharmony_ci   ```
18e41f4b71Sopenharmony_ci
19e41f4b71Sopenharmony_ci3. Set the type and maximum number of the files to select.
20e41f4b71Sopenharmony_ci   The following uses images as an example. For details about the media file types, see [PhotoViewMIMETypes](../../reference/apis-media-library-kit/js-apis-photoAccessHelper.md#photoviewmimetypes).
21e41f4b71Sopenharmony_ci
22e41f4b71Sopenharmony_ci   ```ts
23e41f4b71Sopenharmony_ci   photoSelectOptions.MIMEType = photoAccessHelper.PhotoViewMIMETypes.IMAGE_TYPE; // Select images.
24e41f4b71Sopenharmony_ci   photoSelectOptions.maxSelectNumber = 5; // Set the maximum number of images that can be selected.
25e41f4b71Sopenharmony_ci   ```
26e41f4b71Sopenharmony_ci
27e41f4b71Sopenharmony_ci4. Create a **photoViewPicker** instance and call [PhotoViewPicker.select](../../reference/apis-media-library-kit/js-apis-photoAccessHelper.md#select) to open the **Gallery** page for the user to select images. After the images are selected, [PhotoSelectResult](../../reference/apis-media-library-kit/js-apis-photoAccessHelper.md#photoselectresult) is returned.
28e41f4b71Sopenharmony_ci
29e41f4b71Sopenharmony_ci   ```ts
30e41f4b71Sopenharmony_ci   let uris: Array<string> = [];
31e41f4b71Sopenharmony_ci   const photoViewPicker = new photoAccessHelper.PhotoViewPicker();
32e41f4b71Sopenharmony_ci   photoViewPicker.select(photoSelectOptions).then((photoSelectResult: photoAccessHelper.PhotoSelectResult) => {
33e41f4b71Sopenharmony_ci     uris = photoSelectResult.photoUris;
34e41f4b71Sopenharmony_ci     console.info('photoViewPicker.select to file succeed and uris are:' + uris);
35e41f4b71Sopenharmony_ci   }).catch((err: BusinessError) => {
36e41f4b71Sopenharmony_ci     console.error(`Invoke photoViewPicker.select failed, code is ${err.code}, message is ${err.message}`);
37e41f4b71Sopenharmony_ci   })
38e41f4b71Sopenharmony_ci   ```
39e41f4b71Sopenharmony_ci
40e41f4b71Sopenharmony_ci   The permission on the URIs returned by **select()** is read-only. File data can be read 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 open the file. For details, see [Reading File Data by URI](#reading-file-data-by-uri).
41e41f4b71Sopenharmony_ci
42e41f4b71Sopenharmony_ci   You can also [obtain an image or video based on the URI](#obtaining-an-image-or-video-by-uri).
43e41f4b71Sopenharmony_ci
44e41f4b71Sopenharmony_ci   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 file attribute information, such as the file name, size, access time, modification time, and path, based on the URI.
45e41f4b71Sopenharmony_ci
46e41f4b71Sopenharmony_ci## Reading File Data by URI
47e41f4b71Sopenharmony_ci
48e41f4b71Sopenharmony_ci1. After the application UI is returned from **Gallery**, use a button to trigger the application's API. Use [fileIo.openSync](../../reference/apis-core-file-kit/js-apis-file-fs.md#fsopensync) to open an image based on the URI. After the image is opened, the FD is returned. Note that the **mode** parameter of **fileIo.openSync()** must be **fileIo.OpenMode.READ_ONLY**.
49e41f4b71Sopenharmony_ci
50e41f4b71Sopenharmony_ci   ```ts
51e41f4b71Sopenharmony_ci   let uri: string = '';
52e41f4b71Sopenharmony_ci   let file = fileIo.openSync(uri, fileIo.OpenMode.READ_ONLY);
53e41f4b71Sopenharmony_ci   console.info('file fd: ' + file.fd);
54e41f4b71Sopenharmony_ci   ```
55e41f4b71Sopenharmony_ci
56e41f4b71Sopenharmony_ci2. Use [fileIo.readSync](../../reference/apis-core-file-kit/js-apis-file-fs.md#readsync) to read the file based on the FD, and close the FD after the data is read.
57e41f4b71Sopenharmony_ci
58e41f4b71Sopenharmony_ci   ```ts
59e41f4b71Sopenharmony_ci   let buffer = new ArrayBuffer(4096);
60e41f4b71Sopenharmony_ci   let readLen = fileIo.readSync(file.fd, buffer);
61e41f4b71Sopenharmony_ci   console.info('readSync data to file succeed and buffer size is:' + readLen);
62e41f4b71Sopenharmony_ci   fileIo.closeSync(file);
63e41f4b71Sopenharmony_ci   ```
64e41f4b71Sopenharmony_ci
65e41f4b71Sopenharmony_ci## Obtaining an Image or Video by URI
66e41f4b71Sopenharmony_ci
67e41f4b71Sopenharmony_ciAfter an image or video is selected by Picker, the URI of the image or video is returned. You can obtain the image or video based on the URI. The following example demonstrates how to obtain the image based on the URI **file://media/Photo/1/IMG_datetime_0001/displayName.jpg**.
68e41f4b71Sopenharmony_ci
69e41f4b71Sopenharmony_ci```ts
70e41f4b71Sopenharmony_ciimport { dataSharePredicates } from '@kit.ArkData';
71e41f4b71Sopenharmony_ci
72e41f4b71Sopenharmony_ciconst context = getContext(this);
73e41f4b71Sopenharmony_cilet phAccessHelper = photoAccessHelper.getPhotoAccessHelper(context);
74e41f4b71Sopenharmony_ci
75e41f4b71Sopenharmony_ciclass MediaDataHandler implements photoAccessHelper.MediaAssetDataHandler<ArrayBuffer> {
76e41f4b71Sopenharmony_ci  onDataPrepared(data: ArrayBuffer) {
77e41f4b71Sopenharmony_ci    if (data === undefined) {
78e41f4b71Sopenharmony_ci      console.error('Error occurred when preparing data');
79e41f4b71Sopenharmony_ci      return;
80e41f4b71Sopenharmony_ci    }
81e41f4b71Sopenharmony_ci    console.info('on image data prepared');
82e41f4b71Sopenharmony_ci    // Customize the logic for processing data.
83e41f4b71Sopenharmony_ci  }
84e41f4b71Sopenharmony_ci}
85e41f4b71Sopenharmony_ci
86e41f4b71Sopenharmony_ciasync function example() {
87e41f4b71Sopenharmony_ci  let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates();
88e41f4b71Sopenharmony_ci  let uri = 'file://media/Photo/1/IMG_datetime_0001/displayName.jpg' // The URI must exist.
89e41f4b71Sopenharmony_ci  predicates.equalTo(photoAccessHelper.PhotoKeys.URI, uri.toString());
90e41f4b71Sopenharmony_ci  let fetchOptions: photoAccessHelper.FetchOptions = {
91e41f4b71Sopenharmony_ci    fetchColumns: [photoAccessHelper.PhotoKeys.TITLE],
92e41f4b71Sopenharmony_ci    predicates: predicates
93e41f4b71Sopenharmony_ci  };
94e41f4b71Sopenharmony_ci
95e41f4b71Sopenharmony_ci  try {
96e41f4b71Sopenharmony_ci    let fetchResult: photoAccessHelper.FetchResult<photoAccessHelper.PhotoAsset> = await phAccessHelper.getAssets(fetchOptions);
97e41f4b71Sopenharmony_ci    let photoAsset: photoAccessHelper.PhotoAsset = await fetchResult.getFirstObject();
98e41f4b71Sopenharmony_ci    console.info('getAssets photoAsset.uri : ' + photoAsset.uri);
99e41f4b71Sopenharmony_ci    // Obtain the file attribute information, such as the title. If the attribute to obtain is not a default one, add the column name to fetchColumns.
100e41f4b71Sopenharmony_ci    console.info('title : ' + photoAsset.get(photoAccessHelper.PhotoKeys.TITLE));
101e41f4b71Sopenharmony_ci    // Request image data.
102e41f4b71Sopenharmony_ci    let requestOptions: photoAccessHelper.RequestOptions = {
103e41f4b71Sopenharmony_ci      deliveryMode: photoAccessHelper.DeliveryMode.HIGH_QUALITY_MODE,
104e41f4b71Sopenharmony_ci    }
105e41f4b71Sopenharmony_ci    await photoAccessHelper.MediaAssetManager.requestImageData(context, photoAsset, requestOptions, new MediaDataHandler());
106e41f4b71Sopenharmony_ci    console.info('requestImageData successfully');
107e41f4b71Sopenharmony_ci    fetchResult.close();
108e41f4b71Sopenharmony_ci  } catch (err) {
109e41f4b71Sopenharmony_ci    console.error('getAssets failed with err: ' + err);
110e41f4b71Sopenharmony_ci  }
111e41f4b71Sopenharmony_ci}
112e41f4b71Sopenharmony_ci```
113