176fd607bSopenharmony_ci# MediaLibrary组件<a name="ZH-CN_TOPIC_0000001147574647"></a> 276fd607bSopenharmony_ci 376fd607bSopenharmony_ci- [简介](#section1158716411637) 476fd607bSopenharmony_ci- [目录](#section161941989596) 576fd607bSopenharmony_ci- [使用说明](#usage-guidelines) 676fd607bSopenharmony_ci - [查询音频资产](#get-audioasset) 776fd607bSopenharmony_ci - [创建相册](#create-album) 876fd607bSopenharmony_ci - [拷贝图片资产](#copy-imageasset) 976fd607bSopenharmony_ci- [相关仓](#section1533973044317) 1076fd607bSopenharmony_ci 1176fd607bSopenharmony_ci 1276fd607bSopenharmony_ci## 简介<a name="section1158716411637"></a> 1376fd607bSopenharmony_ci**图1** medialibrary组件架构图 1476fd607bSopenharmony_ci 1576fd607bSopenharmony_ci**medialibrary\_standard** 仓库提供了一系列易用的接口用于获取媒体文件元数据信息。 1676fd607bSopenharmony_ciMediaLibrary接口暂不对外部应用开放, 仅内部使用。 1776fd607bSopenharmony_ci 1876fd607bSopenharmony_ci支持能力列举如下: 1976fd607bSopenharmony_ci- 查询音频、视频和图片文件元数据信息 2076fd607bSopenharmony_ci- 查询图片和视频相册 2176fd607bSopenharmony_ci- 媒体文件操作如创建、重命名、拷贝和删除 2276fd607bSopenharmony_ci- 相册操作如创建、重命名和删除 2376fd607bSopenharmony_ci 2476fd607bSopenharmony_ci 2576fd607bSopenharmony_ci## 目录<a name="section161941989596"></a> 2676fd607bSopenharmony_ci 2776fd607bSopenharmony_ci仓目录结构如下: 2876fd607bSopenharmony_ci``` 2976fd607bSopenharmony_ci/foundation/multimedia/medialibrary_standard # 媒体库组件代码 3076fd607bSopenharmony_ci├── frameworks # 框架代码 3176fd607bSopenharmony_ci│ ├── innerkitsimpl # 内部接口实现 3276fd607bSopenharmony_ci│ │ └── media_library # Native MediaLibrary 实现 3376fd607bSopenharmony_ci│ └── kitsimpl # 外部接口实现 3476fd607bSopenharmony_ci│ └── medialibrary # 外部 MediaLibrary NAPI 实现 3576fd607bSopenharmony_ci├── interfaces # 接口代码 3676fd607bSopenharmony_ci│ ├── innerkits # 内部 Native 接口 3776fd607bSopenharmony_ci│ └── kits # 外部 JS 接口 3876fd607bSopenharmony_ci├── LICENSE # 证书文件 3976fd607bSopenharmony_ci├── ohos.build # 编译文件 4076fd607bSopenharmony_ci├── sa_profile # 服务配置文件 4176fd607bSopenharmony_ci└── services # 服务实现 4276fd607bSopenharmony_ci``` 4376fd607bSopenharmony_ci 4476fd607bSopenharmony_ci## 使用说明<a name="usage-guidelines"></a> 4576fd607bSopenharmony_ci### 查询音频资产<a name="get-audioasset"></a> 4676fd607bSopenharmony_ci可以使用接口如 **GetMediaAssets**、**GetAudioAssets**、**GetVideoAssets** 和 **GetImageAssets** 来查询不同类型的媒体文件元数据信息。 4776fd607bSopenharmony_ci以下步骤描述了如何使用 GetAudioAssets 接口来获取相应的音频元数据。 4876fd607bSopenharmony_ci1. 使用 **GetMediaLibraryClientInstance** 接口来获取 **Medialibrary** 实例。 4976fd607bSopenharmony_ci ```cpp 5076fd607bSopenharmony_ci IMediaLibraryClient* mediaLibClientInstance = IMediaLibraryClient::GetMediaLibraryClientInstance(); 5176fd607bSopenharmony_ci ``` 5276fd607bSopenharmony_ci2. 在 **selection** 内设置音频文件的扫描目录。selection 代表媒体公共根目录的相对路径, i.e. "/storage/media/local/files"。接口会在指定目录内递归查找所有音频文件。 5376fd607bSopenharmony_ci ```cpp 5476fd607bSopenharmony_ci string selection = "audios/audio1"; 5576fd607bSopenharmony_ci ``` 5676fd607bSopenharmony_ci3. 使用 **GetAudioAssets** 接口来查询音频文件。输入参数 *selectionArgs* 暂无作用,接口会返回 *AudioAsset* 的列表。 5776fd607bSopenharmony_ci ```cpp 5876fd607bSopenharmony_ci vector<string> selectionArgs; 5976fd607bSopenharmony_ci vector<unique_ptr<AudioAsset>> audioAssets = mediaLibClientInstance->GetAudioAssets(selection, selectionArgs); 6076fd607bSopenharmony_ci ``` 6176fd607bSopenharmony_ci4. 可以从列表内获取音频元数据信息。 6276fd607bSopenharmony_ci ```cpp 6376fd607bSopenharmony_ci for (size_t i = 0; i < audioAssets.size(); i++) { 6476fd607bSopenharmony_ci cout << audioAssets[i]->uri_ << endl; 6576fd607bSopenharmony_ci cout << audioAssets[i]->size_ << endl; 6676fd607bSopenharmony_ci cout << audioAssets[i]->dateAdded_ << endl; 6776fd607bSopenharmony_ci cout << audioAssets[i]->dateModified_ << endl; 6876fd607bSopenharmony_ci cout << audioAssets[i]->albumName_ << endl; 6976fd607bSopenharmony_ci cout << audioAssets[i]->duration_ << endl; 7076fd607bSopenharmony_ci cout << audioAssets[i]->artist_ << endl; 7176fd607bSopenharmony_ci } 7276fd607bSopenharmony_ci ``` 7376fd607bSopenharmony_ci 7476fd607bSopenharmony_ci### 创建相册<a name="create-album"></a> 7576fd607bSopenharmony_ciMediaLibrary 给应用提供了操作相册的接口, 如创建、修改和删除。以下展示了创建一个新相册的步骤。 7676fd607bSopenharmony_ci1. 使用 **GetMediaLibraryInstance** 接口来获取 **Medialibrary** 实例。 7776fd607bSopenharmony_ci ```cpp 7876fd607bSopenharmony_ci IMediaLibraryClient* mediaLibClientInstance = IMediaLibraryClient::GetMediaLibraryClientInstance(); 7976fd607bSopenharmony_ci ``` 8076fd607bSopenharmony_ci2. 选择一种相册类型, 可以是 *ASSET_GENERIC_ALBUM*、*ASSET_IMAGEALBUM* 或 *ASSET_VIDEOALBUM* 。 8176fd607bSopenharmony_ci ```cpp 8276fd607bSopenharmony_ci AssetType assetType = ASSET_VIDEOALBUM; 8376fd607bSopenharmony_ci ``` 8476fd607bSopenharmony_ci3. 创建一个 **AlbumAsset** 并提供相册名称。如下 "new_video" 相册将在 "/storage/media/local/files/videos" 内被创建。 8576fd607bSopenharmony_ci ```cpp 8676fd607bSopenharmony_ci AlbumAsset albumAsset; 8776fd607bSopenharmony_ci albumAsset.albumName_ = "videos/new_video"; 8876fd607bSopenharmony_ci ``` 8976fd607bSopenharmony_ci4. 使用 **CreateMediaAlbumAsset** 接口来创建新相册, 返回值表示相册创建的成功与否。 9076fd607bSopenharmony_ci ```cpp 9176fd607bSopenharmony_ci bool errCode = mediaLibClientInstance->CreateMediaAlbumAsset(assetType, albumAsset); 9276fd607bSopenharmony_ci ``` 9376fd607bSopenharmony_ci 9476fd607bSopenharmony_ci### 拷贝图片资产<a name="copy-imageasset"></a> 9576fd607bSopenharmony_ci文件操作通过如**CreateMediaAsset**、**ModifyMediaAsset**、**CopyMediaAsset**、**DeleteMediaAsset** 的接口支持。以下例子说明 **CopyMediaAsset** 接口的使用方法。 9676fd607bSopenharmony_ci1. 使用 **GetMediaLibraryInstance** 接口来获取 **Medialibrary** 实例。 9776fd607bSopenharmony_ci ```cpp 9876fd607bSopenharmony_ci IMediaLibraryClient* mediaLibClientInstance = IMediaLibraryClient::GetMediaLibraryClientInstance(); 9976fd607bSopenharmony_ci ``` 10076fd607bSopenharmony_ci2. 选择一种资产类型, 可以是 *ASSET_MEDIA*、*ASSET_IMAGE*、*ASSET_AUDIO* 或 *ASSET_VIDEO*. 10176fd607bSopenharmony_ci ```cpp 10276fd607bSopenharmony_ci AssetType assetType = ASSET_IMAGE; 10376fd607bSopenharmony_ci ``` 10476fd607bSopenharmony_ci3. 确定 **ImageAsset** 的源和目标位置, 目标资产需要指定源拷贝的目标相册名。 10576fd607bSopenharmony_ci ```cpp 10676fd607bSopenharmony_ci MediaAsset srcMediaAsset; 10776fd607bSopenharmony_ci MediaAsset dstMediaAsset; 10876fd607bSopenharmony_ci 10976fd607bSopenharmony_ci srcMediaAsset.name_ = "image1.jpg"; 11076fd607bSopenharmony_ci srcMediaAsset.uri_ = "/storage/media/local/files/images/001/image1.jpg"; 11176fd607bSopenharmony_ci 11276fd607bSopenharmony_ci dstMediaAsset.albumName_ = "images/new_image"; 11376fd607bSopenharmony_ci ``` 11476fd607bSopenharmony_ci4. 使用 **CopyMediaAsset** 接口来从源资产拷贝到目标资产相册目录, 接口返回值表示文件操作状态。源文件 "image1.jpg" 将会被拷贝到 "/storage/media/local/files/images/new_image" 。 11576fd607bSopenharmony_ci ```cpp 11676fd607bSopenharmony_ci bool errCode = mediaLibClientInstance->CopyMediaAsset(assetType, srcMediaAsset, dstMediaAsset); 11776fd607bSopenharmony_ci ``` 11876fd607bSopenharmony_ci 11976fd607bSopenharmony_ci## 相关仓<a name="section1533973044317"></a> 12076fd607bSopenharmony_ci**[multimedia/medialibrary_standard](https://gitee.com/openharmony/multimedia_medialibrary_standard)** 121