harmony 鸿蒙Requesting Media Assets Using MediaAssetManager (C/C++)
Requesting Media Assets Using MediaAssetManager (C/C++)
You can use MediaAssetManager to copy a media asset to a sandbox directory. This topic walks you through on how to use MediaAssetManager to copy an image to the specified sandbox directory.
The procedure is as follows:
- Create a MediaAssetManager instance.
- Set parameters for requesting an image and request the image.
- (Optional) Cancel the request.
How to Develop
Add the dynamic library in the CMake script.
target_link_libraries(sample PUBLIC libmedia_asset_manager.so)
Include the media_asset_manager_capi.h and media_asset_base_capi.h header files to use the APIs of MediaAssetManager. For details about the APIs, see MediaAssetManager APIs.
NOTE
The application must have the ohos.permission.READ_IMAGEVIDEO permission. For details, see Before You Start.
- Call OH_MediaAssetManager_Create() to create a MediaAssetManager instance.
- Set the URI of the image to request, destination URI, asset requesting policy, and callback used to return the result.
- Call OH_MediaAssetManager_RequestImageForPath() to copy the image to the target URI.
- (Optional) Call OH_MediaAssetManager_CancelRequest() to cancel the request.
Example
#include "napi/native_api.h"
#include "multimedia/media_library/media_asset_base_capi.h"
#include "multimedia/media_library/media_asset_manager_capi.h"
#include "hilog/log.h"
#include <stdio.h>
#include <string.h>
const char ERROR_REQUEST_ID[UUID_STR_MAX_LENGTH] = "00000000-0000-0000-0000-000000000000";
// Callback to be invoked when the request image is ready.
void OnDataPrepared(int32_t result, MediaLibrary_RequestId requestIdStruct)
{
printf("OnDataPrepared requestId: %s result: %d\n", requestIdStruct.requestId, result);
}
int main()
{
// Create a MediaAssetManager instance.
OH_MediaAssetManager *manager = OH_MediaAssetManager_Create();
if (manager == nullptr) {
// Exception handling.
printf("Get MediaAssetManager failed.\n");
} else {
// Set the callback.
OH_MediaLibrary_OnDataPrepared callback = OnDataPrepared;
// Set the delivery mode.
MediaLibrary_RequestOptions options;
options.deliveryMode = MEDIA_LIBRARY_HIGH_QUALITY_MODE;
// Preset the URI of the image with the default high quality. The URI used is an example only. You need to create or obtain the URI based on actual requirements.
const char *srcUri = "file://media/Photo/87/VID_1712195295_025/request_image_src.jpg";
// URI of the destination directory. The URI used is an example. You need to create or obtain the URI based on actual requirements.
const char *destUri = "file://media/Photo/9/IMG_1712195237_008/request_image_dest.jpg";
// Request the image and write it to the destination directory.
MediaLibrary_RequestId requestIdStruct = OH_MediaAssetManager_RequestImageForPath(manager, srcUri,
options, destUri, callback);
if (strcmp(requestIdStruct.requestId, ERROR_REQUEST_ID) == 0) {
// Exception handling.
printf("Request image failed requestId: %s\n", requestIdStruct.requestId);
} else {
// Print the request ID if the request is successful.
printf("Request image success, requestId: %s\n", requestIdStruct.requestId);
// Call CancelRequest() to cancel the request being processed.
// Note that OH_MediaAssetManager_CancelRequest is optional.
bool ret = OH_MediaAssetManager_CancelRequest(manager, requestIdStruct);
}
}
return 0;
}
你可能感兴趣的鸿蒙文章
harmony 鸿蒙Media Library Kit (Media File Management Service)
harmony 鸿蒙Playing Moving Photos with MovingPhotoView
harmony 鸿蒙Accessing and Managing Moving Photos
harmony 鸿蒙Observing Media Assets
harmony 鸿蒙Introduction to Media Library Kit
harmony 鸿蒙Selecting Media Assets Using Picker
- 所属分类: 后端技术
- 本文标签:
热门推荐
-
2、 - 优质文章
-
3、 gate.io
-
8、 golang
-
9、 openharmony
-
10、 Vue中input框自动聚焦