# FileUri开呿Œ‡å¯¼(C/C++) ## åœºæ™¯ä»‹ç» FileUriæä¾›äº†å…³äºŽæ–‡ä»¶URI的基本æ“作,对外æä¾›äº†URI与沙箱路径之间互相转æ¢ã€è¿œç«¯URI判定ã€èŽ·å–URI所在目录路径的URIç‰æŽ¥å£ï¼Œæ–¹ä¾¿ç”¨æˆ·å°†æ–‡ä»¶URI与沙箱路径相互转æ¢ã€‚ ## 基本概念 **结果集**:满足使用场景æ£ç¡®çš„路径或者URI。 ## 约æŸé™åˆ¶ - è½¬æ¢æˆ–者判æ–URI类型之å‰å¿…é¡»ä¿è¯ä¼ å…¥çš„å‚æ•°æ£ç¡®æœ‰æ•ˆã€‚ - 为ä¿è¯æ•°æ®çš„å‡†ç¡®æ€§ï¼Œåœ¨è½¬æ¢æˆ–者判æ–过程ä¸åªå…许处ç†ä¸€ä¸ªå¯¹è±¡ã€‚ ## 接å£è¯´æ˜Ž 接å£çš„详细说明,请å‚考[APIå‚考](../reference/apis-core-file-kit/fileuri.md) | 接å£åç§° | æè¿° | | -------- |--------------------------------------------| | FileManagement_ErrCode OH_FileUri_GetUriFromPath(const char *path, unsigned int length, char **result)| é€šè¿‡ä¼ å…¥çš„è·¯å¾„PATH获å–到对应的URI。 | | FileManagement_ErrCode OH_FileUri_GetPathFromUri(const char *uri, unsigned int length, char **result) | é€šè¿‡ä¼ å…¥çš„URI获å–到对应的沙箱路径PATH。 | | FileManagement_ErrCode OH_FileUri_GetFullDirectoryUri(const char *uri, unsigned int length, char **result) | èŽ·å–æ‰€åœ¨è·¯å¾„URIï¼Œæ–‡ä»¶èŽ·å–æ‰€åœ¨è·¯å¾„URI,如果URI指å‘目录则获å–当å‰è·¯å¾„URI。 | | bool OH_FileUri_IsValidUri(const char *uri, unsigned int length) | 判æ–ä¼ å…¥çš„URIçš„æ ¼å¼æ˜¯å¦æ£ç¡®ã€‚ | | FileManagement_ErrCode OH_FileUri_GetFileName(const char *uri, unsigned int length, char **result) | é€šè¿‡ä¼ å…¥çš„URI获å–到对应的文件å称。 | ## 开呿¥éª¤ **在CMake脚本ä¸é“¾æŽ¥åЍæ€åº“** CMakeLists.txt䏿·»åР以䏋lib。 ```txt target_link_libraries(sample PUBLIC libohfileuri.so) ``` **æ·»åŠ å¤´æ–‡ä»¶** ```c++ #include <filemanagement/file_uri/oh_file_uri.h> ``` 1. 调用OH_FileUri_GetUriFromPath接å£ï¼Œåœ¨æŽ¥å£ä¸malloc的内å˜éœ€è¦åœ¨ä½¿ç”¨å®ŒåŽé‡Šæ”¾ï¼Œå› æ¤éœ€è¦free对应的内å˜ã€‚示例代ç 如下所示: ```c #include <cstring> void OH_FileUri_GetUriFromPathExample() { char *path = "/data/storage/el2/base/files/test.txt"; unsigned int length = strlen(path); char *uriResult = NULL; FileManagement_ErrCode ret = OH_FileUri_GetUriFromPath(path, length ,&uriResult); if (ret == 0 && uriResult !=NULL) { printf("pathUri=%s", uriResult); // 应用a获å–到的URI为:file://com.example.demo/data/storage/el2/base/files/test.txt } if (uriResult != NULL) { free(uriResult); } } ``` 2. 调用OH_FileUri_GetPathFromUri通过URi转æˆå¯¹åº”çš„PATH,在接å£ä¸malloc的内å˜éœ€è¦åœ¨ä½¿ç”¨å®ŒåŽé‡Šæ”¾ï¼Œå› æ¤éœ€è¦free对应的内å˜ã€‚示例代ç 如下所示: ```c #include <cstring> void OH_FileUri_GetPathFromUriExample() { char *uri = "file://com.example.demo/data/storage/el2/base/files/test.txt"; unsigned int length = strlen(uri); char *pathResult = NULL; FileManagement_ErrCode ret = OH_FileUri_GetPathFromUri(uri, length, &pathResult); if (ret == 0 && pathResult != NULL) { printf("pathResult=%s", pathResult); // PathResult值为:/data/storage/el2/base/files/test.txt } if (pathResult != NULL) { free(pathResult); } } ``` 3. 调用OH_FileUri_GetFullDirectoryUri获å–URI所在路径的URI,在接å£ä¸malloc的内å˜éœ€è¦åœ¨ä½¿ç”¨å®ŒåŽé‡Šæ”¾ï¼Œå› æ¤éœ€è¦free对应的内å˜ã€‚示例代ç 如下所示: ```c #include <cstring> void OH_FileUri_GetFullDirectoryUriExample() { char *uri = "file://com.example.demo/data/storage/el2/base/files/test.txt"; unsigned int length = strlen(uri); char *uriResult = NULL; FileManagement_ErrCode ret = OH_FileUri_GetFullDirectoryUri(uri, length, &uriResult); if (ret == 0 && uriResult != NULL) { printf("pathUri=%s",uriResult);//URI所在路径的URI:file://com.example.demo/data/storage/el2/base/files/ } if (uriResult != NULL) { free(uriResult); } } ``` 4. å¯ä»¥è°ƒç”¨OH_FileUri_IsValidUri接å£è¿›è¡ŒURIæ ¼å¼éªŒè¯ã€‚ 示例代ç 如下所示: ```c #include <cstring> void OH_FileUri_IsValidUriExample() { char *uri = "file://com.example.demo/data/storage/el2/base/files/test.txt"; unsigned int length = strlen(uri); bool falgs = OH_FileUri_IsValidUri(uri, length); printf("The URI is valid? falgs=%d", falgs); } ``` 5. 调用OH_FileUri_GetFileName获å–URIä¸çš„æ–‡ä»¶å称,在接å£ä¸malloc的内å˜éœ€è¦åœ¨ä½¿ç”¨å®ŒåŽé‡Šæ”¾ï¼Œå› æ¤éœ€è¦free对应的内å˜ã€‚示例代ç 如下所示: ```c #include <cstring> void OH_FileUri_GetFileNameExample() { char *uri = "file://com.example.demo/data/storage/el2/base/files/test.txt"; unsigned int length = strlen(uri); char *uriResult = NULL; FileManagement_ErrCode ret = OH_FileUri_GetFileName(uri, length, &uriResult); if (ret == 0 && uriResult != NULL) { printf("pathUri=%s",uriResult);//获å–到URIä¸çš„æ–‡ä»¶å:test.txt } if (uriResult != NULL) { free(uriResult); } } ```