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