@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 "@ohos.file.fileuri";

Before using this module, you need to obtain the application sandbox path of the file. The following is an example:

import UIAbility from '@ohos.app.ability.UIAbility';
import window from '@ohos.window';

export default class EntryAbility extends UIAbility {
  onWindowStageCreate(windowStage: window.WindowStage) {
    let context = this.context;
    let pathDir = context.filesDir;
  }
}

FileUri10+

Attributes

System capability: SystemCapability.FileManagement.AppFileService

Name Type Readable Writable Description
path10+ string Yes No Path of the file.
name10+ string Yes No Name of the file.

constructor10+

constructor(uriOrPath: string)

A constructor used to create a FileUri instance.

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 file URI: file://docs/storage/Users/currentUser/<publicPath>
- User media asset 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

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 obtained, 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 full directory of this file or folder.

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.

For a folder, this API returns the URI of the folder.

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 '@ohos.base';
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)}`);
}

fileUri.getUriFromPath

getUriFromPath(path: string): string

Obtains the URI from a file path. This API returns the result synchronously.

System capability: SystemCapability.FileManagement.AppFileService

Parameters

Name Type Mandatory Description
path string Yes Application sandbox path of the file.

Return value

Type Description
string File URI obtained.

Error codes

For details about the error codes, see Universal Error Codes.

ID Error Message
401 The input parameter is invalid

Example

let filePath = pathDir + "/test";
let uri = fileUri.getUriFromPath(filePath);