harmony 鸿蒙@ohos.file.fileuri (File URI)

  • 2023-06-24
  • 浏览 (477)

@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 path of the file in the application sandbox. The following is an example:

Stage Model

  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;
    }
  }

FA Model

  import featureAbility from '@ohos.ability.featureAbility';
 
  let context = featureAbility.getContext();
  context.getFilesDir().then((data) => {
    let pathDir = data;
  })

For details about how to obtain the FA model context, see Context.

FileUri10+

Attributes

System capability: SystemCapability.FileManagement.AppFileService

Name Type Readable Writable Description
path10+ string Yes No Path of the file corresponding to the URI.
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>
- URI of the user’s document: file://docs/storage/Users/currentUser/<publicPath>
- URI of the user’s media asset: 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(filePath);  // 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

Obtains the URI of the string type.

Return value

Type Description
string URI of the string type obtained.

Example

  let path = pathDir + '/test';
  let fileUriObject = new fileuri.FileUri(path);
  console.info("The uri of FileUri is " + fileUriObject.toString());

fileuri.getUriFromPath

getUriFromPath(path: string): string

Obtains the URI of a file in synchronous mode.

System capability: SystemCapability.FileManagement.AppFileService

Parameters

Name Type Mandatory Description
path string Yes Path of the file in the application sandbox.

Return value

Type Description
string File URI obtained.

Error codes

For details about the error codes, see File Management Error Codes. |ID |Error Message | |—————————-|———-| |401|The input parameter is invalid|

Example

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

你可能感兴趣的鸿蒙文章

harmony 鸿蒙APIs

harmony 鸿蒙System Common Events (To Be Deprecated Soon)

harmony 鸿蒙System Common Events

harmony 鸿蒙API Reference Document Description

harmony 鸿蒙Enterprise Device Management Overview (for System Applications Only)

harmony 鸿蒙BundleStatusCallback

harmony 鸿蒙@ohos.bundle.innerBundleManager (innerBundleManager)

harmony 鸿蒙@ohos.distributedBundle (Distributed Bundle Management)

harmony 鸿蒙@ohos.bundle (Bundle)

harmony 鸿蒙@ohos.enterprise.EnterpriseAdminExtensionAbility (EnterpriseAdminExtensionAbility)

0  赞