harmony 鸿蒙@ohos.file.fileuri (File URI)
@ohos.file.fileuri (File URI)
The fileUri module allows the uniform resource identifier (URI) of a file to be obtained based on the file path. With the file URI, you can use the APIs provided by @ohos.file.fs to operate the file.
NOTE
The initial APIs of this module are supported since API version 9. Newly added APIs will be marked with a superscript to indicate their earliest API version.
Modules to Import
import { fileUri } from '@kit.CoreFileKit';
Before using this module, you need to obtain the application sandbox path of the file. The following is an example:
import { UIAbility } from '@kit.AbilityKit';
import { window } from '@kit.ArkUI';
export default class EntryAbility extends UIAbility {
onWindowStageCreate(windowStage: window.WindowStage) {
let context = this.context;
let pathDir = context.filesDir;
}
}
FileUri10+
Properties
System capability: SystemCapability.FileManagement.AppFileService
Name | Type | Mandatory | Description |
---|---|---|---|
path10+ | string | Yes | Converts the URI to the corresponding sandbox path. 1. During URI-to-path conversion, the ASCII code in the URI is decoded and then concatenated to the original position. The URI generated by a non-system API may contain characters beyond the ASCII code parsing range. As a result, the string cannot be concatenated. 2. The conversion is performed based on the string replacement rule specified by the system (the rule may change with the system evolution). During the conversion, the path is not verified, and the conversion result may not be accessible. |
name10+ | string | Yes | Obtains the file name based on the given URI. (If the file name contains the ASCII code, the file name will be decoded and then concatenated.) Atomic service API: This API can be used in atomic services since API version 15. |
constructor10+
constructor(uriOrPath: string)
A constructor used to create a FileUri instance.
Atomic service API: This API can be used in atomic services since API version 15.
System capability: SystemCapability.FileManagement.AppFileService
Parameters
Name | Type | Mandatory | Description |
---|---|---|---|
uriOrPath | string | Yes | URI or path. The following types of URIs are available: - Application sandbox URI: file://<bundleName>/<sandboxPath> - User directory file URI: file://docs/storage/Users/currentUser/<publicPath> - User directory media URI: file://media/<mediaType>/IMG_DATATIME_ID/<displayName> |
Error codes
For details about the error codes, see File Management Error Codes. |ID|Error Message | |——–|———-| |13900005|I/O error| |13900042|Unknown error| |13900020|Invalid argument| |14300002|Invalid uri|
Example
let path = pathDir + '/test';
let uri = fileUri.getUriFromPath(path); // file://<packageName>/data/storage/el2/base/haps/entry/files/test
let fileUriObject = new fileUri.FileUri(uri);
console.info("The name of FileUri is " + fileUriObject.name);
toString10+
toString(): string
System capability: SystemCapability.FileManagement.AppFileService
Converts this URI into a string.
Return value
Type | Description |
---|---|
string | URI in the string format. |
Example
let path = pathDir + '/test';
let fileUriObject = new fileUri.FileUri(path);
console.info("The uri of FileUri is " + fileUriObject.toString());
getFullDirectoryUri11+
getFullDirectoryUri(): string
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 without processing. If the file specified by the URI does not exist or the attribute fails to be obtained, an empty string is returned.
For a file, this API returns the URI of the directory where the file is located. For example, xxx
will be returned for the xxx/example.txt
file.
If the current FileUri points to a directory, the URI of the current path is returned.
Atomic service API: This API can be used in atomic services since API version 15.
System capability: SystemCapability.FileManagement.AppFileService
Return value
Type | Description |
---|---|
string | URI of the directory where the current file is located or URI of the current folder. |
Error codes
For details about the error codes, see File Management Error Codes.
ID | Error Message |
---|---|
13900002 | No such file or directory |
13900012 | Permission denied |
13900042 | Unknown error |
Example
import { BusinessError } from '@kit.BasicServicesKit';
try {
let path = pathDir + '/test.txt';
let fileUriObject = new fileUri.FileUri(path);
let directoryUri = fileUriObject.getFullDirectoryUri();
console.log(`success to getFullDirectoryUri: ${JSON.stringify(directoryUri)}`);
} catch (error) {
console.error(`failed to getFullDirectoryUri because: ${JSON.stringify(error)}`);
}
isRemoteUri12+
isRemoteUri(): boolean
Checks whether this URI is a remote URI.
Atomic service API: This API can be used in atomic services since API version 15.
System capability: SystemCapability.FileManagement.AppFileService
Return value
Type | Description |
---|---|
boolean | - Returns true if the URI points to a remote file or folder, for example, xxx/example.txt?networkid=xxx .- Returns false if the URI points to a local file or folder. |
Error codes
For details about the error codes, see File Management Error Codes.
ID | Error Message |
---|---|
13900042 | Unknown error |
Example
import { BusinessError } from '@kit.BasicServicesKit';
function isRemoteUriExample() {
let uri = "file://com.example.demo/data/stroage/el2/base/test.txt?networkid=xxxx";// ?networkid identifies a remote device.
let fileUriObject = new fileUri.FileUri(uri);
let ret = fileUriObject.isRemoteUri();
if (ret) {
console.log(`It is a remote uri.`);
}
}
fileUri.getUriFromPath
getUriFromPath(path: string): string
The URI of the application is generated based on the input path. When a path is converted to a URI, Chinese characters and non-digit characters in the path are compiled into the corresponding ASCII code and combined into the URI.
Atomic service API: This API can be used in atomic services since API version 15.
System capability: SystemCapability.FileManagement.AppFileService
Parameters
Name | Type | Mandatory | Description |
---|---|---|---|
path | string | Yes | Application sandbox path of the file. |
Return value
Type | Description |
---|---|
string | The URI of the application is generated based on the input path. When a path is converted to a URI, Chinese characters and non-digit characters in the path are compiled into the corresponding ASCII code and combined into the URI. |
Error codes
For details about the error codes, see Universal Error Codes. |ID|Error Message | |———-|———-| |401|The input parameter is invalid. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types|
Example
let filePath = pathDir + "/test";
let uri = fileUri.getUriFromPath(filePath);
你可能感兴趣的鸿蒙文章
harmony 鸿蒙FileShare_PolicyErrorResult
harmony 鸿蒙FileShare_PolicyInfo
- 所属分类: 后端技术
- 本文标签:
热门推荐
-
2、 - 优质文章
-
3、 gate.io
-
8、 golang
-
9、 openharmony
-
10、 Vue中input框自动聚焦