未验证 提交 b5e91976 编写于 作者: O openharmony_ci 提交者: Gitee

!18111 [翻译完成】#I6XLH9

Merge pull request !18111 from Annie_wang/PR17077
# @ohos.file.fileAccess (User File Access and Management)
The **fileAccess** module is a framework for accessing and operating user files based on the ExtensionAbility mechanism. This module interacts with diverse file management services, such as the media library and external storage management service, and provides a set of file access and management APIs for system applications. The media library service allows access to user files on local devices and distributed devices. The external storage management service allows access to the user files stored on devices such as shared disks, USB flash drives, and SD cards.
The **fileAccess** module provides a framework for accessing and operating user files based on the ExtensionAbility mechanism. This module interacts with file management services, such as the media library and external storage management service, and provides a set of unified interfaces for system applications to access and manage files. The media library service allows access to user files on local and distributed devices. The external storage management service allows access to the user files stored on devices, such as shared disks, USB flash drives, and SD cards.
>**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.
>- The APIs provided by this module are system APIs and cannot be called by third-party applications. Currently, the APIs can be called only by **picker** and **fs**.
>- The APIs of this module support processing of error codes. For details, see [File Management Error Codes](../errorcodes/errorcode-filemanagement.md).
> - 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.
> - The APIs provided by this module are system APIs and cannot be called by third-party applications. Currently, the APIs can be called only by **FilePicker** and **FileManager**.
## Modules to Import
......@@ -16,9 +15,11 @@ import fileAccess from '@ohos.file.fileAccess';
## fileAccess.getFileAccessAbilityInfo
getFileAccessAbilityInfo( ) : Promise<Array<Want>>
getFileAccessAbilityInfo() : Promise<Array<Want>>
Obtains information about all wants with **extension** set to **fileAccess** in the system. A want is a basic communication component used to start services. This API uses a promise to return the result.
Obtains information about all Wants with **extension** set to **fileAccess** in the system. A Want contains information for starting an ability. This API uses a promise to return the result.
**Model restriction**: This API can be used only in the stage model.
**System capability**: SystemCapability.FileManagement.UserFileService
......@@ -26,9 +27,13 @@ Obtains information about all wants with **extension** set to **fileAccess** in
**Return value**
| Type| Description|
| --- | -- |
| Promise<Array<Want>> | Promise used to return the **want** information obtained.|
| Type| Description|
| --- | -- |
| Promise<Array<[Want](js-apis-app-ability-want.md)>> | Promise used to return the Want information obtained.|
**Error codes**
For details about error codes, see [File Management Error Codes](../errorcodes/errorcode-filemanagement.md).
**Example**
......@@ -46,9 +51,11 @@ Obtains information about all wants with **extension** set to **fileAccess** in
## fileAccess.getFileAccessAbilityInfo
getFileAccessAbilityInfo(callback: AsyncCallback<Array<Want>>): void;
getFileAccessAbilityInfo(callback: AsyncCallback<Array<Want>>): void
Obtains information about all Wants with **extension** set to **fileAccess** in the system. A Want contains information for starting an ability. This API uses an asynchronous callback to return the result.
Obtains information about all wants with **extension** set to **fileAccess** in the system. A want is a basic communication component used to start services. This API uses an asynchronous callback to return the result.
**Model restriction**: This API can be used only in the stage model.
**System capability**: SystemCapability.FileManagement.UserFileService
......@@ -56,9 +63,13 @@ Obtains information about all wants with **extension** set to **fileAccess** in
**Parameters**
| Name| Type| Mandatory| Description|
| --- | --- | --- | -- |
| callback | AsyncCallback<Array<Want>> | Yes| Promise used to return the **want** information obtained.|
| Name| Type| Mandatory| Description|
| --- | --- | --- | -- |
| callback | AsyncCallback<Array<[Want](js-apis-app-ability-want.md)>> | Yes| Promise used to return the Want information obtained.|
**Error codes**
For details about error codes, see [File Management Error Codes](../errorcodes/errorcode-filemanagement.md).
**Example**
......@@ -82,7 +93,9 @@ Obtains information about all wants with **extension** set to **fileAccess** in
createFileAccessHelper(context: Context, wants: Array<Want>) : FileAccessHelper
Synchronously creates a **Helper** object to connect to the specified wants. The **Helper** object provides file access and management capabilities.
Synchronously creates a **Helper** object to connect to the specified Wants. The **Helper** object provides file access and management capabilities.
**Model restriction**: This API can be used only in the stage model.
**System capability**: SystemCapability.FileManagement.UserFileService
......@@ -90,16 +103,20 @@ Synchronously creates a **Helper** object to connect to the specified wants. The
**Parameters**
| Name| Type| Mandatory| Description|
| --- | --- | --- | -- |
| context | Context | Yes| Context of the ability.|
| wants | Array<Want> | Yes| Wants to connect.|
| Name| Type| Mandatory| Description|
| --- | --- | --- | -- |
| context | [Context](js-apis-inner-application-context.md) | Yes| Context of the ability.|
| wants | Array<[Want](js-apis-app-ability-want.md)> | Yes| Wants to connect.|
**Return value**
| Type| Description|
| --- | -- |
| FileAccessHelper | **Helper** object created.|
| Type| Description|
| --- | -- |
| [FileAccessHelper](#fileaccesshelper) | **Helper** object created.|
**Error codes**
For details about error codes, see [File Management Error Codes](../errorcodes/errorcode-filemanagement.md).
**Example**
......@@ -131,21 +148,27 @@ createFileAccessHelper(context: Context) : FileAccessHelper
Synchronously creates a **Helper** object to connect to all file management services in the system.
**Model restriction**: This API can be used only in the stage model.
**System capability**: SystemCapability.FileManagement.UserFileService
**Required permissions**: ohos.permission.FILE_ACCESS_MANAGER and ohos.permission.GET_BUNDLE_INFO_PRIVILEGED
**Parameters**
| Name| Type| Mandatory| Description|
| --- | --- | --- | -- |
| context | Context | Yes| Context of the ability.|
| Name| Type| Mandatory| Description|
| --- | --- | --- | -- |
| context | [Context](js-apis-inner-application-context.md) | Yes| Context of the ability.|
**Return value**
| Type| Description|
| --- | -- |
| FileAccessHelper | **Helper** object created.|
| Type| Description|
| --- | -- |
| [FileAccessHelper](#fileaccesshelper) | **Helper** object created.|
**Error codes**
For details about error codes, see [File Management Error Codes](../errorcodes/errorcode-filemanagement.md).
**Example**
......@@ -164,55 +187,91 @@ Synchronously creates a **Helper** object to connect to all file management serv
}
```
## FileAccessHelper.getRoots
## FileInfo
Provides the file or directory attribute information and APIs.
**Model restriction**: This API can be used only in the stage model.
**System capability**: SystemCapability.FileManagement.UserFileService
**Required permissions**: ohos.permission.FILE_ACCESS_MANAGER
getRoots( ) : Promise<RootIterator>
### Attributes
Obtains information about the device root nodes of the file management service type connected to the **Helper** object. This API uses a promise to return a **RootIterator** object, which returns [RootInfo](#rootinfo) by using [next()](#rootiteratornext).
| Name| Type | Readable| Writable| Description |
| ------ | ------ | -------- | ------ | -------- |
| uri | string | Yes| No| URI of the file or directory.|
| relativePath<sup>10+</sup> | string | Yes| No| Relative path of the file or directory.|
| fileName | string | Yes| No| Name of the file or directory.|
| mode | number | Yes| No| Permissions on the file or directory.|
| size | number | Yes| No| Size of the file or directory.|
| mtime | number | Yes| No| Time when the file or directory was last modified.|
| mimeType | string | Yes| No| Multipurpose Internet Mail Extensions (MIME) type of the file or directory.|
### listFile
listFile(filter?: Filter) : FileIterator
Synchronously obtains a **FileIterator** object that lists the next-level files (directories) matching the conditions of the filter from a directory and returns [FileInfo](#fileinfo) using [next()](#next). Currently, only built-in storage devices support the file filter.
**Model restriction**: This API can be used only in the stage model.
**System capability**: SystemCapability.FileManagement.UserFileService
**Required permissions**: ohos.permission.FILE_ACCESS_MANAGER
**Parameters**
| Name| Type| Mandatory| Description|
| --- | --- | -- | -- |
| filter | [Filter](js-apis-file-fs.md#filter) | No| **Filter** object. |
**Return value**
| Type| Description|
| --- | -- |
| Promise&lt;RootIterator&gt; | Promise used to return the **RootIterator** object obtained.|
| Type| Description|
| --- | -- |
| [FileIterator](#fileiterator) | **FileIterator** object obtained.|
**Error codes**
For details about error codes, see [File Management Error Codes](../errorcodes/errorcode-filemanagement.md).
**Example**
```js
async getRoots() {
let rootIterator = null;
let rootinfos = [];
let isDone = false;
try {
// Obtain fileAccessHelper by referring to the sample code of fileAccess.createFileAccessHelper.
rootIterator = await fileAccessHelper.getRoots();
if (!rootIterator) {
console.error("getRoots interface returns an undefined object");
return;
}
while (!isDone) {
let result = rootIterator.next();
console.log("next result = " + JSON.stringify(result));
isDone = result.done;
if (!isDone)
rootinfos.push(result.value);
}
} catch (error) {
console.error("getRoots failed, errCode:" + error.code + ", errMessage:" + error.message);
// fileInfoDir indicates information about a directory.
// let filter = { suffix : [".txt", ".jpg", ".xlsx"] };
let fileInfoDir = fileInfos[0];
let subfileInfos = [];
let isDone = false;
try {
let fileIterator = fileInfoDir.listFile();
// listFile() with the filter implementation.
// let fileIterator = rootInfo.listFile(filter);
if (!fileIterator) {
console.error("listFile interface returns an undefined object");
return;
}
while (!isDone) {
let result = fileIterator.next();
console.log("next result = " + JSON.stringify(result));
isDone = result.done;
if (!isDone)
subfileInfos.push(result.value);
}
} catch (error) {
console.error("listFile failed, errCode:" + error.code + ", errMessage:" + error.message);
}
```
## FileAccessHelper.getRoots
### scanFile
scanFile(filter?: Filter) : FileIterator;
getRoots(callback:AsyncCallback&lt;RootIterator&gt;) : void;
Synchronously obtains a **FileIterator** object that recursively retrieves the files matching the conditions of the filter from a directory and returns [FileInfo](#fileinfo) using [next()](#next). Currently, this API supports only built-in storage devices.
Obtains information about the device root nodes of the file management service type connected to the **Helper** object. This API uses an asynchronous callback to return the result.
The callback has a **RootIterator** object, which returns [RootInfo](#rootinfo) through [next()](#rootiteratornext).
**Model restriction**: This API can be used only in the stage model.
**System capability**: SystemCapability.FileManagement.UserFileService
......@@ -220,59 +279,127 @@ The callback has a **RootIterator** object, which returns [RootInfo](#rootinfo)
**Parameters**
| Name| Type| Mandatory| Description|
| --- | --- | --- | -- |
| callback | AsyncCallback&lt;RootIterator&gt; | Yes| Promise used to return the **RootIterator** object obtained.|
| Name| Type| Mandatory| Description|
| --- | --- | -- | -- |
| filter | [Filter](js-apis-file-fs.md#filter) | No| **Filter** object. |
**Return value**
| Type| Description|
| --- | -- |
| [FileIterator](#fileiterator) | **FileIterator** object obtained.|
**Error codes**
For details about error codes, see [File Management Error Codes](../errorcodes/errorcode-filemanagement.md).
**Example**
```js
async getRoots() {
let rootinfos = [];
let isDone = false;
try {
// Obtain fileAccessHelper by referring to the sample code of fileAccess.createFileAccessHelper.
fileAccessHelper.getRoots(function (err, rootIterator) {
if (err) {
console.error("Failed to getRoots in async, errCode:" + err.code + ", errMessage:" + err.message);
return;
}
while (!isDone) {
let result = rootIterator.next();
console.log("next result = " + JSON.stringify(result));
isDone = result.done;
if (!isDone)
rootinfos.push(result.value);
}
});
} catch (error) {
console.error("getRoots failed, errCode:" + error.code + ", errMessage:" + error.message);
// fileInfoDir indicates information about a directory.
// let filter = {suffix : [".txt", ".jpg", ".xlsx"]};
let fileInfoDir = fileInfos[0];
let subfileInfos = [];
let isDone = false;
try {
let fileIterator = fileInfoDir.scanFile();
// scanFile() with the filter implementation.
// let fileIterator = rootInfo.scanFile(filter);
if (!fileIterator) {
console.error("scanFile interface returns an undefined object");
return;
}
while (!isDone) {
let result = fileIterator.next();
console.log("next result = " + JSON.stringify(result));
isDone = result.done;
if (!isDone)
subfileInfos.push(result.value);
}
} catch (error) {
console.error("scanFile failed, errCode:" + error.code + ", errMessage:" + error.message);
}
```
## RootInfo.listfile
## FileIterator
listFile(filter?: Filter) : FileIterator
Provides the **FileIterator** object.
Synchronously obtains the **FileIterator** object of the first-level files (directory) matching the conditions of the filter from the device root node. The **FileIterator** object then returns [FileInfo](#fileinfo) by using [next()](#fileiteratornext).
**Model restriction**: This API can be used only in the stage model.
**System capability**: SystemCapability.FileManagement.UserFileService
**Required permissions**: ohos.permission.FILE_ACCESS_MANAGER
**Parameters**
### next
| Name| Type| Mandatory| Description|
| --- | --- | -- | -- |
| filter | Filter | No| **Filter** object. |
next() : { value: FileInfo, done: boolean }
Obtains information about the next-level files or directories.
**Model restriction**: This API can be used only in the stage model.
**System capability**: SystemCapability.FileManagement.UserFileService
**Required permissions**: ohos.permission.FILE_ACCESS_MANAGER
**Return value**
| Type| Description|
| --- | -- |
| FileIterator | **FileIterator** object obtained.|
| {value: [FileInfo](#fileinfo), done: boolean} | File or directory information obtained. This method traverses the specified directory until **true** is returned. The **value** field contains the file or directory information obtained.|
**Error codes**
For details about error codes, see [File Management Error Codes](../errorcodes/errorcode-filemanagement.md).
## RootInfo
Provides the device's root attribute information and APIs.
**Model restriction**: This API can be used only in the stage model.
**System capability**: SystemCapability.FileManagement.UserFileService
**Required permissions**: ohos.permission.FILE_ACCESS_MANAGER
### Attributes
| Name| Type | Readable| Writable| Description |
| ------ | ------ | -------- | ------ | -------- |
| deviceType | number | Yes| No|Type of the device.|
| uri | string | Yes| No| Root directory URI of the device.|
| relativePath<sup>10+</sup> | string | Yes| No| Relative path of the root directory.|
| displayName | string | Yes| No| Device name.|
| deviceFlags | number | Yes| No| Capabilities supported by the device.|
### listFile
listFile(filter?: Filter) : FileIterator
Synchronously obtains a **FileIterator** object that lists the first-level files (directories) matching the conditions of the filter from the device root directory and returns [FileInfo](#fileinfo) using [next()](#next). Currently, only built-in storage devices support the file filter.
**Model restriction**: This API can be used only in the stage model.
**System capability**: SystemCapability.FileManagement.UserFileService
**Required permissions**: ohos.permission.FILE_ACCESS_MANAGER
**Parameters**
| Name| Type| Mandatory| Description|
| --- | --- | -- | -- |
| filter | [Filter](js-apis-file-fs.md#filter) | No| **Filter** object. |
**Return value**
| Type| Description|
| --- | -- |
| [FileIterator](#fileiterator) | **FileIterator** object obtained.|
**Error codes**
For details about error codes, see [File Management Error Codes](../errorcodes/errorcode-filemanagement.md).
**Example**
......@@ -284,7 +411,7 @@ Synchronously obtains the **FileIterator** object of the first-level files (dire
let isDone = false;
try {
let fileIterator = rootInfo.listFile();
// listFile contains the filter implementation.
// listFile() with the filter implementation.
// let fileIterator = rootInfo.listFile(filter);
if (!fileIterator) {
console.error("listFile interface returns an undefined object");
......@@ -302,11 +429,13 @@ Synchronously obtains the **FileIterator** object of the first-level files (dire
}
```
## RootInfo.scanFile
### scanFile
scanFile(filter?: Filter) : FileIterator
Recursively obtains the **FileIterator** object of the files matching the conditions of the filter from the device root node synchronously. The **FileIterator** object then returns [FileInfo](#fileinfo) by using [next()](#fileiteratornext).
Synchronously obtains a **FileIterator** object that recursively retrieves the files matching the conditions of the filter from the device root directory and returns [FileInfo](#fileinfo)using [next()](#next). Currently, this API supports only built-in storage devices.
**Model restriction**: This API can be used only in the stage model.
**System capability**: SystemCapability.FileManagement.UserFileService
......@@ -314,15 +443,19 @@ Recursively obtains the **FileIterator** object of the files matching the condit
**Parameters**
| Name| Type| Mandatory| Description|
| --- | --- | -- | -- |
| filter | Filter | No| **Filter** object. |
| Name| Type| Mandatory| Description|
| --- | --- | -- | -- |
| filter | [Filter](js-apis-file-fs.md#filter) | No| **Filter** object. |
**Return value**
| Type| Description|
| --- | -- |
| FileIterator | **FileIterator** object obtained.|
| Type| Description|
| --- | -- |
| [FileIterator](#fileiterator) | **FileIterator** object obtained.|
**Error codes**
For details about error codes, see [File Management Error Codes](../errorcodes/errorcode-filemanagement.md).
**Example**
......@@ -334,7 +467,7 @@ Recursively obtains the **FileIterator** object of the files matching the condit
let isDone = false;
try {
let fileIterator = rootInfo.scanFile();
// scanFile contains the filter implementation.
// scanFile with the filter implementation.
// let fileIterator = rootInfo.scanFile(filter);
if (!fileIterator) {
console.error("scanFile interface returns undefined object");
......@@ -352,61 +485,100 @@ Recursively obtains the **FileIterator** object of the files matching the condit
}
```
## FileInfo.listfile
## RootIterator
listFile(filter?: Filter) : FileIterator
Provides an iterator object of the device root directory.
Synchronously obtains the **FileIterator** object of the next-level files (directories) matching the conditions of the filter from a directory. The **FileIterator** object then returns [FileInfo](#fileinfo) by using [next()](#fileiteratornext).
**Model restriction**: This API can be used only in the stage model.
**System capability**: SystemCapability.FileManagement.UserFileService
**Required permissions**: ohos.permission.FILE_ACCESS_MANAGER
**Parameters**
### next
| Name| Type| Mandatory| Description|
| --- | --- | -- | -- |
| filter | Filter | No| **Filter** object. |
next() : { value: RootInfo, done: boolean }
Obtains the root directory of the next-level device.
**Model restriction**: This API can be used only in the stage model.
**System capability**: SystemCapability.FileManagement.UserFileService
**Required permissions**: ohos.permission.FILE_ACCESS_MANAGER
**Return value**
| Type| Description|
| --- | -- |
| FileIterator | **FileIterator** object obtained.|
| {value: [RootInfo](#rootinfo), done: boolean} | Root directory information obtained. This method traverses the directory until **true** is returned. The **value** field contains the root directory information.|
**Error codes**
For details about error codes, see [File Management Error Codes](../errorcodes/errorcode-filemanagement.md).
## FileAccessHelper
Provides a **FileAccessHelper** object.
**System capability**: SystemCapability.FileManagement.UserFileService
**Required permissions**: ohos.permission.FILE_ACCESS_MANAGER
### getRoots
getRoots() : Promise&lt;RootIterator&gt;
Obtains information about the device root nodes of the file management service connected to the **Helper** object. This API uses a promise to return a **RootIterator** object,
which returns [RootInfo](#rootinfo) by using [next](#next-1).
**System capability**: SystemCapability.FileManagement.UserFileService
**Required permissions**: ohos.permission.FILE_ACCESS_MANAGER
**Return value**
| Type| Description|
| --- | -- |
| Promise&lt;[RootIterator](#rootiterator)&gt; | Promise used to return the **RootIterator** object obtained.|
**Error codes**
For details about error codes, see [File Management Error Codes](../errorcodes/errorcode-filemanagement.md).
**Example**
```js
// fileInfoDir specifies the target directory.
// let filter = { suffix : [".txt", ".jpg", ".xlsx"] };
let fileInfoDir = fileInfos[0];
let subfileInfos = [];
let isDone = false;
try {
let fileIterator = fileInfoDir.listFile();
// listFile contains the filter implementation.
// let fileIterator = rootInfo.listFile(filter);
if (!fileIterator) {
console.error("listFile interface returns an undefined object");
return;
}
while (!isDone) {
let result = fileIterator.next();
console.log("next result = " + JSON.stringify(result));
isDone = result.done;
if (!isDone)
subfileInfos.push(result.value);
async getRoots() {
let rootIterator = null;
let rootinfos = [];
let isDone = false;
try {
// Obtain fileAccessHelper by referring to the sample code of fileAccess.createFileAccessHelper.
rootIterator = await fileAccessHelper.getRoots();
if (!rootIterator) {
console.error("getRoots interface returns an undefined object");
return;
}
while (!isDone) {
let result = rootIterator.next();
console.log("next result = " + JSON.stringify(result));
isDone = result.done;
if (!isDone)
rootinfos.push(result.value);
}
} catch (error) {
console.error("getRoots failed, errCode:" + error.code + ", errMessage:" + error.message);
}
} catch (error) {
console.error("listFile failed, errCode:" + error.code + ", errMessage:" + error.message);
}
```
## FileInfo.scanfile
### getRoots
scanFile(filter?: Filter) : FileIterator;
getRoots(callback:AsyncCallback&lt;RootIterator&gt;) : void
Recursively obtains the **FileIterator** object of the files matching the conditions of the filter from a directory synchronously. The **FileIterator** object then returns [FileInfo](#fileinfo) by using [next()](#fileiteratornext).
Obtains information about the device root nodes of the file management service connected to the **Helper** object. This API uses an asynchronous callback to return a **RootIterator** object,
which returns [RootInfo](#rootinfo) by using [next](#next-1).
**System capability**: SystemCapability.FileManagement.UserFileService
......@@ -415,45 +587,41 @@ Recursively obtains the **FileIterator** object of the files matching the condit
**Parameters**
| Name| Type| Mandatory| Description|
| --- | --- | -- | -- |
| filter | Filter | No| **Filter** object. |
| --- | --- | --- | -- |
| callback | AsyncCallback&lt;[RootIterator](#rootiterator)&gt; | Yes| Callback invoked to return the **RootIterator** object obtained.|
**Return value**
**Error codes**
| Type| Description|
| --- | -- |
| FileIterator | **FileIterator** object obtained.|
For details about error codes, see [File Management Error Codes](../errorcodes/errorcode-filemanagement.md).
**Example**
```js
// fileInfoDir specifies the target directory.
// let filter = {suffix : [".txt", ".jpg", ".xlsx"]};
let fileInfoDir = fileInfos[0];
let subfileInfos = [];
let isDone = false;
try {
let fileIterator = fileInfoDir.scanFile();
// scanFile contains the filter implementation.
// let fileIterator = rootInfo.scanFile(filter);
if (!fileIterator) {
console.error("scanFile interface returns an undefined object");
return;
}
while (!isDone) {
let result = fileIterator.next();
console.log("next result = " + JSON.stringify(result));
isDone = result.done;
if (!isDone)
subfileInfos.push(result.value);
async getRoots() {
let rootinfos = [];
let isDone = false;
try {
// Obtain fileAccessHelper by referring to the sample code of fileAccess.createFileAccessHelper.
fileAccessHelper.getRoots(function (err, rootIterator) {
if (err) {
console.error("Failed to getRoots in async, errCode:" + err.code + ", errMessage:" + err.message);
return;
}
while (!isDone) {
let result = rootIterator.next();
console.log("next result = " + JSON.stringify(result));
isDone = result.done;
if (!isDone)
rootinfos.push(result.value);
}
});
} catch (error) {
console.error("getRoots failed, errCode:" + error.code + ", errMessage:" + error.message);
}
} catch (error) {
console.error("scanFile failed, errCode:" + error.code + ", errMessage:" + error.message);
}
```
## FileAccessHelper.createFile
### createFile
createFile(uri: string, displayName: string) : Promise&lt;string&gt;
......@@ -467,7 +635,7 @@ Creates a file in a directory. This API uses a promise to return the result.
| Name| Type| Mandatory| Description|
| --- | --- | --- | -- |
| uri | string | Yes| URI of the parent directory for the file to create.|
| uri | string | Yes| URI of the destination directory for the file to create.|
| displayName | string | Yes| Name of the file to create. By default, the name of a local file must contain the file name extension.|
**Return value**
......@@ -476,6 +644,10 @@ Creates a file in a directory. This API uses a promise to return the result.
| --- | -- |
| Promise&lt;string&gt; | Promise used to return the URI of the file created.|
**Error codes**
For details about error codes, see [File Management Error Codes](../errorcodes/errorcode-filemanagement.md).
**Example**
```js
......@@ -498,9 +670,9 @@ Creates a file in a directory. This API uses a promise to return the result.
};
```
## FileAccessHelper.createFile
### createFile
createFile(uri: string, displayName: string, callback: AsyncCallback&lt;string&gt;) : void;
createFile(uri: string, displayName: string, callback: AsyncCallback&lt;string&gt;) : void
Creates a file in a directory. This API uses an asynchronous callback to return the result.
......@@ -512,9 +684,13 @@ Creates a file in a directory. This API uses an asynchronous callback to return
| Name| Type| Mandatory| Description|
| --- | --- | --- | -- |
| uri | string | Yes| URI of the parent directory for the file to create.|
| uri | string | Yes| URI of the destination directory for the file to create.|
| displayName | string | Yes| Name of the file to create. By default, the name of a local file must contain the file name extension.|
| callback | AsyncCallback&lt;string&gt; | Yes| Promise used to return the URI of the file created.|
| callback | AsyncCallback&lt;string&gt; | Yes| Callback invoked to return the URI of the file created.|
**Error codes**
For details about error codes, see [File Management Error Codes](../errorcodes/errorcode-filemanagement.md).
**Example**
......@@ -538,7 +714,7 @@ Creates a file in a directory. This API uses an asynchronous callback to return
};
```
## FileAccessHelper.mkDir
### mkDir
mkDir(parentUri: string, displayName: string) : Promise&lt;string&gt;
......@@ -552,7 +728,7 @@ Creates a directory. This API uses a promise to return the result.
| Name| Type| Mandatory| Description|
| --- | --- | --- | -- |
| parentUri | string | Yes| URI of the parent directory for the directory to create.|
| parentUri | string | Yes| URI of the destination directory for the directory to create.|
| displayName | string | Yes| Name of the directory to create.|
**Return value**
......@@ -561,6 +737,10 @@ Creates a directory. This API uses a promise to return the result.
| --- | -- |
| Promise&lt;string&gt; | Promise used to return the URI of the directory created.|
**Error codes**
For details about error codes, see [File Management Error Codes](../errorcodes/errorcode-filemanagement.md).
**Example**
```js
......@@ -583,9 +763,9 @@ Creates a directory. This API uses a promise to return the result.
};
```
## FileAccessHelper.mkDir
### mkDir
mkDir(parentUri: string, displayName: string, callback: AsyncCallback&lt;string&gt;) : void;
mkDir(parentUri: string, displayName: string, callback: AsyncCallback&lt;string&gt;) : void
Creates a directory. This API uses an asynchronous callback to return the result.
......@@ -597,9 +777,13 @@ Creates a directory. This API uses an asynchronous callback to return the result
| Name| Type| Mandatory| Description|
| --- | --- | --- | -- |
| parentUri | string | Yes| URI of the parent directory for the directory to create.|
| parentUri | string | Yes| URI of the destination directory for the directory to create.|
| displayName | string | Yes| Name of the directory to create.|
| callback | AsyncCallback&lt;string&gt; | Yes| Promise used to return the URI of the directory created.|
| callback | AsyncCallback&lt;string&gt; | Yes| Callback invoked to return the URI of the directory created.|
**Error codes**
For details about error codes, see [File Management Error Codes](../errorcodes/errorcode-filemanagement.md).
**Example**
......@@ -623,7 +807,7 @@ Creates a directory. This API uses an asynchronous callback to return the result
};
```
## FileAccessHelper.openFile
### openFile
openFile(uri: string, flags: OPENFLAGS) : Promise&lt;number&gt;
......@@ -644,13 +828,17 @@ Opens a file. This API uses a promise to return the result.
| Type| Description|
| --- | -- |
| Promise&lt;number&gt; | Promise used to return the file descriptor of the file opened.|
| Promise&lt;number&gt; | Promise used to return the file descriptor (FD) of the file opened.|
**Error codes**
For details about error codes, see [File Management Error Codes](../errorcodes/errorcode-filemanagement.md).
**Example**
```js
// The media library URI is used as an example.
//In the sample code, targetUri indicates a file in the Download directory. The URI is the URI in fileInfo.
// In the sample code, targetUri indicates a file in the Download directory. The URI is the URI in fileInfo.
// You can use the URI obtained.
let targetUri = "datashare:///media/file/100";
try {
......@@ -661,9 +849,9 @@ Opens a file. This API uses a promise to return the result.
};
```
## FileAccessHelper.openFile
### openFile
openFile(uri: string, flags: OPENFLAGS, callback: AsyncCallback&lt;number&gt;) : void;
openFile(uri: string, flags: OPENFLAGS, callback: AsyncCallback&lt;number&gt;) : void
Opens a file. This API uses an asynchronous callback to return the result.
......@@ -677,13 +865,17 @@ Opens a file. This API uses an asynchronous callback to return the result.
| --- | --- | --- | -- |
| uri | string | Yes| URI of the file to open.|
| flags | [OPENFLAGS](#openflags) | Yes| File open mode.|
| callback | AsyncCallback&lt;number&gt; | Yes| Callback invoked to return the file descriptor of the file opened.|
| callback | AsyncCallback&lt;number&gt; | Yes| Callback invoked to return the FD of the file opened.|
**Error codes**
For details about error codes, see [File Management Error Codes](../errorcodes/errorcode-filemanagement.md).
**Example**
```js
// The media library URI is used as an example.
//In the sample code, targetUri indicates a file in the Download directory. The URI is the URI in fileInfo.
// In the sample code, targetUri indicates a file in the Download directory. The URI is the URI in fileInfo.
// You can use the URI obtained.
let targetUri = "datashare:///media/file/100";
try {
......@@ -700,7 +892,7 @@ Opens a file. This API uses an asynchronous callback to return the result.
};
```
## FileAccessHelper.delete
### delete
delete(uri: string) : Promise&lt;number&gt;
......@@ -722,11 +914,15 @@ Deletes a file or directory. This API uses a promise to return the result.
| --- | -- |
| Promise&lt;number&gt | Promise used to return the result.|
**Error codes**
For details about error codes, see [File Management Error Codes](../errorcodes/errorcode-filemanagement.md).
**Example**
```js
// The media library URI is used as an example.
//In the sample code, targetUri indicates a file in the Download directory. The URI is the URI in fileInfo.
// In the sample code, targetUri indicates a file in the Download directory. The URI is the URI in fileInfo.
// You can use the URI obtained.
let targetUri = "datashare:///media/file/100";
try {
......@@ -739,9 +935,9 @@ Deletes a file or directory. This API uses a promise to return the result.
};
```
## FileAccessHelper.delete
### delete
delete(uri: string, callback: AsyncCallback&lt;number&gt;) : void;
delete(uri: string, callback: AsyncCallback&lt;number&gt;) : void
Deletes a file or directory. This API uses an asynchronous callback to return the result.
......@@ -754,13 +950,17 @@ Deletes a file or directory. This API uses an asynchronous callback to return th
| Name| Type| Mandatory| Description|
| --- | --- | --- | -- |
| uri | string | Yes| URI of the file or directory to delete.|
| callback | AsyncCallback&lt;number&gt; | Yes| Promise used to return the result.|
| callback | AsyncCallback&lt;number&gt; | Yes| Callback invoked to return the result.|
**Error codes**
For details about error codes, see [File Management Error Codes](../errorcodes/errorcode-filemanagement.md).
**Example**
```js
// The media library URI is used as an example.
//In the sample code, targetUri indicates a file in the Download directory. The URI is the URI in fileInfo.
// In the sample code, targetUri indicates a file in the Download directory. The URI is the URI in fileInfo.
// You can use the URI obtained.
let targetUri = "datashare:///media/file/100";
try {
......@@ -777,11 +977,11 @@ Deletes a file or directory. This API uses an asynchronous callback to return th
};
```
## FileAccessHelper.move
### move
move(sourceFile: string, destFile: string) : Promise&lt;string&gt;
Moves a file or directory. This API uses a promise to return the result.
Moves a file or directory. This API uses a promise to return the result. Currently, this API does not support move of files or directories across devices.
**System capability**: SystemCapability.FileManagement.UserFileService
......@@ -792,7 +992,7 @@ Moves a file or directory. This API uses a promise to return the result.
| Name| Type| Mandatory| Description|
| --- | --- | --- | -- |
| sourceFile | string | Yes| URI of the file or directory to move.|
| destFile | string | Yes| URI of the directory, to which the file or directory will be moved.|
| destFile | string | Yes| URI of the destination directory, to which the file or directory will be moved.|
**Return value**
......@@ -800,11 +1000,15 @@ Moves a file or directory. This API uses a promise to return the result.
| ----- | ------ |
| Promise&lt;string&gt; | Promise used to return the URI of the file or directory in the destination directory.|
**Error codes**
For details about error codes, see [File Management Error Codes](../errorcodes/errorcode-filemanagement.md).
**Example**
```js
// The media library URI is used as an example.
//In the sample code, sourceFile destFile indicates the file or directory in the Download directory. The URI is the URI in fileInfo.
// In the sample code, sourceFile destFile indicates the file or directory in the Download directory. The URI is the URI in fileInfo.
// You can use the URI obtained.
let sourceFile = "datashare:///media/file/102";
let destFile = "datashare:///media/file/101";
......@@ -817,11 +1021,11 @@ Moves a file or directory. This API uses a promise to return the result.
};
```
## FileAccessHelper.move
### move
move(sourceFile: string, destFile: string, callback: AsyncCallback&lt;string&gt;) : void;
move(sourceFile: string, destFile: string, callback: AsyncCallback&lt;string&gt;) : void
Moves a file or directory. This API uses an asynchronous callback to return the result.
Moves a file or directory. This API uses an asynchronous callback to return the result. Currently, this API does not support move of files or directories across devices.
**System capability**: SystemCapability.FileManagement.UserFileService
......@@ -832,14 +1036,18 @@ Moves a file or directory. This API uses an asynchronous callback to return the
| Name| Type| Mandatory| Description|
| --- | --- | --- | -- |
| sourceFile | string | Yes| URI of the file or directory to move.|
| destFile | string | Yes| URI of the directory, to which the file or directory will be moved.|
| callback | AsyncCallback&lt;string&gt; | Yes| Promise used to return the URI of the file or directory in the destination directory.|
| destFile | string | Yes| URI of the destination directory, to which the file or directory will be moved.|
| callback | AsyncCallback&lt;string&gt; | Yes| Callback invoked to return the URI of the file or directory in the destination directory.|
**Error codes**
For details about error codes, see [File Management Error Codes](../errorcodes/errorcode-filemanagement.md).
**Example**
```js
// The media library URI is used as an example.
//In the sample code, sourceFile destFile indicates the file or directory in the Download directory. The URI is the URI in fileInfo.
// In the sample code, sourceFile destFile indicates the file or directory in the Download directory. The URI is the URI in fileInfo.
// You can use the URI obtained.
let sourceFile = "datashare:///media/file/102";
let destFile = "datashare:///media/file/101";
......@@ -857,7 +1065,7 @@ Moves a file or directory. This API uses an asynchronous callback to return the
};
```
## FileAccessHelper.rename
### rename
rename(uri: string, displayName: string) : Promise&lt;string&gt;
......@@ -880,6 +1088,10 @@ Renames a file or directory. This API uses a promise to return the result.
| --- | -- |
| Promise&lt;string&gt; | Promise used to return the URI of the renamed file or directory.|
**Error codes**
For details about error codes, see [File Management Error Codes](../errorcodes/errorcode-filemanagement.md).
**Example**
```js
......@@ -896,9 +1108,9 @@ Renames a file or directory. This API uses a promise to return the result.
};
```
## FileAccessHelper.rename
### rename
rename(uri: string, displayName: string, callback: AsyncCallback&lt;string&gt;) : void;
rename(uri: string, displayName: string, callback: AsyncCallback&lt;string&gt;) : void
Renames a file or directory. This API uses an asynchronous callback to return the result.
......@@ -912,7 +1124,11 @@ Renames a file or directory. This API uses an asynchronous callback to return th
| --- | --- | --- | -- |
| uri | string | Yes| URI of the file or directory to rename.|
| displayName | string | Yes| New name of the file or directory, which can contain the file name extension.|
| callback | AsyncCallback&lt;string&gt; | Yes| Promise used to return the URI of the renamed file or directory.|
| callback | AsyncCallback&lt;string&gt; | Yes| Callback invoked to return the URI of the renamed file or directory.|
**Error codes**
For details about error codes, see [File Management Error Codes](../errorcodes/errorcode-filemanagement.md).
**Example**
......@@ -935,7 +1151,7 @@ Renames a file or directory. This API uses an asynchronous callback to return th
};
```
## FileAccessHelper.access
### access
access(sourceFileUri: string) : Promise&lt;boolean&gt;
......@@ -949,7 +1165,7 @@ Checks whether a file or directory exists. This API uses a promise to return the
| Name| Type| Mandatory| Description|
| --- | --- | --- | -- |
| sourceFileUri | string | Yes| URI of the file or directory.|
| sourceFileUri | string | Yes| URI of the file or directory to check.|
**Return value**
......@@ -957,6 +1173,10 @@ Checks whether a file or directory exists. This API uses a promise to return the
| --- | -- |
| Promise&lt;boolean&gt; | Promise used to return the result.|
**Error codes**
For details about error codes, see [File Management Error Codes](../errorcodes/errorcode-filemanagement.md).
**Example**
```js
......@@ -976,9 +1196,9 @@ Checks whether a file or directory exists. This API uses a promise to return the
};
```
## FileAccessHelper.access
### access
access(sourceFileUri: string, callback: AsyncCallback&lt;boolean&gt;) : void;
access(sourceFileUri: string, callback: AsyncCallback&lt;boolean&gt;) : void
Checks whether a file or directory exists. This API uses an asynchronous callback to return the result.
......@@ -990,8 +1210,12 @@ Checks whether a file or directory exists. This API uses an asynchronous callbac
| Name| Type| Mandatory| Description|
| --- | --- | --- | -- |
| sourceFileUri | string | Yes| URI of the file or directory.|
| callback | AsyncCallback&lt;boolean&gt; | Yes| Promise used to return the result.|
| sourceFileUri | string | Yes| URI of the file or directory to check.|
| callback | AsyncCallback&lt;boolean&gt; | Yes| Callback invoked to return the result.|
**Error codes**
For details about error codes, see [File Management Error Codes](../errorcodes/errorcode-filemanagement.md).
**Example**
......@@ -1017,11 +1241,11 @@ Checks whether a file or directory exists. This API uses an asynchronous callbac
};
```
## FileAccessHelper.getFileInfoFromUri<sup>10+</sup>
### getFileInfoFromUri<sup>10+</sup>
getFileInfoFromUri(uri: string) : Promise<FileInfo>;
getFileInfoFromUri(uri: string) : Promise\<FileInfo>
Obtains a [FileInfo](#fileinfo) object based on the specified URI. This API uses a promise to return the result.
Obtains a **FileInfo** object based on the specified URI. This API uses a promise to return the result.
**System capability**: SystemCapability.FileManagement.UserFileService
......@@ -1037,7 +1261,7 @@ Obtains a [FileInfo](#fileinfo) object based on the specified URI. This API uses
| Type| Description|
| --- | -- |
| [FileInfo](#fileinfo) | Promise used to return the **FileInfo** object obtained.|
| Promise\<[FileInfo](#fileinfo)> | Promise used to return the **FileInfo** object obtained.|
**Example**
......@@ -1054,11 +1278,11 @@ Obtains a [FileInfo](#fileinfo) object based on the specified URI. This API uses
};
```
## FileAccessHelper.getFileInfoFromUri<sup>10+</sup>
### getFileInfoFromUri<sup>10+</sup>
getFileInfoFromUri(uri: string, callback: AsyncCallback<FileInfo>) : void;
getFileInfoFromUri(uri: string, callback: AsyncCallback\<FileInfo>) : void
Obtains a [FileInfo](#fileinfo) object based on the specified URI. This API uses an asynchronous callback to return the result.
Obtains a **FileInfo** object based on the specified URI. This API uses an asynchronous callback to return the result.
**System capability**: SystemCapability.FileManagement.UserFileService
......@@ -1066,10 +1290,10 @@ Obtains a [FileInfo](#fileinfo) object based on the specified URI. This API uses
**Parameters**
| Name| Type| Mandatory| Description|
| --- | --- | --- | -- |
| uri | string | Yes| URI of a file or directory.|
| callback | AsyncCallback&lt;string&gt; | Yes| Callback invoked to return the **FileInfo** object obtained.|
| Name| Type| Mandatory| Description|
| --- | --- | --- | -- |
| uri | string | Yes| URI of the file or directory.|
| callback | AsyncCallback&lt;[FileInfo](#fileinfo)&gt; | Yes| Callback invoked to return the **FileInfo** object obtained.|
**Example**
......@@ -1093,11 +1317,11 @@ Obtains a [FileInfo](#fileinfo) object based on the specified URI. This API uses
```
## FileAccessHelper.getFileInfoFromRelativePath<sup>10+</sup>
### getFileInfoFromRelativePath<sup>10+</sup>
getFileInfoFromRelativePath(relativePath: string) : Promise<FileInfo>;
getFileInfoFromRelativePath(relativePath: string) : Promise\<FileInfo>
Obtains a [FileInfo](#fileinfo) object based on the specified relative path. This API uses a promise to return the result.
Obtains a **FileInfo** object based on the **relativePath**. This API uses a promise to return the result.
**System capability**: SystemCapability.FileManagement.UserFileService
......@@ -1107,19 +1331,19 @@ Obtains a [FileInfo](#fileinfo) object based on the specified relative path. Thi
| Name| Type| Mandatory| Description|
| --- | --- | --- | -- |
| relativePath | string | Yes| Relative path of a file or directory.|
| relativePath | string | Yes| Relative path of the file or directory.|
**Return value**
| Type| Description|
| --- | -- |
| [FileInfo](#fileinfo) | Promise used to return the **FileInfo** object obtained.|
| Promise\<[FileInfo](#fileinfo)> | Promise used to return the **FileInfo** object obtained.|
**Example**
```js
// The relative path of the media library is used as an example.
// In the sample code, relativePath indicates the download directory, which is the relativePath in fileInfo.
// The relative path of the Media Library is used as an example.
// In the sample code, relativePath indicates the Download directory, which is the relativePath in fileInfo.
// You can use the relativePath obtained.
let relativePath = "Download/";
try {
......@@ -1130,11 +1354,11 @@ Obtains a [FileInfo](#fileinfo) object based on the specified relative path. Thi
};
```
## FileAccessHelper.getFileInfoFromRelativePath<sup>10+</sup>
### getFileInfoFromRelativePath<sup>10+</sup>
getFileInfoFromRelativePath(relativePath: string, callback: AsyncCallback<FileInfo>) : void;
getFileInfoFromRelativePath(relativePath: string, callback: AsyncCallback\<FileInfo>) : void
Obtains a [FileInfo](#fileinfo) object based on the specified relative path. This API uses an asynchronous callback to return the result.
Obtains a **FileInfo** object based on the **relativePath**. This API uses an asynchronous callback to return the result.
**System capability**: SystemCapability.FileManagement.UserFileService
......@@ -1144,14 +1368,14 @@ Obtains a [FileInfo](#fileinfo) object based on the specified relative path. Thi
| Name| Type| Mandatory| Description|
| --- | --- | --- | -- |
| relativePath | string | Yes| Relative path of a file or directory.|
| callback | AsyncCallback&lt;string&gt; | Yes| Callback invoked to return the **FileInfo** object obtained.|
| relativePath | string | Yes| Relative path of the file or directory.|
| callback | AsyncCallback&lt;[FileInfo](#fileinfo)&gt; | Yes| Callback invoked to return the **FileInfo** object obtained.|
**Example**
```js
// The relative path of the media library is used as an example.
// In the sample code, relativePath indicates the download directory, which is the relativePath in fileInfo.
// The relative path of the Media Library is used as an example.
// In the sample code, relativePath indicates the Download directory, which is the relativePath in fileInfo.
// You can use the relativePath obtained.
let relativePath = "Download/";
try {
......@@ -1168,7 +1392,7 @@ Obtains a [FileInfo](#fileinfo) object based on the specified relative path. Thi
};
```
## FileAccessHelper.getThumbnail<sup>10+</sup>
### getThumbnail<sup>10+</sup>
getThumbnail(uri: string, size: image.Size) : Promise&lt;image.PixelMap&gt;
......@@ -1189,7 +1413,7 @@ Obtains the **Pixelmap** object of a media file based on the specified URI and s
| Type | Description |
| :---------------------------- | :----------------- |
| Promise&lt;image.PixelMap&gt; | Promise used to return the **Pixelmap** object obtained.|
| Promise&lt;[image.PixelMap](js-apis-image.md#pixelmap7)&gt; | Promise used to return the **Pixelmap** object obtained.|
**Example**
......@@ -1210,7 +1434,7 @@ try {
};
```
## FileAccessHelper.getThumbnail<sup>10+</sup>
### getThumbnail<sup>10+</sup>
getThumbnail(uri: string, size: image.Size, callback: AsyncCallback&lt;image.PixelMap&gt;) : void
......@@ -1226,7 +1450,7 @@ Obtains the **Pixelmap** object of a media file based on the specified URI and s
| -------- | ----------------------------------- | ---- | ------------------ |
| uri | string | Yes | URI of the media file. |
| size | [image.Size](js-apis-image.md#size) | Yes | Size of the thumbnail. |
| callback | AsyncCallback&lt;image.PixelMap&gt; | Yes | Callback invoked to return the **Pixelmap** object obtained.|
| callback | AsyncCallback&lt;[image.PixelMap](js-apis-image.md#pixelmap7)&gt; | Yes | Callback invoked to return the **Pixelmap** object obtained.|
**Example**
......@@ -1252,79 +1476,277 @@ try {
};
```
## RootIterator.next
### query<sup>10+</sup>
next( ) : { value: RootInfo, done: boolean }
query(uri:string, metaJson: string) : Promise&lt;string&gt;
Obtains the next-level device root directory. **RootIterator** is an iterator object of the device root directory.
Queries the attribute information about a file or directory based on the URI. This API uses a promise to return the result.
**System capability**: SystemCapability.FileManagement.UserFileService
**Required permissions**: ohos.permission.FILE_ACCESS_MANAGER
**Parameters**
| Name | Type | Mandatory| Description |
| -------- | ------ | ---- | ---------------------------------------------------- |
| uri | string | Yes | URI of the file or directory (obtained from [FileInfo](#fileinfo)).|
| metaJson | string | Yes | Attribute [FILEKEY](#filekey10) to query. |
**Return value**
| Type| Description|
| --- | -- |
| {value: RootInfo, done: boolean} | Root directory information obtained. This API traverses the directory until **done** returns **true**. The **value** field contains the root directory information.|
| Type | Description |
| :-------------------- | :------------------------------- |
| Promise&lt;string&gt; | Promised used to return the attribute queried and the value obtained.|
**Example**
## FileIterator.next
```js
var imageFileRelativePath = "Download/queryTest/image/01.jpg";
var jsonStrSingleRelativepath = JSON.stringify({ [fileAccess.FileKey.RELATIVE_PATH]: "" });
try {
// Obtain fileAccessHelper by referring to the sample code of fileAccess.createFileAccessHelper.
var fileInfo = await fileAccessHelper.getFileInfoFromRelativePath(imageFileRelativePath);
let queryResult = await fileAccessHelper.query(fileInfo.uri, jsonStrSingleRelativepath);
console.log("query_file_single faf query, queryResult.relative_path: " + JSON.parse(queryResult).relative_path);
} catch (error) {
console.error("query_file_single faf query failed, error.code :" + error.code + ", errorMessage :" + error.message);
};
```
next( ) : { value: FileInfo, done: boolean }
### query<sup>10+</sup>
Obtains the information about the next-level file or directory. **FileIterator** is an iterator object of a directory.
query(uri:string, metaJson: string, callback: AsyncCallback&lt;string&gt;) : void
Queries the attribute information about a file or directory based on the URI. This API uses an asynchronous callback to return the result.
**System capability**: SystemCapability.FileManagement.UserFileService
**Required permissions**: ohos.permission.FILE_ACCESS_MANAGER
**Parameters**
| Name | Type | Mandatory| Description |
| -------- | --------------------------- | ---- | ---------------------------------------------------- |
| uri | string | Yes | URI of the file or directory (obtained from [FileInfo](#fileinfo)).|
| metaJson | string | Yes | Attribute [FILEKEY](#filekey10) to query. |
| callback | AsyncCallback&lt;string&gt; | Yes | Callback invoked to return the attribute queried and the value obtained. |
**Example**
```js
var imageFileRelativePath = "Download/queryTest/image/01.jpg";
var jsonStrSingleRelativepath = JSON.stringify({ [fileAccess.FileKey.RELATIVE_PATH]: "" });
try {
// Obtain fileAccessHelper by referring to the sample code of fileAccess.createFileAccessHelper.
var fileInfo = await fileAccessHelper.getFileInfoFromRelativePath(imageFileRelativePath);
fileAccessHelper.query(fileInfo.uri, jsonStrSingleRelativepath, (err, queryResult)=>{
if (err) {
console.log("query_file_single faf query Failed, errCode:" + err.code + ", errMessage:" + err.message);
return;
}
console.log("query_file_single faf query, queryResult.relative_path: " + JSON.parse(queryResult).relative_path);
})
} catch (error) {
console.error("query_file_single faf query failed, error.code :" + error.code + ", errorMessage :" + error.message);
};
```
### copy<sup>10+</sup>
copy(sourceUri: string, destUri: string, force?: boolean) : Promise&lt;Array&lt;CopyResult&gt;&gt;
Copies a file or directory. This API uses a promise to return the result.
**System capability**: SystemCapability.FileManagement.UserFileService
**Required permissions**: ohos.permission.FILE_ACCESS_MANAGER
**Parameters**
| Name | Type | Mandatory| Description |
| --------- | ------- | ---- | ------------------------------------------------------------ |
| sourceUri | string | Yes | URI of the file or directory to copy, for example, **datashare:///media/file/102**. |
| destUri | string | Yes | URI of the destination directory, for example, **datashare:///media/file/101**. |
| force | boolean | No | Whether to forcibly overwrite the file with the same name. <br>If **force** is **true**, the file with the same name will be overwritten. If **force** is **false** or not specified, the file with the same name will not be overwritten.|
**Return value**
| Type| Description|
| --- | -- |
| {value: FileInfo, done: boolean} | File or directory information obtained. This API traverses the specified directory until **done** returns **true**. The **value** field contains the file or directory information obtained.|
| Type | Description |
| :------------------------------------------------------ | :----------------------------------------------------------- |
| Promise&lt;Array&lt;[CopyResult](#copyresult10)&gt;&gt; | Promise used to return the result. If the file or directory is copied successfully, no information is returned. If the file copy fails, **copyResult** is returned.|
## RootInfo
Example 1: Copy a file with **force** unspecified.
Represents the root attribute information and interface capabilities of a device.
```js
// The media library URI is used as an example.
// In the sample code, sourceFile indicates the file (directory) in the Download directory to copy, destFile indicates the destination directory in the Download directory, and uri is to URI in fileInfo.
// You can use the URI obtained.
let sourceFile = "datashare:///media/file/102";
let destFile = "datashare:///media/file/101";
try {
// Obtain fileAccessHelper by referring to the sample code of fileAccess.createFileAccessHelper.
let copyResult = await fileAccessHelper.copy(sourceFile, destFile);
if (copyResult.length === 0) {
console.log("copy success");
} else {
for (let i = 0; i < copyResult.length; i++) {
console.error("errCode" + copyResult[i].errCode);
console.error("errMsg" + copyResult[i].errMsg);
console.error("sourceUri" + copyResult[i].sourceUri);
console.error("destUri" + copyResult[i].destUri);
}
}
} catch (error) {
console.error("copy failed, errCode:" + error.code + ", errMessage:" + error.message);
}
```
Example 2: Copy a file or directory when **force** set to **true**.
```js
// The media library URI is used as an example.
// In the sample code, sourceFile indicates the file (directory) in the Download directory to copy, destFile indicates the destination directory in the Download directory, and uri is to URI in fileInfo.
// You can use the URI obtained.
let sourceFile = "datashare:///media/file/102";
let destFile = "datashare:///media/file/101";
try {
// Obtain fileAccessHelper by referring to the sample code of fileAccess.createFileAccessHelper.
let copyResult = await fileAccessHelper.copy(sourceFile, destFile, true);
if (copyResult.length === 0) {
console.log("copy success");
} else {
for (let i = 0; i < copyResult.length; i++) {
console.error("errCode" + copyResult[i].errCode);
console.error("errMsg" + copyResult[i].errMsg);
console.error("sourceUri" + copyResult[i].sourceUri);
console.error("destUri" + copyResult[i].destUri);
}
}
} catch (error) {
console.error("copy failed, errCode:" + error.code + ", errMessage:" + error.message);
}
```
### copy<sup>10+</sup>
copy(sourceUri: string, destUri: string, callback: AsyncCallback&lt;Array&lt;CopyResult&gt;&gt;) : void
Copies a file or directory. This API uses an asynchronous callback to return the result.
**System capability**: SystemCapability.FileManagement.UserFileService
**Required permissions**: ohos.permission.FILE_ACCESS_MANAGER
### Attributes
**Parameters**
| Name| Type | Readable| Writable| Description |
| ------ | ------ | -------- | ------ | -------- |
| deviceType | number | Yes| No|Device type.|
| uri | string | Yes| No| Root directory URI of the device.|
| relativePath<sup>10+</sup> | string | Yes| No| Relative path of the root directory.|
| displayName | string | Yes| No| Device name.|
| deviceFlags | number | Yes| No| Capabilities supported by the device.|
| Name | Type | Mandatory| Description |
| --------- | ------------------------------------------------ | ---- | ------------------------------------------------------------ |
| sourceUri | string | Yes | URI of the file or directory to copy, for example, **datashare:///media/file/102**. |
| destUri | string | Yes | URI of the destination directory, for example, **datashare:///media/file/101**. |
| callback | AsyncCallback&lt;Array&lt;[CopyResult](#copyresult10)&gt;&gt; | Yes | Callback invoked to return the result. If the file or directory is copied successfully, no information is returned. If the file copy fails, **copyResult** is returned.|
## FileInfo
**Example**
Represents the file or directory attribute information and interface capabilities.
```js
// The media library URI is used as an example.
// In the sample code, sourceFile indicates the file (directory) in the Download directory to copy, destFile indicates the destination directory in the Download directory, and uri is to URI in fileInfo.
// You can use the URI obtained.
let sourceFile = "datashare:///media/file/102";
let destFile = "datashare:///media/file/101";
try {
// Obtain fileAccessHelper by referring to the sample code of fileAccess.createFileAccessHelper.
fileAccessHelper.copy(sourceFile, destFile, async (err, copyResult) => {
if (err) {
console.error("copy failed, errCode:" + err.code + ", errMessage:" + err.message);
return;
}
if (copyResult.length === 0) {
console.log("copy success");
} else {
for (let i = 0; i < copyResult.length; i++) {
console.error("errCode" + copyResult[i].errCode);
console.error("errMsg" + copyResult[i].errMsg);
console.error("sourceUri" + copyResult[i].sourceUri);
console.error("destUri" + copyResult[i].destUri);
}
}
});
} catch (error) {
console.error("copy failed, errCode:" + error.code + ", errMessage:" + error.message);
}
```
### copy<sup>10+</sup>
copy(sourceUri: string, destUri: string, force: boolean, callback: AsyncCallback&lt;Array&lt;CopyResult&gt;&gt;) : void
Copies a file or directory. This API uses an asynchronous callback to return the result.
**System capability**: SystemCapability.FileManagement.UserFileService
**Required permissions**: ohos.permission.FILE_ACCESS_MANAGER
### Attributes
**Parameters**
| Name| Type | Readable| Writable| Description |
| ------ | ------ | -------- | ------ | -------- |
| uri | string | Yes| No| URI of the file or directory.|
| relativePath<sup>10+</sup> | string | Yes| No| Relative path of a file or directory.|
| fileName | string | Yes| No| Name of a file or directory.|
| mode | number | Yes| No| Permissions on the file or directory.|
| size | number | Yes| No| Size of the file or directory.|
| mtime | number | Yes| No| Time when the file or directory was last modified.|
| mimeType | string | Yes| No| MIME type of the file or directory.|
| Name | Type | Mandatory| Description |
| --------- | ------------------------------------------------ | ---- | ------------------------------------------------------------ |
| sourceUri | string | Yes | URI of the file or directory to copy, for example, **datashare:///media/file/102**. |
| destUri | string | Yes | URI of the destination directory, for example, **datashare:///media/file/101**. |
| force | boolean | Yes | Whether to forcibly overwrite the file with the same name. <br>If **force** is **true**, the file with the same name will be overwritten. If **force** is **false** or not specified, the file with the same name will not be overwritten.|
| callback | AsyncCallback&lt;Array&lt;[CopyResult](#copyresult10)&gt;&gt; | Yes | Callback invoked to return the result. If the file or directory is copied successfully, no information is returned. If the file copy fails, **copyResult** is returned.|
**Example**
```js
// The media library URI is used as an example.
// In the sample code, sourceFile indicates the file (directory) in the Download directory to copy, destFile indicates the destination directory in the Download directory, and uri is to URI in fileInfo.
// You can use the URI obtained.
let sourceFile = "datashare:///media/file/102";
let destFile = "datashare:///media/file/101";
try {
// Obtain fileAccessHelper by referring to the sample code of fileAccess.createFileAccessHelper.
fileAccessHelper.copy(sourceFile, destFile, true, async (err, copyResult) => {
if (err) {
console.error("copy failed, errCode:" + err.code + ", errMessage:" + err.message);
return;
}
if (copyResult.length === 0) {
console.log("copy success");
} else {
for (let i = 0; i < copyResult.length; i++) {
console.error("errCode" + copyResult[i].errCode);
console.error("errMsg" + copyResult[i].errMsg);
console.error("sourceUri" + copyResult[i].sourceUri);
console.error("destUri" + copyResult[i].destUri);
}
}
});
} catch (error) {
console.error("copy failed, errCode:" + error.code + ", errMessage:" + error.message);
}
```
## CopyResult<sup>10+</sup>
Defines the information returned when the file copy operation fails. If the copy operation is successful, no information is returned.
**System capability**: SystemCapability.FileManagement.UserFileService
**Required permissions**: ohos.permission.FILE_ACCESS_MANAGER
| Name | Type | Readable| Writable| Description |
| --------- | ------ | ---- | ---- | ----------------- |
| sourceUri | string | Yes | No | URI of the source file or directory. |
| destUri | string | Yes | No | URI of the conflict file. If the error is not caused by a conflict, **destUri** is empty.|
| errCode | number | Yes | No | Error code. |
| errMsg | string | Yes | No | Error information. |
## OPENFLAGS
Enumerates the modes for opening a file.
Enumerates the file open modes.
**Model restriction**: This API can be used only in the stage model.
**System capability**: SystemCapability.FileManagement.UserFileService
......@@ -1333,3 +1755,22 @@ Enumerates the modes for opening a file.
| READ | 0o0 | Read mode.|
| WRITE | 0o1 | Write mode.|
| WRITE_READ | 0o2 | Read/Write mode.|
## FILEKEY<sup>10+</sup>
Enumerates the keys of the file attributes to query.
**Model restriction**: This API can be used only in the stage model.
**System capability**: SystemCapability.FileManagement.UserFileService
| Name | Value | Description |
| ------------- | ------------- | ----------------------------------- |
| DISPLAY_NAME | 'display_name' | Name of the file. |
| DATE_ADDED | 'date_added' | Date when the file was created, for example, **1501925454**. |
| DATE_MODIFIED | 'date_modified' | Date when a file was modified, for example, **1665310670**. |
| RELATIVE_PATH | 'relative_path' | Relative path of the file, for example, **Pictures/Screenshots/**.|
| FILE_SIZE | 'size' | Size of a file, in bytes. |
| WIDTH | 'width' | Width of the image file, in pixels. |
| HEIGHT | 'height' | Height of the image file, in pixels. |
| DURATION | 'duration' | Duration of the audio or video file, in milliseconds. |
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册