@@ -59,47 +59,52 @@ Users can access files stored in the public directories through the system appli
...
@@ -59,47 +59,52 @@ Users can access files stored in the public directories through the system appli
You can call [mediaLibrary.FileAsset.open](../reference/apis/js-apis-medialibrary.md#open8-1) to open a file in a public directory.
You can call [mediaLibrary.FileAsset.open](../reference/apis/js-apis-medialibrary.md#open8-1) to open a file in a public directory.
You can call [fileio.open](../reference/apis/js-apis-fileio.md#fileioopen7) to open a file in the application sandbox. The sandbox directory can be accessed only through the application context.
You can call [fs.open](../reference/apis/js-apis-file-fs.md#fsopen) to open a file in the application sandbox. The sandbox directory can be accessed only through the application context.
**Prerequisites**
**Prerequisites**
- You have obtained a **MediaLibrary** instance.
- You have obtained a **MediaLibrary** instance.
- You have granted the permission **ohos.permission.WRITE_MEDIA**.
- You have granted the permissions **ohos.permission.READ_MEDIA** and **ohos.permission.WRITE_MEDIA**.
- You have imported the module [@ohos.fileio](../reference/apis/js-apis-fileio.md) in addition to @ohos.multimedia.mediaLibrary.
- You have imported the module [@ohos.file.fs](../reference/apis/js-apis-file-fs.md) in addition to @ohos.multimedia.mediaLibrary.
- The **testFile.txt** file has been created and contains content.
**How to Develop**
**How to Develop**
1. Call [context.filesDir](../reference/apis/js-apis-inner-app-context.md#contextgetfilesdir) to obtain the directory of the application sandbox.
1. Call [context.filesDir](../reference/apis/js-apis-file-fs.md) to obtain the directory of the application sandbox.
2. Call **MediaLibrary.getFileAssets** and **FetchFileResult.getFirstObject** to obtain the first file in the result set of the public directory.
2. Call **MediaLibrary.getFileAssets** and **FetchFileResult.getFirstObject** to obtain the first file in the result set of the public directory.
3. Call **fileio.open** to open the file in the sandbox.
3. Call **fs.open** to open the file in the sandbox.
4. Call **fileAsset.open** to open the file in the public directory.
4. Call **fileAsset.open** to open the file in the public directory.
5. Call **fileio.copyfile** to copy the file.
5. Call [fs.copyfile](../reference/apis/js-apis-file-fs.md#fscopyfile) to copy the file.
6. Call **fileAsset.close** and **fileio.close** to close the file.
6. Call **fileAsset.close** and [fs.close](../reference/apis/js-apis-file-fs.md#fsclose) to close the file.
**Example 1: Copying Files from the Public Directory to the Sandbox**
**Example 1: Copying Files from the Public Directory to the Sandbox**
console.error('read text failed, message = ',err);
}
}
console.log('content read from public file: ',content_pub);
console.info('content read from public file: ',content_pub);
}
}
```
```
### Reading and Writing a File
### Reading and Writing a File
You can use **FileAsset.open** and **FileAsset.close** of [mediaLibrary](../reference/apis/js-apis-medialibrary.md) to open and close a file, and use **fileio.read** and **fileio.write** of [fileio](../reference/apis/js-apis-fileio.md) to read and write a file.
You can use **FileAsset.open** and **FileAsset.close** of [mediaLibrary](../reference/apis/js-apis-medialibrary.md) to open and close a file, and use **fs.read** and **fs.write** in [file.fs](../reference/apis/js-apis-file-fs.md) to read and write the file.
**Prerequisites**
**Prerequisites**
- You have obtained a **MediaLibrary** instance.
- You have obtained a **MediaLibrary** instance.
- You have granted the permission **ohos.permission.WRITE_MEDIA**.
- You have granted the permissions **ohos.permission.READ_MEDIA** and**ohos.permission.WRITE_MEDIA**.
- You have imported the module [@ohos.fileio](../reference/apis/js-apis-fileio.md) in addition to @ohos.multimedia.mediaLibrary.
- You have imported the module [@ohos.file.fs](../reference/apis/js-apis-file-fs.md) in addition to @ohos.multimedia.mediaLibrary.
@@ -64,64 +64,64 @@ After configuring the permissions in the **module.json5** file, the application
...
@@ -64,64 +64,64 @@ After configuring the permissions in the **module.json5** file, the application
1. Declare the permissions in the **module.json5** file. Add the **requestPermissions** tag under **module** in the file, and set the tag based on the project requirements. For details about the tag, see [Guide for Requesting Permissions from User](../security/accesstoken-guidelines.md).
1. Declare the permissions in the **module.json5** file. Add the **requestPermissions** tag under **module** in the file, and set the tag based on the project requirements. For details about the tag, see [Guide for Requesting Permissions from User](../security/accesstoken-guidelines.md).
```json
```json
{
{
"module":{
"module":{
"requestPermissions":[
"requestPermissions":[
{
{
"name":"ohos.permission.MEDIA_LOCATION",
"name":"ohos.permission.MEDIA_LOCATION",
"reason":"$string:reason",
"reason":"$string:reason",
"usedScene":{
"usedScene":{
"abilities":[
"abilities":[
"EntryAbility"
"EntryAbility"
],
],
"when":"always"
"when":"always"
}
}
},
},
{
{
"name":"ohos.permission.READ_MEDIA",
"name":"ohos.permission.READ_MEDIA",
"reason":"$string:reason",
"reason":"$string:reason",
"usedScene":{
"usedScene":{
"abilities":[
"abilities":[
"EntryAbility"
"EntryAbility"
],
],
"when":"always"
"when":"always"
}
}
},
},
{
{
"name":"ohos.permission.WRITE_MEDIA",
"name":"ohos.permission.WRITE_MEDIA",
"reason":"$string:reason",
"reason":"$string:reason",
"usedScene":{
"usedScene":{
"abilities":[
"abilities":[
"EntryAbility"
"EntryAbility"
],
],
"when":"always"
"when":"always"
}
}
}
}
]
]
}
}
}
}
```
```
2. In the **Ability.ts** file, call **requestPermissionsFromUser** in the **onWindowStageCreate** callback to check for the required permissions and if they are not granted, request the permissions from the user by displaying a dialog box.
2. In the **Ability.ts** file, call **requestPermissionsFromUser** in the **onWindowStageCreate** callback to check for the required permissions and if they are not granted, request the permissions from the user by displaying a dialog box.
console.error('Failed to get first object: '+err);
});
}
}
```
```
### Querying Media Assets with the Specified Date
### Querying Media Assets with the Specified Date
The following describes how to obtain media assets that are added on the specified date. You can also use the modification date and shooting date as the retrieval conditions.
The following describes how to obtain all the media assets that are added from the specified date. You can also use the modification date and shooting date as the retrieval conditions.
To specify the date when the files are added as the retrieval condition, set **selections** to **FileKey.DATE_ADDED**.
To specify the date when the files are added as the retrieval condition, set **selections** to **FileKey.DATE_ADDED**.
...
@@ -64,23 +67,26 @@ To specify the date 2022-8-5, set **selectionArgs** to **2022-8-5**.
...
@@ -64,23 +67,26 @@ To specify the date 2022-8-5, set **selectionArgs** to **2022-8-5**.
You can obtain media assets in an album in either of the following ways:
You can obtain media assets in an album in either of the following ways:
- Call [MediaLibrary.getFileAssets](../reference/apis/js-apis-medialibrary.md#getfileassets7-1) with an album specified, as described in [Querying Media Assets with the Specfied Album Name](#querying-media-assets-with-the-specified-album-name).
- Call [MediaLibrary.getFileAssets](../reference/apis/js-apis-medialibrary.md#getfileassets7-1) with an album specified, as described in [Querying Media Assets with the Specfied Album Name](#querying-media-assets-with-the-specified-album-name).
- Call [Album.getFileAssets](../reference/apis/js-apis-medialibrary.md#getfileassets7-3) to obtain an **Album** instance, so as to obtain the media assets in it.
- Call [Album.getFileAssets](../reference/apis/js-apis-medialibrary.md#getfileassets7-3) to obtain an **Album** instance, so as to obtain the media assets in it.
...
@@ -163,24 +170,24 @@ The following describes how to obtain videos in an album named **New Album 1**.
...
@@ -163,24 +170,24 @@ The following describes how to obtain videos in an album named **New Album 1**.
1. Create a retrieval condition for obtaining the target **Album** instance.
1. Create a retrieval condition for obtaining the target **Album** instance.
```ts
```ts
letfileKeyObj=mediaLibrary.FileKey;
letfileKeyObj=mediaLibrary.FileKey;
letAlbumNoArgsFetchOp={
letAlbumNoArgsFetchOp={
selections:fileKeyObj.ALBUM_NAME+'= ?',
selections:fileKeyObj.ALBUM_NAME+'= ?',
selectionArgs:['New Album 1']
selectionArgs:['New Album 1']
}
}
```
```
2. Create a retrieval condition for obtaining videos in the target album.
2. Create a retrieval condition for obtaining videos in the target album.
```ts
```ts
letfileKeyObj=mediaLibrary.FileKey;
letfileKeyObj=mediaLibrary.FileKey;
letimageType=mediaLibrary.MediaType.VIDEO;
letvideoType=mediaLibrary.MediaType.VIDEO;
letimagesFetchOp={
letvideoFetchOp={
selections:fileKeyObj.MEDIA_TYPE+'= ?',
selections:fileKeyObj.MEDIA_TYPE+'= ?',
selectionArgs:[imageType.toString()],
selectionArgs:[videoType.toString()],
}
}
```
```
3. Call **Album.getFileAssets** to obtain the videos in the target album.
3. Call **Album.getFileAssets** to obtain the videos in the target album.