The **fileAccess** module is a framework for accessing and operating user files based on the extension mechanism. This module interacts with diverse file management services, such as the media library and external storage management service, and provides a set of unified 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 is a framework for accessing and operating user files based on the Extension ability 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.
>**NOTE**
>**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 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 the file manager app.
>- 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 **Files**.
## Modules to Import
## Modules to Import
...
@@ -21,17 +21,13 @@ Obtains information about all wants with **extension** set to **fileAcesss** in
...
@@ -21,17 +21,13 @@ Obtains information about all wants with **extension** set to **fileAcesss** in
console.error("createFileAccessHelper interface returns an undefined object");
console.error("createFileAccessHelper interface returns an undefined object");
...
@@ -135,22 +133,18 @@ Synchronously creates a **Helper** object that connects to all file management s
...
@@ -135,22 +133,18 @@ Synchronously creates a **Helper** object that connects to all file management s
getRoots( ) : Promise<RootIterator>
getRoots( ) : Promise<RootIterator>
Obtains information about the root nodes for the devices of the file management service type connected to the **Helper** object. This API uses a promise to return a **RootIterator** object, which
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).
returns [RootInfo](#rootinfo) by using [next()](#rootiteratornext).
| Promise<RootIterator> | Promise used to return the**RootIterator** object obtained.|
**Example**
**Example**
...
@@ -160,7 +154,8 @@ None
...
@@ -160,7 +154,8 @@ None
letrootinfos=[];
letrootinfos=[];
letisDone=false;
letisDone=false;
try{
try{
rootIterator=awaitfileAccesssHelper.getRoots();
// Obtain fileAccessHelper by referring to the sample code of fileAccess.createFileAccessHelper.
rootIterator=awaitfileAccessHelper.getRoots();
if(!rootIterator){
if(!rootIterator){
console.error("getRoots interface returns an undefined object");
console.error("getRoots interface returns an undefined object");
return;
return;
...
@@ -182,7 +177,7 @@ None
...
@@ -182,7 +177,7 @@ None
listFile(filter?: Filter) : FileIterator
listFile(filter?: Filter) : FileIterator
Synchronously obtains the **FileIterator** object of the first-level files (file folder) matching the conditions of the filter from the root node of a device. The **FileIterator** object then returns [FileInfo](#fileinfo) by using [next()](#fileiteratornext).
Synchronously obtains the **FileIterator** object of the first-level files (file folder) matching the conditions of the filter from the device root node. The **FileIterator** object then returns [FileInfo](#fileinfo) by using [next()](#fileiteratornext).
// let filter = {suffix : [".txt", ".jpg", ".xlsx"]};
// let filter = {suffix : [".txt", ".jpg", ".xlsx"]};
letrootInfo=rootinfos[0];
letrootInfo=rootinfos[0];
letfileInfos=[];
letfileInfos=[];
letisDone=false;
letisDone=false;
try{
try{
letfileIterator=rootInfo.listFile();
letfileIterator=rootInfo.listFile();
// ListFile that contains the filter implementation.
// listFile contains the filter implementation.
// let fileIterator = rootInfo.listFile(filter);
// let fileIterator = rootInfo.listFile(filter);
if(!fileIterator){
if(!fileIterator){
console.error("listFile interface returns an undefined object");
console.error("listFile interface returns an undefined object");
...
@@ -233,7 +228,7 @@ Synchronously obtains the **FileIterator** object of the first-level files (file
...
@@ -233,7 +228,7 @@ Synchronously obtains the **FileIterator** object of the first-level files (file
scanFile(filter?: Filter) : FileIterator
scanFile(filter?: Filter) : FileIterator
Recursively obtains the **FileIterator** object of the files matching the conditions of the filter from the root node of a device synchronously. The **FileIterator** object then returns [FileInfo](#fileinfo) by using the[next()](#fileiteratornext).
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).
@@ -362,7 +357,7 @@ Recursively obtains the **FileIterator** object of the files matching the condit
...
@@ -362,7 +357,7 @@ Recursively obtains the **FileIterator** object of the files matching the condit
letisDone=false;
letisDone=false;
try{
try{
letfileIterator=fileInfoDir.scanFile();
letfileIterator=fileInfoDir.scanFile();
// ScanFile that contains the filter implementation
// scanFile contains the filter implementation.
// let fileIterator = rootInfo.scanFile(filter);
// let fileIterator = rootInfo.scanFile(filter);
if(!fileIterator){
if(!fileIterator){
console.error("scanFile interface returns an undefined object");
console.error("scanFile interface returns an undefined object");
...
@@ -370,7 +365,7 @@ Recursively obtains the **FileIterator** object of the files matching the condit
...
@@ -370,7 +365,7 @@ Recursively obtains the **FileIterator** object of the files matching the condit
}
}
while(!isDone){
while(!isDone){
letresult=fileIterator.next();
letresult=fileIterator.next();
console.error("next result = "+JSON.stringify(result));
console.log("next result = "+JSON.stringify(result));
isDone=result.done;
isDone=result.done;
if(!isDone)
if(!isDone)
subfileInfos.push(result.value);
subfileInfos.push(result.value);
...
@@ -392,25 +387,28 @@ Creates a file in a directory. This API uses a promise to return the result.
...
@@ -392,25 +387,28 @@ Creates a file in a directory. This API uses a promise to return the result.
**Parameters**
**Parameters**
| Name| Type| Mandatory| Description|
| Name| Type| Mandatory| Description|
| --- | --- | --- | -- |
| --- | --- | --- | -- |
| uri | string | Yes| URI of the parent directory for the file to create.|
| uri | string | Yes| URI of the parent directory for the file to create.|
| displayName | string | Yes| Name of the file to create. A file name extension must be added for a local file. It is not required for a file stored in a shared disk.|
| displayName | string | Yes| Name of the file to create. A file name extension must be added for a local file. It is not required for a file stored in a shared disk.|
**Return value**
**Return value**
| Type| Description|
| Type| Description|
| --- | -- |
| --- | -- |
| Promise<string> | URI of the file created.|
| Promise<string> | Promise used to return the URI of the file created.|
**Example**
**Example**
```js
```js
// The URI of a shared disk is used as an example.