harmony 鸿蒙@ohos.fileshare (File Sharing)

2023-06-24 浏览 (807)

@ohos.fileshare (File Sharing)

The fileShare module provides APIs for granting the access permissions on a user file to another application by the Uniform Resource Identifier (URI). Then, the authorized application can access the file by using the APIs provided by @ohos.file.fs.

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 fileShare from '@ohos.fileshare';

fileShare.grantUriPermission

grantUriPermission(uri: string, bundleName: string, flag: wantConstant.Flags, callback: AsyncCallback<void>): void

Grants permissions on a user file by the URI to an application. This API uses an asynchronous callback to return the result.

Required permissions: ohos.permission.WRITE_MEDIA

System API: This is a system API.

System capability: SystemCapability.FileManagement.AppFileService

Parameters

NameTypeMandatoryDescription
uristringYesURI of a user file.
bundleNamestringYesBundle name of the application to be grated with the permissions.
flagwantConstant.FlagsYesPermissions to grant. For details, see wantConstant.Flags.
wantConstant.Flags.FLAG_AUTH_READ_URI_PERMISSION: permission to read the file.
wantConstant.Flags.FLAG_AUTH_WRITE_URI_PERMISSION: permission to write the file.
callbackAsyncCallback<void>YesCallback invoked to return the result.

Error codes

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

IDError Message
201Permission verification failed
202The caller is not a system application
401The input parameter is invalid
143000001IPC error

Example

import wantConstant from '@ohos.app.ability.wantConstant';
import { BusinessError } from '@ohos.base';
let uri: string = 'file://media/image/8';
let bundleName: string = 'com.demo.test';
try {
  fileShare.grantUriPermission(uri, bundleName, wantConstant.Flags.FLAG_AUTH_READ_URI_PERMISSION|
    wantConstant.Flags.FLAG_AUTH_WRITE_URI_PERMISSION, (err: BusinessError) => {
    if (err) {
      console.error("grantUriPermission failed with error: " + JSON.stringify(err));
      return;
    }
    console.info("grantUriPermission success!");
  });
} catch (err) {
  let error: BusinessError = err as BusinessError;
  console.error("grantUriPermission failed with error:" + JSON.stringify(error));
}

fileShare.grantUriPermission

grantUriPermission(uri: string, bundleName: string, flag: wantConstant.Flags): Promise<void>

Grants permissions on a user file by the URI to an application. This API uses a promise to return the result.

Required permissions: ohos.permission.WRITE_MEDIA

System API: This is a system API.

System capability: SystemCapability.FileManagement.AppFileService

Parameters

NameTypeMandatoryDescription
uristringYesURI of a user file.
bundleNamestringYesBundle name of the application to be grated with the permissions.
flagwantConstant.FlagsYesPermissions to grant. For details, see wantConstant.Flags.
wantConstant.Flags.FLAG_AUTH_READ_URI_PERMISSION: permission to read the file.
wantConstant.Flags.FLAG_AUTH_WRITE_URI_PERMISSION: permission to write the file.

Return value

TypeDescription
Promise<void>Promise that returns no value.

Error codes

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

IDError Message
201Permission verification failed
202The caller is not a system application
401The input parameter is invalid
143000001IPC error

Example

import wantConstant from '@ohos.app.ability.wantConstant';
import { BusinessError } from '@ohos.base';
let uri: string = 'file://media/image/8';
let bundleName: string = 'com.demo.test';
try {
  fileShare.grantUriPermission(uri, bundleName, wantConstant.Flags.FLAG_AUTH_READ_URI_PERMISSION|
    wantConstant.Flags.FLAG_AUTH_WRITE_URI_PERMISSION).then(() => {
    console.info("grantUriPermission success!");
  }).catch((error: BusinessError) => {
    console.error("grantUriPermission failed with error:" + JSON.stringify(error));
  });
} catch (err) {
  let error: BusinessError = err as BusinessError;
  console.error("grantUriPermission failed with error:" + JSON.stringify(error));
}

你可能感兴趣的鸿蒙文章

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/beef0fd404894e0d9eef263c590523a9