@@ -36,7 +36,7 @@ You can use [ohos.file.fs](../reference/apis/js-apis-file-fs.md) to implement ac
First, obtain the [application file path](../application-models/application-context-stage.md#obtaining-application-file-paths). The following example shows how to obtain a HAP file path using **UIAbilityContext**. For details about how to obtain **UIAbilityContext**, see [Obtaining the Context of UIAbility](../application-models/uiability-usage.md#obtaining-the-context-of-uiability).
Then, perform common file operations.
Then, perform file operations.
### Creating, Reading, and Writing a File
...
...
@@ -46,21 +46,29 @@ The following example demonstrates how to create a file, read data from it, and
// After the applications to be restored are added, call getFileHandle() to obtain the handles of the application files to be restored based on the application name.
// The number of application data files to be restored varies depending on the number of backup files. The following is only an example.
awaitg_session.getFileHandle({
lethandle:backup.FileMeta={
bundleName:restoreApps[0],
uri:"manage.json"
});
awaitg_session.getFileHandle({
bundleName:restoreApps[0],
uri:"1.tar"
});
}
awaitg_session.getFileHandle(handle);
handle.uri="1.tar";
awaitg_session.getFileHandle(handle);
console.info('getFileHandle success');
}
```
...
...
@@ -249,23 +265,30 @@ If the application has not been installed, you can install the application and t
@@ -8,46 +8,52 @@ For details about the APIs used to develop a file manager application, see [User
## How to Develop
1. Apply for permissions required.<br>
Apply for the **ohos.permission.FILE_ACCESS_MANAGER** and **ohos.permission.GET_BUNDLE_INFO_PRIVILEGED** permissions. For details, see [Applying for Permissions](../security/accesstoken-guidelines.md).
1. Apply for permissions required.
Apply for the **ohos.permission.FILE_ACCESS_MANAGER** and **ohos.permission.GET_BUNDLE_INFO_PRIVILEGED** permissions. For details, see [Applying for Permissions](../security/accesstoken-guidelines.md).
> **NOTE**
>
> **ohos.permission.FILE_ACCESS_MANAGER** allows your application to use the user file access framework APIs.
>
> **ohos.permission.GET_BUNDLE_INFO_PRIVILEGED** allows your application to obtain information about file management server applications supported by the system.
> - **ohos.permission.FILE_ACCESS_MANAGER** allows your application to use the user file access framework APIs.
>- **ohos.permission.GET_BUNDLE_INFO_PRIVILEGED** allows your application to obtain information about file management server applications supported by the system.
The **fileAccess** module provides APIs for basic file operations, and the **fileExtensionInfo** module provides key structs for application development.
3. Query device information.<br>
3. Query device information.
You can obtain attributes of the devices managed by one or all file management servers in the system. You can also filter devices as required.
In the user file access framework, **RootInfo** indicates the attribute information of a device. For example, obtain **RootInfo** of all devices.
In the user file access framework, **FileInfo** indicates basic information about a file (directory). You can use **listfile()** to obtain a **FileIterator** object that traverses all files (directories) of the next level or use **scanfile()** to obtain a **FileIterator** object that meets the specified conditions.
Currently, **listfile()** and **scanfile()** can be called by the **RootInfo** object to traverse the next-level files or filter the entire directory tree. In addition, **listfile()** and **scanfile()** can be called by the **FileInfo** object to traverse the next-level files or filter the specified directories.
```ts
// Start from the root directory.
letrootInfo=rootinfos[0];
letfileInfos=[];
letisDone=false;
letfilter={suffix:[".txt",".jpg",".xlsx"]};// Set filter criteria.
letrootInfo=rootInfos[0];
letfileInfos:Array<fileAccess.FileInfo>=[];
letisDone:boolean=false;
letfilter:Filter={suffix:[".txt",".jpg",".xlsx"]};// Set the filter.
try{
letfileIterator=rootInfo.listFile();// Traverse the root directory of rootinfos[0] and return an iterator object.
// let fileIterator = rootInfo.scanFile(filter); // Filter device rootinfos[0] files that meet the specified conditions and return an iteration object.
if(!fileIterator){
console.error("listFile interface returns an undefined object");
return;
}
while(!isDone){
letresult=fileIterator.next();
...
...
@@ -92,35 +99,37 @@ For details about the APIs used to develop a file manager application, see [User
You can integrate APIs of the user file access framework to implement user behaviors, such as deleting, renaming, creating, and moving a file (directory). The following example shows how to create a file. For details about other APIs, see [User File Access and Management](../reference/apis/js-apis-fileAccess.md).
```ts
...
...
@@ -128,18 +137,20 @@ For details about the APIs used to develop a file manager application, see [User
// Create a file.
// sourceUri is the URI in fileinfo of the Download directory.
// You need to use the obtained URI for development.
@@ -12,7 +12,7 @@ You can use the related APIs to [share a file with another application](#sharing
The file URIs are in the following format:
**file**://<*bundleName*>/<*path*>
**file://**<bundleName>/<path>
-**file**: indicates a file URI.
...
...
@@ -43,6 +43,7 @@ Before sharing application files, you need to [obtain the application file path]
```
2. Set the target application, with which you want to share the file, and grant permissions on the file.
Use [startAbility()](../reference/apis/js-apis-inner-application-uiAbilityContext.md#uiabilitycontextstartability) to share the file with the target application. You need to pass in the obtained URI in **uri** of the **want** parameter, set the type of the file to share, set **action** to **ohos.want.action.sendData**, and set the granted permission on the file in **flags**. For details, see [Want](../reference/apis/js-apis-app-ability-want.md#attributes).
> **NOTE**
...
...
@@ -54,6 +55,8 @@ Before sharing application files, you need to [obtain the application file path]
console.error(`Invoke startAbility failed, code is ${err.code}, message is ${err.message}`);
});
}
...
// ...
}
```
## Using Shared Files
In the [**module.json5** file](../quick-start/module-configuration-file.md) of the application, which wants to use the shared file, set **actions** to **ohos.want.action.sendData** to allow the application to receive files shared by another application and set **uris** to the type of the URI to receive. In the following example, the application receives only .txt files with **scheme** of **file**.
...
...
@@ -120,10 +123,12 @@ After obtaining the URI of the shared file from **want**, the application can ca
```ts
// xxx.ets
importfsfrom'@ohos.file.fs';
importWantfrom'@ohos.app.ability.Want';
import{BusinessError}from'@ohos.base';
functiongetShareFile(){
try{
letwant=...;// Obtain the want information sent from the application that shares the file.
letwant:Want=...;// Obtain the want sent from the application that shares the file.
// Obtain the uri field from the want information.
leturi=want.uri;
...
...
@@ -135,11 +140,13 @@ function getShareFile() {
// Perform operations on the URI of the shared file as required. For example, open the URI to obtain the file object in read/write mode.
letfile=fs.openSync(uri,fs.OpenMode.READ_WRITE);
console.info('open file successfully!');
}catch(error){
}catch(err){
leterror:BusinessError=errasBusinessError;
console.error(`Invoke openSync failed, code is ${error.code}, message is ${error.message}`);
}
}catch(error){
console.error(`Invoke openSync failed, code is ${error.code}, message is ${error.message}`);
leterr:BusinessError=errorasBusinessError;
console.error(`Invoke openSync failed, code is ${err.code}, message is ${err.message}`);