From 566274b517b2fb40b49cac4d0acc4e634a3e4ba1 Mon Sep 17 00:00:00 2001 From: Annie_wang Date: Tue, 28 Mar 2023 16:34:07 +0800 Subject: [PATCH] fixed 7496437 from https://gitee.com/Annie_wang/docs/pulls/16606 update docs Signed-off-by: Annie_wang --- .../reference/apis/Readme-EN.md | 2 + .../reference/apis/js-apis-file-fileUri.md | 61 +++++++++ .../reference/apis/js-apis-fileShare.md | 126 ++++++++++++++++++ 3 files changed, 189 insertions(+) create mode 100644 en/application-dev/reference/apis/js-apis-file-fileUri.md create mode 100644 en/application-dev/reference/apis/js-apis-fileShare.md diff --git a/en/application-dev/reference/apis/Readme-EN.md b/en/application-dev/reference/apis/Readme-EN.md index 71cd1da72b..72336e6374 100644 --- a/en/application-dev/reference/apis/Readme-EN.md +++ b/en/application-dev/reference/apis/Readme-EN.md @@ -234,6 +234,7 @@ - [@ohos.file.environment (Directory Environment Capability)](js-apis-file-environment.md) - [@ohos.file.fileAccess (User File Access and Management)](js-apis-fileAccess.md) - [@ohos.file.fileExtensionInfo (User File Extension Information)](js-apis-fileExtensionInfo.md) + - [@ohos.file.fileUri (File URI)](js-apis-file-fileUri.md) - [@ohos.file.fs (File Management)](js-apis-file-fs.md) - [@ohos.file.hash (File Hash Processing)](js-apis-file-hash.md) - [@ohos.file.picker (Picker)](js-apis-file-picker.md) @@ -242,6 +243,7 @@ - [@ohos.file.storageStatistics (Application Storage Statistics)](js-apis-file-storage-statistics.md) - [@ohos.file.volumeManager (Volume Management)](js-apis-file-volumemanager.md) - [@ohos.filemanagement.userFileManager (User Data Management)](js-apis-userFileManager.md) + - [@ohos.fileShare (File Sharing)](js-apis-fileShare.md) - Telephony Service - [@ohos.contact (Contacts)](js-apis-contact.md) diff --git a/en/application-dev/reference/apis/js-apis-file-fileUri.md b/en/application-dev/reference/apis/js-apis-file-fileUri.md new file mode 100644 index 0000000000..9f524c0652 --- /dev/null +++ b/en/application-dev/reference/apis/js-apis-file-fileUri.md @@ -0,0 +1,61 @@ +# @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](js-apis-file-fs.md) 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 + +```js +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: + + ```js +import UIAbility from '@ohos.app.ability.UIAbility'; + +export default class EntryAbility extends UIAbility { + onWindowStageCreate(windowStage) { + let context = this.context; + let pathDir = context.filesDir; + } +} + ``` + +## 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](../errorcodes/errorcode-filemanagement.md). +| ID | Error Message | +| ---------------------------- | ---------- | +| 401 | The input parameter is invalid | + + +**Example** + + ```js +let filePath = pathDir + "test.txt"; +let uri = fileUri.getUriFromPath(filePath); + ``` diff --git a/en/application-dev/reference/apis/js-apis-fileShare.md b/en/application-dev/reference/apis/js-apis-fileShare.md new file mode 100644 index 0000000000..b9d9fb65f7 --- /dev/null +++ b/en/application-dev/reference/apis/js-apis-fileShare.md @@ -0,0 +1,126 @@ +# @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](js-apis-file-fs.md). + +> **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 + +```js +import fileShare from '@ohos.fileShare'; +``` + +## fileShare.grantUriPermission + +grantUriPermission(uri: string, bundleName: string, mode: number, 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** + +| Name| Type | Mandatory| Description | +| ------ | ------ | ---- | -------------------------- | +| uri | string | Yes | URI of a user file.| +| bundleName | string | Yes | Bundle name of the application to be grated with the permissions.| +| mode | number | Yes | Permissions to grant. For details, see [wantConstant.Flags](js-apis-app-ability-wantConstant.md#wantconstantflags).
**wantConstant.Flags.FLAG_AUTH_READ_URI_PERMISSION**: permission to read the file.
**wantConstant.Flags.FLAG_AUTH_WRITE_URI_PERMISSION**: permission to write the file.| +| callback | AsyncCallback<void> | Yes | Callback invoked to return the result. | + +**Error codes** + +For details about the error codes, see [File Management Error Codes](../errorcodes/errorcode-filemanagement.md). + +| ID | Error Message | +| ---------------------------- | ---------- | +| 201 | Permission verification failed | +| 202 | The caller is not a system application | +| 401 | The input parameter is invalid | +| 143000001 | IPC error | + + +**Example** + + ```js +import wantConstant from '@ohos.app.ability.wantConstant'; + + +let uri = 'datashare:///media/image/8'; +let bundleName = 'com.demo.test'; +try { + fileShare.grantUriPermission(uri, bundleName, wantConstant.Flags.FLAG_AUTH_READ_URI_PERMISSION | wantConstant.Flags.FLAG_AUTH_WRITE_URI_PERMISSION, (err) => { + if (err) { + console.error("grantUriPermission failed with error: " + err); + return; + } + console.info("grantUriPermission success!"); + }); +} catch (error) { + console.error("grantUriPermission failed with error:" + error); +} + ``` + + +## fileShare.grantUriPermission + +grantUriPermission(uri: string, bundleName: string, mode: number): 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** + +| Name| Type | Mandatory| Description | +| ------ | ------ | ---- | -------------------------- | +| uri | string | Yes | URI of a user file.| +| bundleName | string | Yes | Bundle name of the application to be grated with the permissions.| +| mode | number | Yes | Permissions to grant. For details, see [wantConstant.Flags](js-apis-app-ability-wantConstant.md#wantconstantflags).
**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** + +| Type | Description | +| ---------------------------- | ---------- | +| Promise<void> | Promise that returns no value.| + + +**Error codes** + +For details about the error codes, see [File Management Error Codes](../errorcodes/errorcode-filemanagement.md). + +| ID | Error Message | +| ---------------------------- | ---------- | +| 201 | Permission verification failed | +| 202 | The caller is not a system application | +| 401 | The input parameter is invalid | +| 143000001 | IPC error | + + +**Example** + + ```js +import wantConstant from '@ohos.app.ability.wantConstant'; + +let uri = 'datashare:///media/image/8'; +let bundleName = 'com.demo.test'; +try { + fileShare.grantUriPermission(uri, bundleName, wantConstant.Flags.FLAG_AUTH_READ_URI_PERMISSION | + wantConstant.Flags.FLAG_AUTH_WRITE_URI_PERMISSION).then(function () { + console.info("grantUriPermission success!"); + }).catch(function (error) { + console.error("grantUriPermission failed with error:" + error); + }); +} catch (error) { + console.error("grantUriPermission failed with error:" + error); +} + ``` -- GitLab