harmony 鸿蒙FileUri

  • 2025-06-12
  • 浏览 (5)

FileUri

Overview

The FileUri module provides APIs for manipulating file Uniform Resource Identifiers (URIs).

It supports URI-path conversion, validity verification, and conversion of pointed files or paths.

It is mainly used to verify the URI format and convert URIs. When files are shared between applications, the application sandbox path is converted into a URI based on specific rules.

You must ensure that all input parameters of the API are valid. The converted URI will be output regardless of whether it exists or not.

System capability: SystemCapability.FileManagement.AppFileService

Since: 12

Summary

Files

Name Description
oh_file_uri.h Provides APIs for URI operations, including performing URI-path conversion, obtaining directory URIs, and verifying URIs.

Functions

Name Description
FileManagement_ErrCodeOH_FileUri_GetUriFromPath (const char *path, unsigned int length, char **result) Converts the input path to the URI of the application.
In this process, Chinese characters and non-digit characters in the path are compiled into the corresponding ASCII code and concatenated to the URI.
FileManagement_ErrCodeOH_FileUri_GetPathFromUri (const char *uri, unsigned int length, char **result) Converts the URI to the corresponding sandbox path.
1. In this process, the ASCII code in the URI is decoded and then concatenated to the original position. If the URI generated by a non-system API contains special characters that cannot be decoded, the string will not be concatenated.
2. Since strings are replaced based on specific rules that may evolve with the system, and no verification is performed, the converted path may not be accessible.
FileManagement_ErrCodeOH_FileUri_GetFullDirectoryUri (const char *uri, unsigned int length, char **result) Obtains the URI of the path.
If the URI points to a file, the URI of the path is returned.
If the URI points to a directory, the original string is returned.
If the file specified by the URI does not exist or the property fails to be obtained, an empty string is returned.
bool OH_FileUri_IsValidUri (const char *uri, unsigned int length) Only checks the validity of the URI format, excluding the URI validity.
FileManagement_ErrCodeOH_FileUri_GetFileName (const char *uri, unsigned int length, char **result) Obtains the file name based on the URI. The ASCII code in the file name will be decoded and concatenated to the original position.

Function Description

OH_FileUri_GetFullDirectoryUri()

FileManagement_ErrCode OH_FileUri_GetFullDirectoryUri (const char * uri, unsigned int length, char ** result )

Description Obtains the URI of the path.
If the URI points to a file, the URI of the path is returned;
If the URI points to a directory, the original string is returned;

If the file specified by the URI does not exist or the property fails to be obtained, an empty string is returned.

System capability: SystemCapability.FileManagement.AppFileService

Since: 12

Parameters

Name Description
uri Pointer to the target URI.
length Length of the URI, in bytes.
result Double pointer to the URI obtained. You also need to use free() of the standard library to release the memory allocated.

Return value

FileManagement_ErrCode

OH_FileUri_GetPathFromUri()

FileManagement_ErrCode OH_FileUri_GetPathFromUri (const char * uri, unsigned int length, char ** result )

Description Converts the URI to the corresponding sandbox path.

  1. In this process, the ASCII code in the URI is decoded and then concatenated to the original position. If the URI generated by a non-system API contains special characters that cannot be decoded, the string will not be concatenated. 2. Since strings are replaced based on specific rules that may evolve with the system, and no verification is performed, the converted path may not be accessible.

System capability: SystemCapability.FileManagement.AppFileService

Since: 12

Parameters

Name Description
uri Pointer to the URI to convert.
length Length of the URI to convert.
result Double pointer to the path obtained. You also need to use free() of the standard library to release the memory allocated.

Return value

FileManagement_ErrCode

OH_FileUri_GetUriFromPath()

FileManagement_ErrCode OH_FileUri_GetUriFromPath (const char * path, unsigned int length, char ** result )

Description Converts the input path to the URI of the application.

In this process, Chinese characters and non-digit characters in the path are compiled into the corresponding ASCII code and concatenated to the URI.

System capability: SystemCapability.FileManagement.AppFileService

Since: 12

Parameters

Name Description
path Pointer to the path to convert.
length Length of the path to convert.
result Double pointer to the URI obtained. You also need to use free() of the standard library to release the memory allocated.

Return value

FileManagement_ErrCode

OH_FileUri_IsValidUri()

bool OH_FileUri_IsValidUri (const char * uri, unsigned int length )

Description Only checks the validity of the URI format, excluding the URI validity.

System capability: SystemCapability.FileManagement.AppFileService

Since: 12

Parameters

Name Description
uri Pointer to the URI to check.
length Length of the URI to check.

Return value

The value true means the URI is valid; the value false means the opposite.

OH_FileUri_GetFileName()

FileManagement_ErrCode OH_FileUri_GetFileName (const char * uri, unsigned int length, char ** result )

Description

Obtains the file name based on the URI. Any ASCII code in the file name will be decoded and concatenated to the original position.

System capability: SystemCapability.FileManagement.AppFileService

Since: 13

Parameters

Name Description
uri Pointer to the URI to obtain.
length Length of the URI, in bytes.
result Double pointer to the file name obtained. You also need to use free() of the standard library to release the memory allocated.

Return value

FileManagement_ErrCode

你可能感兴趣的鸿蒙文章

harmony 鸿蒙Core File Kit

harmony 鸿蒙Environment

harmony 鸿蒙FileIO

harmony 鸿蒙FileShare_PolicyErrorResult

harmony 鸿蒙FileShare_PolicyInfo

harmony 鸿蒙error_code.h

harmony 鸿蒙File Management Error Codes

harmony 鸿蒙FileShare

harmony 鸿蒙@ohos.application.BackupExtensionAbility (Backup and Restore Extension Capability) (System API)

harmony 鸿蒙@ohos.application.BackupExtensionAbility (BackupExtensionAbility)

0  赞