1# MediaLibrary<a name="EN-US_TOPIC_0000001147574647"></a>
2
3- [Introduction<a name="section1158716411637"></a>](#introduction)
4- [Directory Structure<a name="section161941989596"></a>](#directory-structure)
5- [Usage Guidelines<a name="usage-guidelines"></a>](#usage-guidelines)
6    - [Query AudioAsset<a name="get-audioasset"></a>](#query-audioasset)
7    - [Create Album<a name="create-album"></a>](#create-album)
8    - [Copy ImageAsset<a name="copy-imageasset"></a>](#copy-imageasset)
9- [Repositories Involved<a name="section1533973044317"></a>](#repositories-involved)
10
11
12## Introduction<a name="section1158716411637"></a>
13
14**Figure  1**  medialibrary architecture<a name="fig99659301300"></a>
15![](figures/medialibrary-architecture.png "medialibrary-architecture")
16
17The **medialibrary\_standard**  repository provides a set of easy-to-use APIs for getting media file metadata information.
18MediaLibrary APIs can only be used internally, not exposed to public application currently.
19
20The various capabilities can be categorized as below:
21- Query for audio, video and image files metadata information
22- Query for image and video albums
23- File operations like create, rename, copy and delete media file
24- Album operations like create, rename and delete album
25
26
27## Directory Structure<a name="section161941989596"></a>
28
29The structure of the repository directory is as follows:
30```
31/foundation/multimedia/medialibrary_standard    # Medialibrary code
32├── frameworks                                  # Framework code
33│   ├── innerkitsimpl                           # Internal Native API implementation
34│   │   └── media_library                       # Native MediaLibrary implementation
35│   └── kitsimpl                                # External JS API implementation
36│       └── medialibrary                        # External MediaLibrary NAPI implementation
37├── interfaces                                  # Interfaces
38│   ├── innerkits                               # Internal Native APIs
39│   └── kits                                    # External JS APIs
40├── LICENSE                                     # License file
41├── ohos.build                                  # Build file
42├── sa_profile                                  # Service configuration profile
43└── services                                    # Service implementation
44```
45
46## Usage Guidelines<a name="usage-guidelines"></a>
47### Query AudioAsset<a name="get-audioasset"></a>
48We can use APIs like **GetMediaAssets**, **GetAudioAssets**, **GetVideoAssets** and **GetImageAssets** to query for different file metadata information. The following steps describe how to use the GetAudioAssets API to obtain audio related metadata.
491. Use **GetMediaLibraryClientInstance** API to obtain the **Medialibrary** instance
50    ```cpp
51    IMediaLibraryClient* mediaLibClientInstance = IMediaLibraryClient::GetMediaLibraryClientInstance();
52    ```
532. Set the scanning directory for audio files in **selection** string. The selection value will be a relative path to a media root directory i.e. "/storage/media/local/files". It will check for audio files recursively in the given directory.
54    ```cpp
55    string selection = "audios/audio1";
56    ```
573. Use the **GetAudioAssets** API to query for audio files. The input argument *selectionArgs* does not have any use case currently. The API returns a list of *AudioAsset*.
58    ```cpp
59    vector<string> selectionArgs;
60    vector<unique_ptr<AudioAsset>> audioAssets = mediaLibClientInstance->GetAudioAssets(selection, selectionArgs);
61    ```
624. We can obtain audio metadata for each file from the list.
63    ```cpp
64    for (size_t i = 0; i < audioAssets.size(); i++) {
65        cout << audioAssets[i]->uri_ << endl;
66        cout << audioAssets[i]->size_ << endl;
67        cout << audioAssets[i]->dateAdded_ << endl;
68        cout << audioAssets[i]->dateModified_ << endl;
69        cout << audioAssets[i]->albumName_ << endl;
70        cout << audioAssets[i]->duration_ << endl;
71        cout << audioAssets[i]->artist_ << endl;
72    }
73    ```
74
75### Create Album<a name="create-album"></a>
76MediaLibrary offers APIs for application to perform album operations like create, modify and delete. Below are the steps on how to create a new album.
771. Use **GetMediaLibraryInstance** API to obtain the **Medialibrary** instance
78    ```cpp
79    IMediaLibraryClient* mediaLibClientInstance = IMediaLibraryClient::GetMediaLibraryClientInstance();
80    ```
812. Specify the album type. It can be either *ASSET_GENERIC_ALBUM*, *ASSET_IMAGEALBUM* or *ASSET_VIDEOALBUM*.
82    ```cpp
83    AssetType assetType = ASSET_VIDEOALBUM;
84    ```
853. Create a new **AlbumAsset** object and provide a new album name. Below album "new_video" will get created in the path "/storage/media/local/files/videos".
86    ```cpp
87    AlbumAsset albumAsset;
88    albumAsset.albumName_ = "videos/new_video";
89    ```
904. Use **CreateMediaAlbumAsset** API to create the new album in the device. The return value will be boolean, which states whether the album creation succeeded or failed.
91    ```cpp
92    bool errCode = mediaLibClientInstance->CreateMediaAlbumAsset(assetType, albumAsset);
93    ```
94
95### Copy ImageAsset<a name="copy-imageasset"></a>
96File operations are supported via APIs like **CreateMediaAsset**, **ModifyMediaAsset**, **CopyMediaAsset**, **DeleteMediaAsset**. The example below explains the usage of CopyMediaAsset API.
971. Use **GetMediaLibraryInstance** API to obtain the **Medialibrary** instance
98    ```cpp
99    IMediaLibraryClient* mediaLibClientInstance = IMediaLibraryClient::GetMediaLibraryClientInstance();
100    ```
1012. Specify the asset type. It can be either *ASSET_MEDIA*, *ASSET_IMAGE*, *ASSET_AUDIO* or *ASSET_VIDEO*.
102    ```cpp
103    AssetType assetType = ASSET_IMAGE;
104    ```
1053. Define the source and target **ImageAsset**. The target asset must specify the new album name where the source asset will be copied.
106    ```cpp
107    MediaAsset srcMediaAsset;
108    MediaAsset dstMediaAsset;
109
110    srcMediaAsset.name_ = "image1.jpg";
111    srcMediaAsset.uri_ = "/storage/media/local/files/images/001/image1.jpg";
112
113    dstMediaAsset.albumName_ = "images/new_image";
114    ```
1154. Use **CopyMediaAsset** API to copy the source asset to target asset's album name location. The boolean return value denotes the status of the file operation. The source file "image1.jpg" will get copied to "/storage/media/local/files/images/new_image".
116    ```cpp
117    bool errCode = mediaLibClientInstance->CopyMediaAsset(assetType, srcMediaAsset, dstMediaAsset);
118    ```
119
120## Repositories Involved<a name="section1533973044317"></a>
121**[multimedia/medialibrary_standard](https://gitee.com/openharmony/multimedia_medialibrary_standard)**
122