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

2023-06-24 浏览 (734)

@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

NameTypeReadableWritableDescription
path10+stringYesNoPath of the file corresponding to the URI.
name10+stringYesNoName of the file.

constructor10+

constructor(uriOrPath: string)

A constructor used to create a FileUri instance.

System capability: SystemCapability.FileManagement.AppFileService

Parameters

NameTypeMandatoryDescription
uriOrPathstringYesURI 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.

IDError Message
13900005I/O error
13900042Unknown 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

TypeDescription
stringURI 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

NameTypeMandatoryDescription
pathstringYesPath of the file in the application sandbox.

Return value

TypeDescription
stringFile URI obtained.

Error codes

For details about the error codes, see File Management Error Codes.

IDError Message
401The 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)

  • 所属分类: 后端技术
  • 本文标签: 鸿蒙 软件
  • 版权声明: 本文链接 https://seaxiang.com/blog/0a1c82ce46874440a3dd5a61f5e09197