1# Requesting Media Assets Using MediaAssetManager (C/C++)
2
3You 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.
4
5The procedure is as follows:
6
71. Create a **MediaAssetManager** instance.
82. Set parameters for requesting an image and request the image.
93. (Optional) Cancel the request.
10
11## How to Develop
12
13Add the dynamic library in the CMake script.
14
15```
16target_link_libraries(sample PUBLIC libmedia_asset_manager.so)
17```
18
19Import [media_asset_manager.h](../../reference/apis-media-library-kit/media__asset__manager__capi_8h.md) and [media_asset_base_capi.h](../../reference/apis-media-library-kit/media__asset__base__capi_8h.md) header files.
20For details about the APIs, see [MediaAssetManager API](../../reference/apis-media-library-kit/_media_asset_manager.md).
21
22> **NOTE**<br>
23> The application must have the ohos.permission.READ_IMAGEVIDEO permission. For details, see [Before You Start](photoAccessHelper-preparation.md).
24
251. Call **OH_MediaAssetManager_Create()** to create a **MediaAssetManager** instance.
262. Set the URI of the image to request, destination URI, asset requesting policy, and callback used to return the result.
273. Call **OH_MediaAssetManager_RequestImageForPath()** to copy the image to the target URI.
284. (Optional) Call **OH_MediaAssetManager_CancelRequest()** to cancel the request.
29
30## Example
31
32```c
33#include "napi/native_api.h"
34#include "multimedia/media_library/media_asset_base_capi.h"
35#include "multimedia/media_library/media_asset_manager_capi.h"
36#include "hilog/log.h"
37#include <stdio.h>
38#include <string.h>
39
40const char ERROR_REQUEST_ID[UUID_STR_MAX_LENGTH] = "00000000-0000-0000-0000-000000000000";
41
42// Callback to be invoked when the request image is ready.
43void OnDataPrepared(int32_t result, MediaLibrary_RequestId requestIdStruct)
44{
45    printf("OnDataPrepared requestId: %s result: %d\n", requestIdStruct.requestId, result);
46}
47
48int main()
49{
50    // Create a MediaAssetManager instance.
51    OH_MediaAssetManager *manager = OH_MediaAssetManager_Create();
52    if (manager == nullptr) {
53        // Exception handling.
54        printf("Get MediaAssetManager failed.\n");
55    } else {
56        // Set the callback.
57        OH_MediaLibrary_OnDataPrepared callback = OnDataPrepared;
58
59        // Set the delivery mode.
60        MediaLibrary_RequestOptions options;
61        options.deliveryMode = MEDIA_LIBRARY_HIGH_QUALITY_MODE;
62
63        // 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.
64        const char *srcUri = "file://media/Photo/87/VID_1712195295_025/request_image_src.jpg";
65
66        // URI of the destination directory. The URI used is an example. You need to create or obtain the URI based on actual requirements.
67        const char *destUri = "file://media/Photo/9/IMG_1712195237_008/request_image_dest.jpg";
68
69        // Request the image and write it to the destination directory.
70        MediaLibrary_RequestId requestIdStruct = OH_MediaAssetManager_RequestImageForPath(manager, srcUri,
71            options, destUri, callback);
72
73        if (strcmp(requestIdStruct.requestId, ERROR_REQUEST_ID) == 0) {
74            // Exception handling.
75            printf("Request image failed requestId: %s\n", requestIdStruct.requestId);
76        } else {
77            // Print the request ID if the request is successful.
78            printf("Request image success, requestId: %s\n", requestIdStruct.requestId);
79
80            // Call CancelRequest() to cancel the request being processed.
81            // Note that OH_MediaAssetManager_CancelRequest is optional.
82            bool ret = OH_MediaAssetManager_CancelRequest(manager, requestId);
83        }
84    }
85    return 0;
86}
87```
88