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

!21127 [翻译完成】#I7DLQB

Merge pull request !21127 from Annie_wang/PR19690
...@@ -3,11 +3,12 @@ ...@@ -3,11 +3,12 @@
> **NOTE** > **NOTE**
> >
> - The APIs of this module are supported since API version 6. Updates will be marked with a superscript to indicate their earliest API version. > - The APIs of this module are supported since API version 6. Updates will be marked with a superscript to indicate their earliest API version.
> - This API is deprecated since API version 9 and will be retained until API version 13. > - The APIs of this module are deprecated since API version 9 and will be retained until API version 13.
> - Certain functionalities are changed as system APIs and can be used only by system applications. To use these functionalities, call [@ohos.filemanagement.userFileManager](js-apis-userFileManager.md). > - Some APIs are changed as system APIs and can be used only by system applications. Use [@ohos.filemanagement.userFileManager](js-apis-userFileManager.md) instead.
> - The functionalities for selecting and storing media assets are still open to common applications. To use these functionalities, call [@ohos.file.picker](js-apis-file-picker.md). > - The APIs for selecting and storing media assets are still open to common applications. Use [@ohos.file.picker](js-apis-file-picker.md) instead.
## Modules to Import ## Modules to Import
```js ```js
import mediaLibrary from '@ohos.multimedia.mediaLibrary'; import mediaLibrary from '@ohos.multimedia.mediaLibrary';
``` ```
...@@ -81,6 +82,9 @@ getFileAssets(options: MediaFetchOptions, callback: AsyncCallback<FetchFileRe ...@@ -81,6 +82,9 @@ getFileAssets(options: MediaFetchOptions, callback: AsyncCallback<FetchFileRe
Obtains file assets (also called files). This API uses an asynchronous callback to return the result. Obtains file assets (also called files). This API uses an asynchronous callback to return the result.
> **NOTE**
> From API version 10, the albums represented by physical directories are replaced by logical albums, which allows multiple files in an album and presence of a file in multiple albums. This design, however, makes **parent**, **albumId**, **albumUri**, and **albumName** incompatible. They cannot be used as parameters of **MediaFetchOptions** in **getFileAssets()**. For details, see [changelogs-mediaLibrary.md](../../../release-notes/changelogs/OpenHarmony_4.0.8.2/changelogs-mediaLibrary.md).
**Required permissions**: ohos.permission.READ_MEDIA **Required permissions**: ohos.permission.READ_MEDIA
**System capability**: SystemCapability.Multimedia.MediaLibrary.Core **System capability**: SystemCapability.Multimedia.MediaLibrary.Core
...@@ -90,56 +94,56 @@ Obtains file assets (also called files). This API uses an asynchronous callback ...@@ -90,56 +94,56 @@ Obtains file assets (also called files). This API uses an asynchronous callback
| Name | Type | Mandatory| Description | | Name | Type | Mandatory| Description |
| -------- | --------------------------------------------------- | ---- | --------------------------------- | | -------- | --------------------------------------------------- | ---- | --------------------------------- |
| options | [MediaFetchOptions](#mediafetchoptions7) | Yes | Options for fetching the files. | | options | [MediaFetchOptions](#mediafetchoptions7) | Yes | Options for fetching the files. |
| callback | AsyncCallback<[FetchFileResult](#fetchfileresult7)> | Yes | Asynchronous callback of **FetchFileResult**.| | callback | AsyncCallback<[FetchFileResult](#fetchfileresult7)> | Yes | Callback invoked to return the file retrieval result set.|
**Example** **Example**
```js ```js
async function example() { async function example() {
let fileKeyObj = mediaLibrary.FileKey; let fileKeyObj = mediaLibrary.FileKey;
let imageType = mediaLibrary.MediaType.IMAGE; let imageType = mediaLibrary.MediaType.IMAGE;
// Create options for fetching the files. The options are used to obtain files of the image type. // Create options for fetching the files of the image type.
let imagesFetchOp = { let imagesFetchOp = {
selections: fileKeyObj.MEDIA_TYPE + '= ?', selections: fileKeyObj.MEDIA_TYPE + '= ?',
selectionArgs: [imageType.toString()], selectionArgs: [imageType.toString()],
}; };
// Obtain the files in asynchronous callback mode. // Obtain the files in asynchronous callback mode.
media.getFileAssets(imagesFetchOp, async (error, fetchFileResult) => { media.getFileAssets(imagesFetchOp, async (error, fetchFileResult) => {
// Check whether the result set of the obtained files is undefined. If yes, the API call fails. // Check whether the result set of the obtained files is undefined. If yes, the API call fails.
if (fetchFileResult == undefined) { if (fetchFileResult == undefined) {
console.error('get fetchFileResult failed with error: ' + error); console.error('get fetchFileResult failed with error: ' + error);
return; return;
} }
// Obtain the total number of files in the result set. // Obtain the total number of files in the result set.
const count = fetchFileResult.getCount(); const count = fetchFileResult.getCount();
// Check whether the number is less than 0. If yes, the API call fails. // Check whether the number is less than 0. If yes, the API call fails.
if (count < 0) { if (count < 0) {
console.error('get count from fetchFileResult failed, count: ' + count); console.error('get count from fetchFileResult failed, count: ' + count);
return; return;
} }
// Check whether the number is 0. If yes, the API call is successful, but the result set is empty. Check whether the options for fetching the files are correctly set and whether the corresponding files exist on the device. // Check whether the number is 0. If yes, the API call is successful, but the result set is empty. Check whether the options for fetching the files are correctly set and whether the corresponding files exist on the device.
if (count == 0) { if (count == 0) {
console.info('The count of fetchFileResult is zero'); console.info('The count of fetchFileResult is zero');
return; return;
} }
console.info('Get fetchFileResult successfully, count: ' + count); console.info('Get fetchFileResult successfully, count: ' + count);
// Obtain the first file in the result set in asynchronous callback mode. If there are a large number of files, use getAllObject instead. // Obtain the first file in the result set in asynchronous callback mode. If there are a large number of files, use getAllObject() instead.
fetchFileResult.getFirstObject(async (error, fileAsset) => { fetchFileResult.getFirstObject(async (error, fileAsset) => {
// Check whether the first file is undefined. If yes, the API call fails. // Check whether the first file is undefined. If yes, the API call fails.
if (fileAsset == undefined) { if (fileAsset == undefined) {
console.error('get first object failed with error: ' + error); console.error('get first object failed with error: ' + error);
return; return;
} }
console.info('fileAsset.displayName ' + '0 : ' + fileAsset.displayName); console.info('fileAsset.displayName ' + '0 : ' + fileAsset.displayName);
// Call getNextObject to obtain the next file until the last one. // Call getNextObject to obtain the next file until the last one.
for (let i = 1; i < count; i++) { for (let i = 1; i < count; i++) {
let fileAsset = await fetchFileResult.getNextObject(); let fileAsset = await fetchFileResult.getNextObject();
console.info('fileAsset.displayName ' + i + ': ' + fileAsset.displayName); console.info('fileAsset.displayName ' + i + ': ' + fileAsset.displayName);
} }
// Release the FetchFileResult instance and invalidate it. Other APIs can no longer be called. // Release the FetchFileResult instance and invalidate it. Other APIs can no longer be called.
fetchFileResult.close(); fetchFileResult.close();
});
}); });
});
} }
``` ```
...@@ -149,6 +153,9 @@ getFileAssets(options: MediaFetchOptions): Promise&lt;FetchFileResult&gt; ...@@ -149,6 +153,9 @@ getFileAssets(options: MediaFetchOptions): Promise&lt;FetchFileResult&gt;
Obtains file assets. This API uses a promise to return the result. Obtains file assets. This API uses a promise to return the result.
> **NOTE**
> From API version 10, the albums represented by physical directories are replaced by logical albums, which allows multiple files in an album and presence of a file in multiple albums. This design, however, makes **parent**, **albumId**, **albumUri**, and **albumName** incompatible. They cannot be used as parameters of **MediaFetchOptions** in **getFileAssets()**. For details, see [changelogs-mediaLibrary.md](../../../release-notes/changelogs/OpenHarmony_4.0.8.2/changelogs-mediaLibrary.md).
**Required permissions**: ohos.permission.READ_MEDIA **Required permissions**: ohos.permission.READ_MEDIA
**System capability**: SystemCapability.Multimedia.MediaLibrary.Core **System capability**: SystemCapability.Multimedia.MediaLibrary.Core
...@@ -163,52 +170,52 @@ Obtains file assets. This API uses a promise to return the result. ...@@ -163,52 +170,52 @@ Obtains file assets. This API uses a promise to return the result.
| Type | Description | | Type | Description |
| ------------------------------------ | -------------- | | ------------------------------------ | -------------- |
| [FetchFileResult](#fetchfileresult7) | Result set of the file retrieval operation.| | Promise&lt;[FetchFileResult](#fetchfileresult7)&gt; | Promise used to return the file retrieval result set.|
**Example** **Example**
```js ```js
async function example() { async function example() {
let fileKeyObj = mediaLibrary.FileKey; let fileKeyObj = mediaLibrary.FileKey;
let imageType = mediaLibrary.MediaType.IMAGE; let imageType = mediaLibrary.MediaType.IMAGE;
// Create options for fetching the files. The options are used to obtain files of the image type. // Create options for fetching the files of the image type.
let imagesFetchOp = { let imagesFetchOp = {
selections: fileKeyObj.MEDIA_TYPE + '= ?', selections: fileKeyObj.MEDIA_TYPE + '= ?',
selectionArgs: [imageType.toString()], selectionArgs: [imageType.toString()],
}; };
// Obtain the files in promise mode. // Obtain the files in promise mode.
media.getFileAssets(imagesFetchOp).then(async (fetchFileResult) => { media.getFileAssets(imagesFetchOp).then(async (fetchFileResult) => {
// Obtain the total number of files in the result set. // Obtain the total number of files in the result set.
const count = fetchFileResult.getCount(); const count = fetchFileResult.getCount();
// Check whether the number is less than 0. If yes, the API call fails. // Check whether the number is less than 0. If yes, the API call fails.
if (count < 0) { if (count < 0) {
console.error('get count from fetchFileResult failed, count: ' + count); console.error('get count from fetchFileResult failed, count: ' + count);
return; return;
} }
// Check whether the number is 0. If yes, the API call is successful, but the result set is empty. Check whether the options for fetching the files are correctly set and whether the corresponding files exist on the device. // Check whether the number is 0. If yes, the API call is successful, but the result set is empty. Check whether the options for fetching the files are correctly set and whether the corresponding files exist on the device.
if (count == 0) { if (count == 0) {
console.info('The count of fetchFileResult is zero'); console.info('The count of fetchFileResult is zero');
return; return;
} }
console.info('Get fetchFileResult successfully, count: ' + count); console.info('Get fetchFileResult successfully, count: ' + count);
// Obtain the first file in the result set in promise mode. If there are a large number of files, use getAllObject instead. // Obtain the first file in the result set in promise mode. If there are a large number of files, use getAllObject instead.
fetchFileResult.getFirstObject().then(async (fileAsset) => { fetchFileResult.getFirstObject().then(async (fileAsset) => {
console.info('fileAsset.displayName ' + '0 : ' + fileAsset.displayName); console.info('fileAsset.displayName ' + '0 : ' + fileAsset.displayName);
// Call getNextObject to obtain the next file until the last one. // Call getNextObject to obtain the next file until the last one.
for (let i = 1; i < count; i++) { for (let i = 1; i < count; i++) {
let fileAsset = await fetchFileResult.getNextObject(); let fileAsset = await fetchFileResult.getNextObject();
console.info('fileAsset.displayName ' + i + ': ' + fileAsset.displayName); console.info('fileAsset.displayName ' + i + ': ' + fileAsset.displayName);
} }
// Release the FetchFileResult instance and invalidate it. Other APIs can no longer be called. // Release the FetchFileResult instance and invalidate it. Other APIs can no longer be called.
fetchFileResult.close(); fetchFileResult.close();
}).catch((error) => {
// Calling getFirstObject fails.
console.error('get first object failed with error: ' + error);
});
}).catch((error) => { }).catch((error) => {
// Calling getFileAssets fails. // Calling getFirstObject fails.
console.error('get file assets failed with error: ' + error); console.error('get first object failed with error: ' + error);
}); });
}).catch((error) => {
// Calling getFileAssets fails.
console.error('get file assets failed with error: ' + error);
});
} }
``` ```
...@@ -216,7 +223,7 @@ async function example() { ...@@ -216,7 +223,7 @@ async function example() {
on(type: 'deviceChange'&#124;'albumChange'&#124;'imageChange'&#124;'audioChange'&#124;'videoChange'&#124;'fileChange'&#124;'remoteFileChange', callback: Callback&lt;void&gt;): void on(type: 'deviceChange'&#124;'albumChange'&#124;'imageChange'&#124;'audioChange'&#124;'videoChange'&#124;'fileChange'&#124;'remoteFileChange', callback: Callback&lt;void&gt;): void
Subscribes to the media library changes. This API uses an asynchronous callback to return the result. Subscribes to the media library changes. This API uses a callback to return the result.
**System capability**: SystemCapability.Multimedia.MediaLibrary.Core **System capability**: SystemCapability.Multimedia.MediaLibrary.Core
...@@ -224,21 +231,22 @@ Subscribes to the media library changes. This API uses an asynchronous callback ...@@ -224,21 +231,22 @@ Subscribes to the media library changes. This API uses an asynchronous callback
| Name | Type | Mandatory | Description | | Name | Type | Mandatory | Description |
| -------- | -------------------- | ---- | ---------------------------------------- | | -------- | -------------------- | ---- | ---------------------------------------- |
| type | 'deviceChange'&#124;<br>'albumChange'&#124;<br>'imageChange'&#124;<br>'audioChange'&#124;<br>'videoChange'&#124;<br>'fileChange'&#124;<br>'remoteFileChange' | Yes | Media type.<br>'deviceChange': registered device change<br>'albumChange': album change<br>'imageChange': image file change<br>'audioChange': audio file change<br>'videoChange': video file change<br>'fileChange': file change<br>'remoteFileChange': file change on the registered device| | type | 'deviceChange'&#124;<br>'albumChange'&#124;<br>'imageChange'&#124;<br>'audioChange'&#124;<br>'videoChange'&#124;<br>'fileChange'&#124;<br>'remoteFileChange' | Yes | Type of the event to subscribe to.<br>'deviceChange': registered device change<br>'albumChange': album change<br>'imageChange': image file change<br>'audioChange': audio file change<br>'videoChange': video file change<br>'fileChange': file change<br>'remoteFileChange': file change on the registered device|
| callback | Callback&lt;void&gt; | Yes | Void callback. | | callback | Callback&lt;void&gt; | Yes | Callback that returns no value. |
**Example** **Example**
```js ```js
media.on('imageChange', () => { media.on('imageChange', () => {
// image file had changed, do something // image file had changed, do something.
}) });
``` ```
### off<sup>8+</sup> ### off<sup>8+</sup>
off(type: 'deviceChange'&#124;'albumChange'&#124;'imageChange'&#124;'audioChange'&#124;'videoChange'&#124;'fileChange'&#124;'remoteFileChange', callback?: Callback&lt;void&gt;): void off(type: 'deviceChange'&#124;'albumChange'&#124;'imageChange'&#124;'audioChange'&#124;'videoChange'&#124;'fileChange'&#124;'remoteFileChange', callback?: Callback&lt;void&gt;): void
Unsubscribes from the media library changes. This API uses an asynchronous callback to return the result. Unsubscribes from the media library changes. This API uses a callback to return the result.
**System capability**: SystemCapability.Multimedia.MediaLibrary.Core **System capability**: SystemCapability.Multimedia.MediaLibrary.Core
...@@ -246,15 +254,15 @@ Unsubscribes from the media library changes. This API uses an asynchronous callb ...@@ -246,15 +254,15 @@ Unsubscribes from the media library changes. This API uses an asynchronous callb
| Name | Type | Mandatory | Description | | Name | Type | Mandatory | Description |
| -------- | -------------------- | ---- | ---------------------------------------- | | -------- | -------------------- | ---- | ---------------------------------------- |
| type | 'deviceChange'&#124;<br>'albumChange'&#124;<br>'imageChange'&#124;<br>'audioChange'&#124;<br>'videoChange'&#124;<br>'fileChange'&#124;<br>'remoteFileChange' | Yes | Media type.<br>'deviceChange': registered device change<br>'albumChange': album change<br>'imageChange': image file change<br>'audioChange': audio file change<br>'videoChange': video file change<br>'fileChange': file change<br>'remoteFileChange': file change on the registered device| | type | 'deviceChange'&#124;<br>'albumChange'&#124;<br>'imageChange'&#124;<br>'audioChange'&#124;<br>'videoChange'&#124;<br>'fileChange'&#124;<br>'remoteFileChange' | Yes | Type of the event to unsubscribe from.<br>'deviceChange': registered device change<br>'albumChange': album change<br>'imageChange': image file change<br>'audioChange': audio file change<br>'videoChange': video file change<br>'fileChange': file change<br>'remoteFileChange': file change on the registered device|
| callback | Callback&lt;void&gt; | No | Void callback. | | callback | Callback&lt;void&gt; | No | Callback that returns no value. |
**Example** **Example**
```js ```js
media.off('imageChange', () => { media.off('imageChange', () => {
// Stop listening successfully. // stop listening successfully.
}) });
``` ```
### createAsset<sup>8+</sup> ### createAsset<sup>8+</sup>
...@@ -263,6 +271,9 @@ createAsset(mediaType: MediaType, displayName: string, relativePath: string, cal ...@@ -263,6 +271,9 @@ createAsset(mediaType: MediaType, displayName: string, relativePath: string, cal
Creates a media asset. This API uses an asynchronous callback to return the result. Creates a media asset. This API uses an asynchronous callback to return the result.
> **NOTE**
> From the SDK of API version 10, **relativePath** is no longer associated with an album. After a file is created, the last-level directory of **relativePath** is not displayed. For details, see [changelogs-mediaLibrary.md](../../../release-notes/changelogs/OpenHarmony_4.0.8.2/changelogs-mediaLibrary.md).
**Required permissions**: ohos.permission.READ_MEDIA and ohos.permission.WRITE_MEDIA **Required permissions**: ohos.permission.READ_MEDIA and ohos.permission.WRITE_MEDIA
**System capability**: SystemCapability.Multimedia.MediaLibrary.Core **System capability**: SystemCapability.Multimedia.MediaLibrary.Core
...@@ -272,25 +283,25 @@ Creates a media asset. This API uses an asynchronous callback to return the resu ...@@ -272,25 +283,25 @@ Creates a media asset. This API uses an asynchronous callback to return the resu
| Name | Type | Mandatory| Description | | Name | Type | Mandatory| Description |
| ------------ | --------------------------------------- | ---- | ------------------------------------------------------------ | | ------------ | --------------------------------------- | ---- | ------------------------------------------------------------ |
| mediaType | [MediaType](#mediatype8) | Yes | Media type. | | mediaType | [MediaType](#mediatype8) | Yes | Media type. |
| displayName | string | Yes | Display file name. | | displayName | string | Yes | File name to display. |
| relativePath | string | Yes | Path for storing the file. You can use [getPublicDirectory](#getpublicdirectory8) to obtain the paths for storing different types of files.| | relativePath | string | Yes | Path of the file, which can be obtained by using [getPublicDirectory](#getpublicdirectory8).|
| callback | AsyncCallback<[FileAsset](#fileasset7)> | Yes | Asynchronous callback for **FileAsset**. | | callback | AsyncCallback<[FileAsset](#fileasset7)> | Yes | Callback invoked to return the **FileAsset** instance created. |
**Example** **Example**
```js ```js
async function example() { async function example() {
// Create an image file in callback mode. // Create an image file in callback mode.
let mediaType = mediaLibrary.MediaType.IMAGE; let mediaType = mediaLibrary.MediaType.IMAGE;
let DIR_IMAGE = mediaLibrary.DirectoryType.DIR_IMAGE; let DIR_IMAGE = mediaLibrary.DirectoryType.DIR_IMAGE;
const path = await media.getPublicDirectory(DIR_IMAGE); const path = await media.getPublicDirectory(DIR_IMAGE);
media.createAsset(mediaType, 'imageCallBack.jpg', path + 'myPicture/', (error, fileAsset) => { media.createAsset(mediaType, 'imageCallBack.jpg', path + 'myPicture/', (error, fileAsset) => {
if (fileAsset != undefined) { if (fileAsset != undefined) {
console.info('createAsset successfully, message'); console.info('createAsset successfully, message');
} else { } else {
console.error('createAsset failed with error: ' + error); console.error('createAsset failed with error: ' + error);
} }
}); });
} }
``` ```
...@@ -300,6 +311,9 @@ createAsset(mediaType: MediaType, displayName: string, relativePath: string): Pr ...@@ -300,6 +311,9 @@ createAsset(mediaType: MediaType, displayName: string, relativePath: string): Pr
Creates a media asset. This API uses a promise to return the result. Creates a media asset. This API uses a promise to return the result.
> **NOTE**
> From the SDK of API version 10, **relativePath** is no longer associated with an album. After a file is created, the last-level directory of **relativePath** is not displayed. For details, see [changelogs-mediaLibrary.md](../../../release-notes/changelogs/OpenHarmony_4.0.8.2/changelogs-mediaLibrary.md).
**Required permissions**: ohos.permission.READ_MEDIA and ohos.permission.WRITE_MEDIA **Required permissions**: ohos.permission.READ_MEDIA and ohos.permission.WRITE_MEDIA
**System capability**: SystemCapability.Multimedia.MediaLibrary.Core **System capability**: SystemCapability.Multimedia.MediaLibrary.Core
...@@ -309,28 +323,28 @@ Creates a media asset. This API uses a promise to return the result. ...@@ -309,28 +323,28 @@ Creates a media asset. This API uses a promise to return the result.
| Name | Type | Mandatory| Description | | Name | Type | Mandatory| Description |
| ------------ | ------------------------ | ---- | ------------------------------------------------------------ | | ------------ | ------------------------ | ---- | ------------------------------------------------------------ |
| mediaType | [MediaType](#mediatype8) | Yes | Media type. | | mediaType | [MediaType](#mediatype8) | Yes | Media type. |
| displayName | string | Yes | Display file name. | | displayName | string | Yes | File name to display. |
| relativePath | string | Yes | Relative path. You can use [getPublicDirectory](#getpublicdirectory8) to obtain the relative path of the level-1 directory of different types of media files.| | relativePath | string | Yes | Relative path of the file, which can be obtained by [getPublicDirectory](#getpublicdirectory8).|
**Return value** **Return value**
| Type | Description | | Type | Description |
| ------------------------ | ----------------- | | ------------------------ | ----------------- |
| [FileAsset](#fileasset7) | Media data (FileAsset).| | Promise&lt;[FileAsset](#fileasset7)&gt; | Promise used to return the **FileAsset** instance created.|
**Example** **Example**
```js ```js
async function example() { async function example() {
// Create an image file in promise mode. // Create an image file in promise mode.
let mediaType = mediaLibrary.MediaType.IMAGE; let mediaType = mediaLibrary.MediaType.IMAGE;
let DIR_IMAGE = mediaLibrary.DirectoryType.DIR_IMAGE; let DIR_IMAGE = mediaLibrary.DirectoryType.DIR_IMAGE;
const path = await media.getPublicDirectory(DIR_IMAGE); const path = await media.getPublicDirectory(DIR_IMAGE);
media.createAsset(mediaType, 'imagePromise.jpg', path + 'myPicture/').then((fileAsset) => { media.createAsset(mediaType, 'imagePromise.jpg', path + 'myPicture/').then((fileAsset) => {
console.info('createAsset successfully, message = ' + JSON.stringify(fileAsset)); console.info('createAsset successfully, message = ' + JSON.stringify(fileAsset));
}).catch((error) => { }).catch((error) => {
console.error('createAsset failed with error: ' + error); console.error('createAsset failed with error: ' + error);
}); });
} }
``` ```
...@@ -353,6 +367,7 @@ Deletes a file asset. This API uses a promise to return the result. ...@@ -353,6 +367,7 @@ Deletes a file asset. This API uses a promise to return the result.
| uri | string | Yes | URI of the file asset to delete.| | uri | string | Yes | URI of the file asset to delete.|
**Return value** **Return value**
| Type | Description | | Type | Description |
| ------------------- | -------------------- | | ------------------- | -------------------- |
| Promise&lt;void&gt; | Promise used to return the result.| | Promise&lt;void&gt; | Promise used to return the result.|
...@@ -361,28 +376,29 @@ Deletes a file asset. This API uses a promise to return the result. ...@@ -361,28 +376,29 @@ Deletes a file asset. This API uses a promise to return the result.
```js ```js
async function example() { async function example() {
let fileKeyObj = mediaLibrary.FileKey; let fileKeyObj = mediaLibrary.FileKey;
let fileType = mediaLibrary.MediaType.FILE; let fileType = mediaLibrary.MediaType.FILE;
let option = { let option = {
selections: fileKeyObj.MEDIA_TYPE + '= ?', selections: fileKeyObj.MEDIA_TYPE + '= ?',
selectionArgs: [fileType.toString()], selectionArgs: [fileType.toString()],
}; };
const fetchFileResult = await media.getFileAssets(option); const fetchFileResult = await media.getFileAssets(option);
let asset = await fetchFileResult.getFirstObject(); let asset = await fetchFileResult.getFirstObject();
if (asset == undefined) { if (asset == undefined) {
console.error('asset not exist'); console.error('asset not exist');
return; return;
} }
media.deleteAsset(asset.uri).then(() => { media.deleteAsset(asset.uri).then(() => {
console.info('deleteAsset successfully'); console.info('deleteAsset successfully');
}).catch((error) => { }).catch((error) => {
console.error('deleteAsset failed with error: ' + error); console.error('deleteAsset failed with error: ' + error);
}); });
fetchFileResult.close(); fetchFileResult.close();
} }
``` ```
### deleteAsset<sup>8+</sup> ### deleteAsset<sup>8+</sup>
deleteAsset(uri: string, callback: AsyncCallback\<void>): void deleteAsset(uri: string, callback: AsyncCallback\<void>): void
Deletes a file asset. This API uses an asynchronous callback to return the result. Deletes a file asset. This API uses an asynchronous callback to return the result.
...@@ -398,32 +414,32 @@ Deletes a file asset. This API uses an asynchronous callback to return the resul ...@@ -398,32 +414,32 @@ Deletes a file asset. This API uses an asynchronous callback to return the resul
| Name | Type | Mandatory | Description | | Name | Type | Mandatory | Description |
| -------- | ---------------------------- | ---- | --------------- | | -------- | ---------------------------- | ---- | --------------- |
| uri | string | Yes | URI of the file asset to delete.| | uri | string | Yes | URI of the file asset to delete.|
|callback |AsyncCallback\<void>| Yes |Callback used to return the result.| |callback |AsyncCallback\<void>| Yes |Callback that returns no value.|
**Example** **Example**
```js ```js
async function example() { async function example() {
let fileKeyObj = mediaLibrary.FileKey; let fileKeyObj = mediaLibrary.FileKey;
let fileType = mediaLibrary.MediaType.FILE; let fileType = mediaLibrary.MediaType.FILE;
let option = { let option = {
selections: fileKeyObj.MEDIA_TYPE + '= ?', selections: fileKeyObj.MEDIA_TYPE + '= ?',
selectionArgs: [fileType.toString()], selectionArgs: [fileType.toString()],
}; };
const fetchFileResult = await media.getFileAssets(option); const fetchFileResult = await media.getFileAssets(option);
let asset = await fetchFileResult.getFirstObject(); let asset = await fetchFileResult.getFirstObject();
if (asset == undefined) { if (asset == undefined) {
console.error('asset not exist'); console.error('asset not exist');
return; return;
}
media.deleteAsset(asset.uri, (error) => {
if (error != undefined) {
console.error('deleteAsset failed with error: ' + error);
} else {
console.info('deleteAsset successfully');
} }
media.deleteAsset(asset.uri, (error) => { });
if (error != undefined) { fetchFileResult.close();
console.error('deleteAsset failed with error: ' + error);
} else {
console.info('deleteAsset successfully');
}
});
fetchFileResult.close();
} }
``` ```
...@@ -431,7 +447,7 @@ async function example() { ...@@ -431,7 +447,7 @@ async function example() {
getPublicDirectory(type: DirectoryType, callback: AsyncCallback&lt;string&gt;): void getPublicDirectory(type: DirectoryType, callback: AsyncCallback&lt;string&gt;): void
Obtains a public directory. This API uses an asynchronous callback to return the result. Obtains a user directory. This API uses an asynchronous callback to return the result.
**System capability**: SystemCapability.Multimedia.MediaLibrary.Core **System capability**: SystemCapability.Multimedia.MediaLibrary.Core
...@@ -439,19 +455,19 @@ Obtains a public directory. This API uses an asynchronous callback to return the ...@@ -439,19 +455,19 @@ Obtains a public directory. This API uses an asynchronous callback to return the
| Name | Type | Mandatory| Description | | Name | Type | Mandatory| Description |
| -------- | -------------------------------- | ---- | ------------------------- | | -------- | -------------------------------- | ---- | ------------------------- |
| type | [DirectoryType](#directorytype8) | Yes | Type of the public directory. | | type | [DirectoryType](#directorytype8) | Yes | Type of the user directory. |
| callback | AsyncCallback&lt;string&gt; | Yes | Callback used to return the public directory.| | callback | AsyncCallback&lt;string&gt; | Yes | Callback invoked to return the user directory obtained.|
**Example** **Example**
```js ```js
let DIR_CAMERA = mediaLibrary.DirectoryType.DIR_CAMERA; let DIR_CAMERA = mediaLibrary.DirectoryType.DIR_CAMERA;
media.getPublicDirectory(DIR_CAMERA, (error, dicResult) => { media.getPublicDirectory(DIR_CAMERA, (error, dicResult) => {
if (dicResult == 'Camera/') { if (dicResult == 'Camera/') {
console.info('getPublicDirectory DIR_CAMERA successfully'); console.info('getPublicDirectory DIR_CAMERA successfully');
} else { } else {
console.error('getPublicDirectory DIR_CAMERA failed with error: ' + error); console.error('getPublicDirectory DIR_CAMERA failed with error: ' + error);
} }
}); });
``` ```
...@@ -459,7 +475,7 @@ media.getPublicDirectory(DIR_CAMERA, (error, dicResult) => { ...@@ -459,7 +475,7 @@ media.getPublicDirectory(DIR_CAMERA, (error, dicResult) => {
getPublicDirectory(type: DirectoryType): Promise&lt;string&gt; getPublicDirectory(type: DirectoryType): Promise&lt;string&gt;
Obtains a public directory. This API uses a promise to return the result. Obtains a user directory. This API uses a promise to return the result.
**System capability**: SystemCapability.Multimedia.MediaLibrary.Core **System capability**: SystemCapability.Multimedia.MediaLibrary.Core
...@@ -467,28 +483,28 @@ Obtains a public directory. This API uses a promise to return the result. ...@@ -467,28 +483,28 @@ Obtains a public directory. This API uses a promise to return the result.
| Name| Type | Mandatory| Description | | Name| Type | Mandatory| Description |
| ------ | -------------------------------- | ---- | ------------ | | ------ | -------------------------------- | ---- | ------------ |
| type | [DirectoryType](#directorytype8) | Yes | Type of the public directory.| | type | [DirectoryType](#directorytype8) | Yes | Type of the user directory.|
**Return value** **Return value**
| Type | Description | | Type | Description |
| ---------------- | ---------------- | | ---------------- | ---------------- |
| Promise\<string> | Promise used to return the public directory.| | Promise\<string> | Promise used to return the user directory obtained.|
**Example** **Example**
```js ```js
async function example() { async function example() {
let DIR_CAMERA = mediaLibrary.DirectoryType.DIR_CAMERA; let DIR_CAMERA = mediaLibrary.DirectoryType.DIR_CAMERA;
media.getPublicDirectory(DIR_CAMERA).then((dicResult) => { media.getPublicDirectory(DIR_CAMERA).then((dicResult) => {
if (dicResult == 'Camera/') { if (dicResult == 'Camera/') {
console.info('getPublicDirectory DIR_CAMERA successfully'); console.info('getPublicDirectory DIR_CAMERA successfully');
} else { } else {
console.error('getPublicDirectory DIR_CAMERA failed'); console.error('getPublicDirectory DIR_CAMERA failed');
} }
}).catch((error) => { }).catch((error) => {
console.error('getPublicDirectory failed with error: ' + error); console.error('getPublicDirectory failed with error: ' + error);
}); });
} }
``` ```
...@@ -496,7 +512,10 @@ async function example() { ...@@ -496,7 +512,10 @@ async function example() {
getAlbums(options: MediaFetchOptions, callback: AsyncCallback&lt;Array&lt;Album&gt;&gt;): void getAlbums(options: MediaFetchOptions, callback: AsyncCallback&lt;Array&lt;Album&gt;&gt;): void
Obtains the albums. This API uses an asynchronous callback to return the result. Obtains albums. This API uses an asynchronous callback to return the result.
> **NOTE**
> From the SDK of API version 10, **relativePath** is no longer associated with an album. Therefore, **relativePath** cannot be used as a search criterion in **getAlbums**. Currently, only **Camera** and **ScreenShots** albums are supported. For more details, see [changelogs-mediaLibrary.md](../../../release-notes/changelogs/OpenHarmony_4.0.8.2/changelogs-mediaLibrary.md).
**Required permissions**: ohos.permission.READ_MEDIA **Required permissions**: ohos.permission.READ_MEDIA
...@@ -506,17 +525,16 @@ Obtains the albums. This API uses an asynchronous callback to return the result. ...@@ -506,17 +525,16 @@ Obtains the albums. This API uses an asynchronous callback to return the result.
| Name | Type | Mandatory| Description | | Name | Type | Mandatory| Description |
| -------- | -------------------------------------------- | ---- | --------------------------- | | -------- | -------------------------------------------- | ---- | --------------------------- |
| options | [MediaFetchOptions](#mediafetchoptions7) | Yes | Options for fetching the albums. | | options | [MediaFetchOptions](#mediafetchoptions7) | Yes | Options for fetching albums. |
| callback | AsyncCallback&lt;Array<[Album](#album7)>&gt; | Yes | Callback used to return the albums.| | callback | AsyncCallback&lt;Array<[Album](#album7)>&gt; | Yes | Callback invoked to return the albums.|
**Example** **Example**
```js ```js
async function example() { async function example() {
// To obtain the file assets in an album, you must preset the album and resources. The sample code below presets 'New Album 1'.
let AlbumNoArgsfetchOp = { let AlbumNoArgsfetchOp = {
selections: mediaLibrary.FileKey.ALBUM_NAME + '= ?', selections: mediaLibrary.FileKey.ALBUM_NAME + '= ?',
selectionArgs:['New Album 1'], selectionArgs: ['Camera'],
}; };
media.getAlbums(AlbumNoArgsfetchOp, (error, albumList) => { media.getAlbums(AlbumNoArgsfetchOp, (error, albumList) => {
if (albumList != undefined) { if (albumList != undefined) {
...@@ -524,7 +542,7 @@ async function example() { ...@@ -524,7 +542,7 @@ async function example() {
} else { } else {
console.error('getAlbums failed with error: ' + error); console.error('getAlbums failed with error: ' + error);
} }
}) });
} }
``` ```
...@@ -532,7 +550,10 @@ async function example() { ...@@ -532,7 +550,10 @@ async function example() {
getAlbums(options: MediaFetchOptions): Promise&lt;Array&lt;Album&gt;&gt; getAlbums(options: MediaFetchOptions): Promise&lt;Array&lt;Album&gt;&gt;
Obtains the albums. This API uses a promise to return the result. Obtains albums. This API uses a promise to return the result.
> **NOTE**
> From the SDK of API version 10, **relativePath** is no longer associated with an album. Therefore, **relativePath** cannot be used as a search criterion in **getAlbums**. Currently, only **Camera** and **ScreenShots** albums are supported. For more details, see [changelogs-mediaLibrary.md](../../../release-notes/changelogs/OpenHarmony_4.0.8.2/changelogs-mediaLibrary.md).
**Required permissions**: ohos.permission.READ_MEDIA **Required permissions**: ohos.permission.READ_MEDIA
...@@ -554,10 +575,9 @@ Obtains the albums. This API uses a promise to return the result. ...@@ -554,10 +575,9 @@ Obtains the albums. This API uses a promise to return the result.
```js ```js
async function example() { async function example() {
// To obtain the file assets in an album, you must preset the album and resources. The sample code below presets 'New Album 1'.
let AlbumNoArgsfetchOp = { let AlbumNoArgsfetchOp = {
selections: mediaLibrary.FileKey.ALBUM_NAME + '= ?', selections: mediaLibrary.FileKey.ALBUM_NAME + '= ?',
selectionArgs:['New Album 1'], selectionArgs: ['Camera'],
}; };
media.getAlbums(AlbumNoArgsfetchOp).then((albumList) => { media.getAlbums(AlbumNoArgsfetchOp).then((albumList) => {
console.info('getAlbums successfully: ' + JSON.stringify(albumList)); console.info('getAlbums successfully: ' + JSON.stringify(albumList));
...@@ -586,7 +606,7 @@ Call this API when you no longer need to use the APIs in the **MediaLibrary** in ...@@ -586,7 +606,7 @@ Call this API when you no longer need to use the APIs in the **MediaLibrary** in
```js ```js
media.release(() => { media.release(() => {
// do something // do something.
}); });
``` ```
...@@ -603,23 +623,21 @@ Call this API when you no longer need to use the APIs in the **MediaLibrary** in ...@@ -603,23 +623,21 @@ Call this API when you no longer need to use the APIs in the **MediaLibrary** in
| Type | Description | | Type | Description |
| ------------------- | -------------------- | | ------------------- | -------------------- |
| Promise&lt;void&gt; | Promise used to return the execution result.| | Promise&lt;void&gt; | Promise that returns no value.|
**Example** **Example**
```js ```js
media.release() media.release();
``` ```
### storeMediaAsset ### storeMediaAsset
storeMediaAsset(option: MediaAssetOption, callback: AsyncCallback&lt;string&gt;): void storeMediaAsset(option: MediaAssetOption, callback: AsyncCallback&lt;string&gt;): void
Stores a media asset. This API uses an asynchronous callback to return the URI that stores the media asset. Stores a media asset. This API uses an asynchronous callback to return the URI of the media asset.
> **NOTE** > **NOTE**<br>This API is supported since API version 6 and can be used only in the FA model.
>
> This API is supported since API version 6 and can be used only by the FA model.
**System capability**: SystemCapability.Multimedia.MediaLibrary.Core **System capability**: SystemCapability.Multimedia.MediaLibrary.Core
...@@ -628,36 +646,33 @@ Stores a media asset. This API uses an asynchronous callback to return the URI t ...@@ -628,36 +646,33 @@ Stores a media asset. This API uses an asynchronous callback to return the URI t
| Name | Type | Mandatory | Description | | Name | Type | Mandatory | Description |
| -------- | ------------------------------------- | ---- | ----------------------- | | -------- | ------------------------------------- | ---- | ----------------------- |
| option | [MediaAssetOption](#mediaassetoption) | Yes | Media asset option. | | option | [MediaAssetOption](#mediaassetoption) | Yes | Media asset option. |
| callback | AsyncCallback&lt;string&gt; | Yes | Callback used to return the URI that stores the media asset.| | callback | AsyncCallback&lt;string&gt; | Yes | Callback invoked to return the URI of the media resource saved.|
**Example** **Example**
```js ```js
let option = { let option = {
src : '/data/storage/el2/base/haps/entry/image.png', src : '/data/storage/el2/base/haps/entry/image.png',
mimeType : 'image/*', mimeType : 'image/*',
relativePath : 'Pictures/' relativePath : 'Pictures/'
}; };
mediaLibrary.getMediaLibrary().storeMediaAsset(option, (error, value) => { mediaLibrary.getMediaLibrary().storeMediaAsset(option, (error, value) => {
if (error) { if (error) {
console.error('storeMediaAsset failed with error: ' + error); console.error('storeMediaAsset failed with error: ' + error);
return; return;
} }
console.info('Media resources stored. '); console.info('Media resources stored. ');
// Obtain the URI that stores the media asset. // Obtain the URI of the media asset.
}); });
``` ```
### storeMediaAsset ### storeMediaAsset
storeMediaAsset(option: MediaAssetOption): Promise&lt;string&gt; storeMediaAsset(option: MediaAssetOption): Promise&lt;string&gt;
Stores a media asset. This API uses a promise to return the URI that stores the media asset. Stores a media asset. This API uses a promise to return the URI of the media asset.
> **NOTE** > **NOTE**<br>This API is supported since API version 6 and can be used only in the FA model.
>
> This API is supported since API version 6 and can be used only by the FA model.
**System capability**: SystemCapability.Multimedia.MediaLibrary.Core **System capability**: SystemCapability.Multimedia.MediaLibrary.Core
...@@ -671,34 +686,33 @@ Stores a media asset. This API uses a promise to return the URI that stores the ...@@ -671,34 +686,33 @@ Stores a media asset. This API uses a promise to return the URI that stores the
| Type | Description | | Type | Description |
| --------------------- | ---------------------------- | | --------------------- | ---------------------------- |
| Promise&lt;string&gt; | Promise used to return the URI that stores the media asset.| | Promise&lt;string&gt; | Promise used to return the URI of the media asset saved.|
**Example** **Example**
```js ```js
let option = { let option = {
src : '/data/storage/el2/base/haps/entry/image.png', src : '/data/storage/el2/base/haps/entry/image.png',
mimeType : 'image/*', mimeType : 'image/*',
relativePath : 'Pictures/' relativePath : 'Pictures/'
}; };
mediaLibrary.getMediaLibrary().storeMediaAsset(option).then((value) => { mediaLibrary.getMediaLibrary().storeMediaAsset(option).then((value) => {
console.info('Media resources stored.'); console.info('Media resources stored.');
// Obtain the URI that stores the media asset. // Obtain the URI of the media asset.
}).catch((error) => { }).catch((error) => {
console.error('storeMediaAsset failed with error: ' + error); console.error('storeMediaAsset failed with error: ' + error);
}); });
``` ```
### startImagePreview ### startImagePreview
startImagePreview(images: Array&lt;string&gt;, index: number, callback: AsyncCallback&lt;void&gt;): void startImagePreview(images: Array&lt;string&gt;, index: number, callback: AsyncCallback&lt;void&gt;): void
Starts image preview, with the first image to preview specified. This API can be used to preview local images whose URIs start with **datashare://** or online images whose URIs start with **https://**. It uses an asynchronous callback to return the execution result. Starts image preview, with the first image to preview specified. This API can be used to preview local images whose URIs start with **datashare://** or online images whose URIs start with **https://**. It uses an asynchronous callback to return the result.
> **NOTE** > **NOTE**
> This API is supported since API version 6 and can be used only by the FA model. > This API is supported since API version 6 and can be used only in the FA model.
> You are advised to use the **\<[Image](../arkui-ts/ts-basic-components-image.md)>** component instead. The **\<Image>** component can be used to render and display local and online images. > You are advised to use the **\<[Image](../arkui-ts/ts-basic-components-image.md)>** component instead.<br>The **\<Image>** component can be used to render and display local and online images.
**System capability**: SystemCapability.Multimedia.MediaLibrary.Core **System capability**: SystemCapability.Multimedia.MediaLibrary.Core
...@@ -708,41 +722,40 @@ Starts image preview, with the first image to preview specified. This API can be ...@@ -708,41 +722,40 @@ Starts image preview, with the first image to preview specified. This API can be
| -------- | ------------------------- | ---- | ---------------------------------------- | | -------- | ------------------------- | ---- | ---------------------------------------- |
| images | Array&lt;string&gt; | Yes | URIs of the images to preview. The value can start with either **'https://'** or **'datashare://'**.| | images | Array&lt;string&gt; | Yes | URIs of the images to preview. The value can start with either **'https://'** or **'datashare://'**.|
| index | number | Yes | Index of the first image to preview. | | index | number | Yes | Index of the first image to preview. |
| callback | AsyncCallback&lt;void&gt; | Yes | Callback used to return the image preview result. If the preview fails, an error message is returned. | | callback | AsyncCallback&lt;void&gt; | Yes | Callback that returns no value. |
**Example** **Example**
```js ```js
let images = [ let images = [
'file://media/xxxx/2', 'file://media/xxxx/2',
'file://media/xxxx/3' 'file://media/xxxx/3'
]; ];
/* Preview online images. /* Preview online images.
let images = [ let images = [
'https://media.xxxx.com/image1.jpg', 'https://media.xxxx.com/image1.jpg',
'https://media.xxxx.com/image2.jpg' 'https://media.xxxx.com/image2.jpg'
]; ];
*/ */
let index = 1; let index = 1;
mediaLibrary.getMediaLibrary().startImagePreview(images, index, (error) => { mediaLibrary.getMediaLibrary().startImagePreview(images, index, (error) => {
if (error) { if (error) {
console.error('startImagePreview failed with error: ' + error); console.error('startImagePreview failed with error: ' + error);
return; return;
} }
console.info('Succeeded in previewing the images.'); console.info('Succeeded in previewing the images.');
}); });
``` ```
### startImagePreview ### startImagePreview
startImagePreview(images: Array&lt;string&gt;, callback: AsyncCallback&lt;void&gt;): void startImagePreview(images: Array&lt;string&gt;, callback: AsyncCallback&lt;void&gt;): void
Starts image preview. This API can be used to preview local images whose URIs start with **datashare://** or online images whose URIs start with **https://**. It uses an asynchronous callback to return the execution result. Starts image preview. This API can be used to preview local images whose URIs start with **datashare://** or online images whose URIs start with **https://**. It uses an asynchronous callback to return the result.
> **NOTE** > **NOTE**
> This API is supported since API version 6 and can be used only by the FA model. > This API is supported since API version 6 and can be used only in the FA model.
> You are advised to use the **\<[Image](../arkui-ts/ts-basic-components-image.md)>** component instead. The **\<Image>** component can be used to render and display local and online images. > You are advised to use the **\<[Image](../arkui-ts/ts-basic-components-image.md)>** component instead.<br>The **\<Image>** component can be used to render and display local and online images.
**System capability**: SystemCapability.Multimedia.MediaLibrary.Core **System capability**: SystemCapability.Multimedia.MediaLibrary.Core
...@@ -751,31 +764,30 @@ Starts image preview. This API can be used to preview local images whose URIs st ...@@ -751,31 +764,30 @@ Starts image preview. This API can be used to preview local images whose URIs st
| Name | Type | Mandatory | Description | | Name | Type | Mandatory | Description |
| -------- | ------------------------- | ---- | ---------------------------------------- | | -------- | ------------------------- | ---- | ---------------------------------------- |
| images | Array&lt;string&gt; | Yes | URIs of the images to preview. The value can start with either **'https://'** or **'datashare://'**.| | images | Array&lt;string&gt; | Yes | URIs of the images to preview. The value can start with either **'https://'** or **'datashare://'**.|
| callback | AsyncCallback&lt;void&gt; | Yes | Callback used to return the image preview result. If the preview fails, an error message is returned. | | callback | AsyncCallback&lt;void&gt; | Yes | Callback that returns no value. |
**Example** **Example**
```js ```js
let images = [ let images = [
'file://media/xxxx/2', 'file://media/xxxx/2',
'file://media/xxxx/3' 'file://media/xxxx/3'
]; ];
/* Preview online images. /* Preview online images.
let images = [ let images = [
'https://media.xxxx.com/image1.jpg', 'https://media.xxxx.com/image1.jpg',
'https://media.xxxx.com/image2.jpg' 'https://media.xxxx.com/image2.jpg'
]; ];
*/ */
mediaLibrary.getMediaLibrary().startImagePreview(images, (error) => { mediaLibrary.getMediaLibrary().startImagePreview(images, (error) => {
if (error) { if (error) {
console.error('startImagePreview failed with error: ' + error); console.error('startImagePreview failed with error: ' + error);
return; return;
} }
console.info('Succeeded in previewing the images.'); console.info('Succeeded in previewing the images.');
}); });
``` ```
### startImagePreview ### startImagePreview
startImagePreview(images: Array&lt;string&gt;, index?: number): Promise&lt;void&gt; startImagePreview(images: Array&lt;string&gt;, index?: number): Promise&lt;void&gt;
...@@ -783,8 +795,8 @@ startImagePreview(images: Array&lt;string&gt;, index?: number): Promise&lt;void& ...@@ -783,8 +795,8 @@ startImagePreview(images: Array&lt;string&gt;, index?: number): Promise&lt;void&
Starts image preview, with the first image to preview specified. This API can be used to preview local images whose URIs start with **datashare://** or online images whose URIs start with **https://**. It uses a promise to return the execution result. Starts image preview, with the first image to preview specified. This API can be used to preview local images whose URIs start with **datashare://** or online images whose URIs start with **https://**. It uses a promise to return the execution result.
> **NOTE** > **NOTE**
> This API is supported since API version 6 and can be used only by the FA model. > This API is supported since API version 6 and can be used only in the FA model.
> You are advised to use the **\<[Image](../arkui-ts/ts-basic-components-image.md)>** component instead. The **\<Image>** component can be used to render and display local and online images. > You are advised to use the **\<[Image](../arkui-ts/ts-basic-components-image.md)>** component instead.<br>The **\<Image>** component can be used to render and display local and online images.
**System capability**: SystemCapability.Multimedia.MediaLibrary.Core **System capability**: SystemCapability.Multimedia.MediaLibrary.Core
...@@ -799,39 +811,38 @@ Starts image preview, with the first image to preview specified. This API can be ...@@ -799,39 +811,38 @@ Starts image preview, with the first image to preview specified. This API can be
| Type | Description | | Type | Description |
| ------------------- | ------------------------------- | | ------------------- | ------------------------------- |
| Promise&lt;void&gt; | Promise used to return the image preview result. If the preview fails, an error message is returned.| | Promise&lt;void&gt; | Promise that returns no value.|
**Example** **Example**
```js ```js
let images = [ let images = [
'file://media/xxxx/2', 'file://media/xxxx/2',
'file://media/xxxx/3' 'file://media/xxxx/3'
]; ];
/* Preview online images. /* Preview online images.
let images = [ let images = [
'https://media.xxxx.com/image1.jpg', 'https://media.xxxx.com/image1.jpg',
'https://media.xxxx.com/image2.jpg' 'https://media.xxxx.com/image2.jpg'
]; ];
*/ */
let index = 1; let index = 1;
mediaLibrary.getMediaLibrary().startImagePreview(images, index).then(() => { mediaLibrary.getMediaLibrary().startImagePreview(images, index).then(() => {
console.info('Succeeded in previewing the images.'); console.info('Succeeded in previewing the images.');
}).catch((error) => { }).catch((error) => {
console.error('startImagePreview failed with error: ' + error); console.error('startImagePreview failed with error: ' + error);
}); });
``` ```
### startMediaSelect ### startMediaSelect
startMediaSelect(option: MediaSelectOption, callback: AsyncCallback&lt;Array&lt;string&gt;&gt;): void startMediaSelect(option: MediaSelectOption, callback: AsyncCallback&lt;Array&lt;string&gt;&gt;): void
Starts media selection. This API uses an asynchronous callback to return the list of URIs that store the selected media assets. Starts media selection. This API uses an asynchronous callback to return the URIs of the selected media assets.
> **NOTE** > **NOTE**
> This API is supported since API version 6 and can be used only by the FA model. > This API is supported since API version 6 and can be used only in the FA model.
> You are advised to use the system app Gallery instead. Gallery is a built-in visual resource access application that provides features such as image and video management and browsing. For details about how to use Gallery, visit [OpenHarmony/applications_photos](https://gitee.com/openharmony/applications_photos). > You are advised to use the system app Gallery instead. Gallery is a built-in visual resource access application that provides features such as image and video management and browsing. For details about how to use Gallery, see [OpenHarmony/applications_photos](https://gitee.com/openharmony/applications_photos).
**System capability**: SystemCapability.Multimedia.MediaLibrary.Core **System capability**: SystemCapability.Multimedia.MediaLibrary.Core
...@@ -840,35 +851,34 @@ Starts media selection. This API uses an asynchronous callback to return the lis ...@@ -840,35 +851,34 @@ Starts media selection. This API uses an asynchronous callback to return the lis
| Name | Type | Mandatory | Description | | Name | Type | Mandatory | Description |
| -------- | ---------------------------------------- | ---- | ------------------------------------ | | -------- | ---------------------------------------- | ---- | ------------------------------------ |
| option | [MediaSelectOption](#mediaselectoption) | Yes | Media selection option. | | option | [MediaSelectOption](#mediaselectoption) | Yes | Media selection option. |
| callback | AsyncCallback&lt;Array&lt;string&gt;&gt; | Yes | Callback used to return the list of URIs (starting with **datashare://**) that store the selected media assets.| | callback | AsyncCallback&lt;Array&lt;string&gt;&gt; | Yes | Callback invoked to return the URIs of the selected media assets.|
**Example** **Example**
```js ```js
let option : mediaLibrary.MediaSelectOption = { let option : mediaLibrary.MediaSelectOption = {
type : 'media', type : 'media',
count : 2 count : 2
}; };
mediaLibrary.getMediaLibrary().startMediaSelect(option, (error, value) => { mediaLibrary.getMediaLibrary().startMediaSelect(option, (error, value) => {
if (error) { if (error) {
console.error('startMediaSelect failed with error: ' + error); console.error('startMediaSelect failed with error: ' + error);
return; return;
} }
console.info('Media resources selected.'); console.info('Media resources selected.');
// Obtain the media selection value. // Obtain the media selection value.
}); });
``` ```
### startMediaSelect ### startMediaSelect
startMediaSelect(option: MediaSelectOption): Promise&lt;Array&lt;string&gt;&gt; startMediaSelect(option: MediaSelectOption): Promise&lt;Array&lt;string&gt;&gt;
Starts media selection. This API uses a promise to return the list of URIs that store the selected media assets. Starts media selection. This API uses a promise to return the URIs of the selected media assets.
> **NOTE** > **NOTE**
> This API is supported since API version 6 and can be used only by the FA model. > This API is supported since API version 6 and can be used only in the FA model.
> You are advised to use the system app Gallery instead. Gallery is a built-in visual resource access application that provides features such as image and video management and browsing. For details about how to use Gallery, visit [OpenHarmony/applications_photos](https://gitee.com/openharmony/applications_photos). > You are advised to use the system app Gallery instead. Gallery is a built-in visual resource access application that provides features such as image and video management and browsing. For details about how to use Gallery, see [OpenHarmony/applications_photos](https://gitee.com/openharmony/applications_photos).
**System capability**: SystemCapability.Multimedia.MediaLibrary.Core **System capability**: SystemCapability.Multimedia.MediaLibrary.Core
...@@ -882,23 +892,23 @@ Starts media selection. This API uses a promise to return the list of URIs that ...@@ -882,23 +892,23 @@ Starts media selection. This API uses a promise to return the list of URIs that
| Type | Description | | Type | Description |
| ---------------------------------- | ---------------------------------------- | | ---------------------------------- | ---------------------------------------- |
| Promise&lt;Array&lt;string&gt;&gt; | Promise used to return the list of URIs (starting with **datashare://**) that store the selected media assets.| | Promise&lt;Array&lt;string&gt;&gt; | Promise used to return the URIs of the selected media assets.|
**Example** **Example**
```js ```js
let option : mediaLibrary.MediaSelectOption = { let option : mediaLibrary.MediaSelectOption = {
type : 'media', type : 'media',
count : 2 count : 2
}; };
mediaLibrary.getMediaLibrary().startMediaSelect(option).then((value) => { mediaLibrary.getMediaLibrary().startMediaSelect(option).then((value) => {
console.info('Media resources selected.'); console.info('Media resources selected.');
// Obtain the media selection value. // Obtain the media selection value.
}).catch((error) => { }).catch((error) => {
console.error('startMediaSelect failed with error: ' + error); console.error('startMediaSelect failed with error: ' + error);
}); });
``` ```
### getActivePeers<sup>8+</sup> ### getActivePeers<sup>8+</sup>
getActivePeers(): Promise\<Array\<PeerInfo>>; getActivePeers(): Promise\<Array\<PeerInfo>>;
...@@ -921,15 +931,15 @@ Obtains information about online peer devices. This API uses a promise to return ...@@ -921,15 +931,15 @@ Obtains information about online peer devices. This API uses a promise to return
```js ```js
async function example() { async function example() {
media.getActivePeers().then((devicesInfo) => { media.getActivePeers().then((devicesInfo) => {
if (devicesInfo != undefined) { if (devicesInfo != undefined) {
console.info('get distributed info ' + JSON.stringify(devicesInfo)); console.info('get distributed info ' + JSON.stringify(devicesInfo));
} else { } else {
console.info('get distributed info is undefined!'); console.info('get distributed info is undefined!');
} }
}).catch((error) => { }).catch((error) => {
console.error('get distributed info failed with error: ' + error); console.error('get distributed info failed with error: ' + error);
}); });
} }
``` ```
...@@ -949,23 +959,22 @@ Obtains information about online peer devices. This API uses an asynchronous cal ...@@ -949,23 +959,22 @@ Obtains information about online peer devices. This API uses an asynchronous cal
| Type | Description | | Type | Description |
| ------------------- | -------------------- | | ------------------- | -------------------- |
| callback: AsyncCallback\<Array\<[PeerInfo](#peerinfo8)>> | Promise used to return the online peer devices, in an array of **PeerInfo** objects.| | callback: AsyncCallback\<Array\<[PeerInfo](#peerinfo8)>> | Callback invoked to return the online peer devices, in an array of **PeerInfo** objects.|
**Example** **Example**
```js ```js
async function example() { async function example() {
media.getActivePeers((error, devicesInfo) => { media.getActivePeers((error, devicesInfo) => {
if (devicesInfo != undefined) { if (devicesInfo != undefined) {
console.info('get distributed info ' + JSON.stringify(devicesInfo)); console.info('get distributed info ' + JSON.stringify(devicesInfo));
} else { } else {
console.error('get distributed failed with error: ' + error); console.error('get distributed failed with error: ' + error);
} }
}); });
} }
``` ```
### getAllPeers<sup>8+</sup> ### getAllPeers<sup>8+</sup>
getAllPeers(): Promise\<Array\<PeerInfo>>; getAllPeers(): Promise\<Array\<PeerInfo>>;
...@@ -988,15 +997,15 @@ Obtains information about all peer devices. This API uses a promise to return th ...@@ -988,15 +997,15 @@ Obtains information about all peer devices. This API uses a promise to return th
```js ```js
async function example() { async function example() {
media.getAllPeers().then((devicesInfo) => { media.getAllPeers().then((devicesInfo) => {
if (devicesInfo != undefined) { if (devicesInfo != undefined) {
console.info('get distributed info ' + JSON.stringify(devicesInfo)); console.info('get distributed info ' + JSON.stringify(devicesInfo));
} else { } else {
console.info('get distributed info is undefined!'); console.info('get distributed info is undefined!');
} }
}).catch((error) => { }).catch((error) => {
console.error('get distributed info failed with error: ' + error); console.error('get distributed info failed with error: ' + error);
}); });
} }
``` ```
...@@ -1004,7 +1013,7 @@ async function example() { ...@@ -1004,7 +1013,7 @@ async function example() {
getAllPeers(callback: AsyncCallback\<Array\<PeerInfo>>): void; getAllPeers(callback: AsyncCallback\<Array\<PeerInfo>>): void;
Obtains information about online peer devices. This API uses an asynchronous callback to return the result. Obtains information about all peer devices. This API uses an asynchronous callback to return the result.
**System API**: This is a system API. **System API**: This is a system API.
...@@ -1016,19 +1025,19 @@ Obtains information about online peer devices. This API uses an asynchronous cal ...@@ -1016,19 +1025,19 @@ Obtains information about online peer devices. This API uses an asynchronous cal
| Type | Description | | Type | Description |
| ------------------- | -------------------- | | ------------------- | -------------------- |
| callback: AsyncCallback\<Array\<[PeerInfo](#peerinfo8)>> | Promise used to return all peer devices, in an array of **PeerInfo** objects.| | callback: AsyncCallback\<Array\<[PeerInfo](#peerinfo8)>> | Callback invoked to return all peer devices, in an array of **PeerInfo** objects.|
**Example** **Example**
```js ```js
async function example() { async function example() {
media.getAllPeers((error, devicesInfo) => { media.getAllPeers((error, devicesInfo) => {
if (devicesInfo != undefined) { if (devicesInfo != undefined) {
console.info('get distributed info ' + JSON.stringify(devicesInfo)); console.info('get distributed info ' + JSON.stringify(devicesInfo));
} else { } else {
console.error('get distributed failed with error: ' + error); console.error('get distributed failed with error: ' + error);
} }
}); });
} }
``` ```
...@@ -1037,6 +1046,7 @@ async function example() { ...@@ -1037,6 +1046,7 @@ async function example() {
Provides APIs for encapsulating file asset attributes. Provides APIs for encapsulating file asset attributes.
> **NOTE** > **NOTE**
>
> 1. The system attempts to parse the file content if the file is an audio or video file. The actual field values will be restored from the passed values during scanning on some devices. > 1. The system attempts to parse the file content if the file is an audio or video file. The actual field values will be restored from the passed values during scanning on some devices.
> 2. Some devices may not support the modification of **orientation**. You are advised to use [ModifyImageProperty](js-apis-image.md#modifyimageproperty9) of the **image** module. > 2. Some devices may not support the modification of **orientation**. You are advised to use [ModifyImageProperty](js-apis-image.md#modifyimageproperty9) of the **image** module.
...@@ -1050,10 +1060,10 @@ Provides APIs for encapsulating file asset attributes. ...@@ -1050,10 +1060,10 @@ Provides APIs for encapsulating file asset attributes.
| uri | string | Yes | No | File asset URI, for example, **file://media/image/2**. | | uri | string | Yes | No | File asset URI, for example, **file://media/image/2**. |
| mimeType | string | Yes | No | Extended file attributes. | | mimeType | string | Yes | No | Extended file attributes. |
| mediaType<sup>8+</sup> | [MediaType](#mediatype8) | Yes | No | Media type. | | mediaType<sup>8+</sup> | [MediaType](#mediatype8) | Yes | No | Media type. |
| displayName | string | Yes | Yes | Display file name, including the file name extension. | | displayName | string | Yes | Yes | File name, including the file name extension, to display. |
| title | string | Yes | Yes | Title in the file. By default, it carries the file name without extension. | | title | string | Yes | Yes | Title in the file. |
| relativePath<sup>8+</sup> | string | Yes | Yes | Relative public directory of the file. | | relativePath<sup>8+</sup> | string | Yes | Yes | Relative path of the user directory. |
| parent<sup>8+</sup> | number | Yes | No | Parent directory ID. | | parent<sup>8+</sup> | number | Yes | No | Parent directory ID. Since SDK of API version 10, an asset can exist in multiple albums. Therefore, this attribute is incompatible. The obtained value is always **0**. |
| size | number | Yes | No | File size, in bytes. | | size | number | Yes | No | File size, in bytes. |
| dateAdded | number | Yes | No | Date when the file was added. The value is the number of seconds elapsed since the Epoch time. | | dateAdded | number | Yes | No | Date when the file was added. The value is the number of seconds elapsed since the Epoch time. |
| dateModified | number | Yes | No | Date when the file content (not the file name) was last modified. The value is the number of seconds elapsed since the Epoch time.| | dateModified | number | Yes | No | Date when the file content (not the file name) was last modified. The value is the number of seconds elapsed since the Epoch time.|
...@@ -1064,10 +1074,9 @@ Provides APIs for encapsulating file asset attributes. ...@@ -1064,10 +1074,9 @@ Provides APIs for encapsulating file asset attributes.
| height | number | Yes | No | Image height, in pixels. | | height | number | Yes | No | Image height, in pixels. |
| orientation | number | Yes | Yes | Image display direction (clockwise rotation angle, for example, 0, 90, or 180, in degrees).| | orientation | number | Yes | Yes | Image display direction (clockwise rotation angle, for example, 0, 90, or 180, in degrees).|
| duration<sup>8+</sup> | number | Yes | No | Duration, in ms. | | duration<sup>8+</sup> | number | Yes | No | Duration, in ms. |
| albumId | number | Yes | No | ID of the album to which the file belongs. | | albumId | number | Yes | No | ID of the album to which the file belongs. Since SDK of API version 10, an asset can exist in multiple albums. Therefore, this attribute is incompatible. The obtained value is always **0**. |
| albumUri<sup>8+</sup> | string | Yes | No | URI of the album to which the file belongs. | | albumUri<sup>8+</sup> | string | Yes | No | URI of the album to which the file belongs. Since SDK of API version 10, an asset can exist in multiple albums. Therefore, this attribute is incompatible. The obtained value is always an empty string. |
| albumName | string | Yes | No | Name of the album to which the file belongs. | | albumName | string | Yes | No | Name of the album to which the file belongs. Since SDK of API version 10, an asset can exist in multiple albums. Therefore, this attribute is incompatible. The obtained value is always an empty string. |
### isDirectory<sup>8+</sup> ### isDirectory<sup>8+</sup>
...@@ -1083,29 +1092,29 @@ Checks whether this file asset is a directory. This API uses an asynchronous cal ...@@ -1083,29 +1092,29 @@ Checks whether this file asset is a directory. This API uses an asynchronous cal
| Name | Type | Mandatory | Description | | Name | Type | Mandatory | Description |
| -------- | ---------------------------- | ---- | ------------------- | | -------- | ---------------------------- | ---- | ------------------- |
| callback | AsyncCallback&lt;boolean&gt; | Yes | Callback used to return whether the file asset is a directory.| | callback | AsyncCallback&lt;boolean&gt; | Yes | Callback invoked to return the result. The value **true** means the file asset is a directory; the value false means the opposite.|
**Example** **Example**
```js ```js
async function example() { async function example() {
let fileKeyObj = mediaLibrary.FileKey; let fileKeyObj = mediaLibrary.FileKey;
let imageType = mediaLibrary.MediaType.IMAGE; let imageType = mediaLibrary.MediaType.IMAGE;
let getImageOp = { let getImageOp = {
selections: fileKeyObj.MEDIA_TYPE + '= ?', selections: fileKeyObj.MEDIA_TYPE + '= ?',
selectionArgs: [imageType.toString()], selectionArgs: [imageType.toString()],
order: fileKeyObj.DATE_ADDED + ' DESC', order: fileKeyObj.DATE_ADDED + ' DESC',
}; };
const fetchFileResult = await media.getFileAssets(getImageOp); const fetchFileResult = await media.getFileAssets(getImageOp);
const asset = await fetchFileResult.getFirstObject(); const asset = await fetchFileResult.getFirstObject();
asset.isDirectory((error, isDirectory) => { asset.isDirectory((error, isDirectory) => {
if (error) { if (error) {
console.error('isDirectory failed with error: ' + error); console.error('isDirectory failed with error: ' + error);
} else { } else {
console.info('isDirectory result:' + isDirectory); console.info('isDirectory result:' + isDirectory);
} }
}); });
fetchFileResult.close(); fetchFileResult.close();
} }
``` ```
...@@ -1123,27 +1132,27 @@ Checks whether this file asset is a directory. This API uses a promise to return ...@@ -1123,27 +1132,27 @@ Checks whether this file asset is a directory. This API uses a promise to return
| Type | Description | | Type | Description |
| ---------------------- | ---------------------------- | | ---------------------- | ---------------------------- |
| Promise&lt;boolean&gt; | Promise used to return whether the file asset is a directory.| | Promise&lt;boolean&gt; | Promise used to return the result. The value **true** means the file asset is a directory; the value **false** means the opposite.|
**Example** **Example**
```js ```js
async function example() { async function example() {
let fileKeyObj = mediaLibrary.FileKey; let fileKeyObj = mediaLibrary.FileKey;
let imageType = mediaLibrary.MediaType.IMAGE; let imageType = mediaLibrary.MediaType.IMAGE;
let getImageOp = { let getImageOp = {
selections: fileKeyObj.MEDIA_TYPE + '= ?', selections: fileKeyObj.MEDIA_TYPE + '= ?',
selectionArgs: [imageType.toString()], selectionArgs: [imageType.toString()],
order: fileKeyObj.DATE_ADDED + ' DESC', order: fileKeyObj.DATE_ADDED + ' DESC',
}; };
const fetchFileResult = await media.getFileAssets(getImageOp); const fetchFileResult = await media.getFileAssets(getImageOp);
const asset = await fetchFileResult.getFirstObject(); const asset = await fetchFileResult.getFirstObject();
asset.isDirectory().then((isDirectory) => { asset.isDirectory().then((isDirectory) => {
console.info('isDirectory result:' + isDirectory); console.info('isDirectory result:' + isDirectory);
}).catch((error) => { }).catch((error) => {
console.error('isDirectory failed with error: ' + error); console.error('isDirectory failed with error: ' + error);
}); });
fetchFileResult.close(); fetchFileResult.close();
} }
``` ```
...@@ -1151,7 +1160,10 @@ async function example() { ...@@ -1151,7 +1160,10 @@ async function example() {
commitModify(callback: AsyncCallback&lt;void&gt;): void commitModify(callback: AsyncCallback&lt;void&gt;): void
Commits the modification in this file asset to the database. This API uses an asynchronous callback to return the result. Commits the modification on the file metadata to the database. This API uses an asynchronous callback to return the result.
> **NOTE**
> Since the SDK of API version 10, **audio** does not have the **orientation** attribute. Therefore, the **orientation** attribute of the audio resource cannot be modified by **commitModify()**. For details, see [changelogs-mediaLibrary.md](../../../release-notes/changelogs/OpenHarmony_4.0.8.2/changelogs-mediaLibrary.md).
**Required permissions**: ohos.permission.READ_MEDIA and ohos.permission.WRITE_MEDIA **Required permissions**: ohos.permission.READ_MEDIA and ohos.permission.WRITE_MEDIA
...@@ -1161,26 +1173,26 @@ Commits the modification in this file asset to the database. This API uses an as ...@@ -1161,26 +1173,26 @@ Commits the modification in this file asset to the database. This API uses an as
| Name | Type | Mandatory | Description | | Name | Type | Mandatory | Description |
| -------- | ------------------------- | ---- | ----- | | -------- | ------------------------- | ---- | ----- |
| callback | AsyncCallback&lt;void&gt; | Yes | Void callback.| | callback | AsyncCallback&lt;void&gt; | Yes | Callback that returns no value.|
**Example** **Example**
```js ```js
async function example() { async function example() {
let fileKeyObj = mediaLibrary.FileKey; let fileKeyObj = mediaLibrary.FileKey;
let imageType = mediaLibrary.MediaType.IMAGE; let imageType = mediaLibrary.MediaType.IMAGE;
let getImageOp = { let getImageOp = {
selections: fileKeyObj.MEDIA_TYPE + '= ?', selections: fileKeyObj.MEDIA_TYPE + '= ?',
selectionArgs: [imageType.toString()], selectionArgs: [imageType.toString()],
order: fileKeyObj.DATE_ADDED + ' DESC', order: fileKeyObj.DATE_ADDED + ' DESC',
}; };
const fetchFileResult = await media.getFileAssets(getImageOp); const fetchFileResult = await media.getFileAssets(getImageOp);
const asset = await fetchFileResult.getFirstObject(); const asset = await fetchFileResult.getFirstObject();
asset.title = 'newtitle'; asset.title = 'newtitle';
asset.commitModify(() => { asset.commitModify(() => {
console.info('commitModify successfully'); console.info('commitModify successfully');
}); });
fetchFileResult.close(); fetchFileResult.close();
} }
``` ```
...@@ -1188,7 +1200,10 @@ async function example() { ...@@ -1188,7 +1200,10 @@ async function example() {
commitModify(): Promise&lt;void&gt; commitModify(): Promise&lt;void&gt;
Commits the modification in this file asset to the database. This API uses a promise to return the result. Commits the modification on the file asset to the database. This API uses a promise to return the result.
> **NOTE**
> Since the SDK of API version 10, **audio** does not have the **orientation** attribute. Therefore, the **orientation** attribute of the audio resource cannot be modified by **commitModify()**. For details, see [changelogs-mediaLibrary.md](../../../release-notes/changelogs/OpenHarmony_4.0.8.2/changelogs-mediaLibrary.md).
**Required permissions**: ohos.permission.READ_MEDIA and ohos.permission.WRITE_MEDIA **Required permissions**: ohos.permission.READ_MEDIA and ohos.permission.WRITE_MEDIA
...@@ -1198,24 +1213,24 @@ Commits the modification in this file asset to the database. This API uses a pro ...@@ -1198,24 +1213,24 @@ Commits the modification in this file asset to the database. This API uses a pro
| Type | Description | | Type | Description |
| ------------------- | ---------- | | ------------------- | ---------- |
| Promise&lt;void&gt; | Void promise.| | Promise&lt;void&gt; | Promise that returns no value.|
**Example** **Example**
```js ```js
async function example() { async function example() {
let fileKeyObj = mediaLibrary.FileKey; let fileKeyObj = mediaLibrary.FileKey;
let imageType = mediaLibrary.MediaType.IMAGE; let imageType = mediaLibrary.MediaType.IMAGE;
let getImageOp = { let getImageOp = {
selections: fileKeyObj.MEDIA_TYPE + '= ?', selections: fileKeyObj.MEDIA_TYPE + '= ?',
selectionArgs: [imageType.toString()], selectionArgs: [imageType.toString()],
order: fileKeyObj.DATE_ADDED + ' DESC', order: fileKeyObj.DATE_ADDED + ' DESC',
}; };
const fetchFileResult = await media.getFileAssets(getImageOp); const fetchFileResult = await media.getFileAssets(getImageOp);
const asset = await fetchFileResult.getFirstObject(); const asset = await fetchFileResult.getFirstObject();
asset.title = 'newtitle'; asset.title = 'newtitle';
await asset.commitModify(); await asset.commitModify();
fetchFileResult.close(); fetchFileResult.close();
} }
``` ```
...@@ -1225,7 +1240,7 @@ open(mode: string, callback: AsyncCallback&lt;number&gt;): void ...@@ -1225,7 +1240,7 @@ open(mode: string, callback: AsyncCallback&lt;number&gt;): void
Opens this file asset. This API uses an asynchronous callback to return the result. Opens this file asset. This API uses an asynchronous callback to return the result.
**NOTE**: When a file is opened in 'w' mode, the returned FD cannot be read. However, due to the implementation differences of file systems, some user-mode files opened in 'w' mode can be read by using FD. To perform the read or write operation on a file by using FD, you are advised to open the file in 'rw' mode. The write operations are mutually exclusive. After a write operation is complete, you must call **close** to release the resource. **NOTE**<br>If a file is opened in 'w' mode, the returned file descriptor (FD) cannot be used to read the file. However, due to the implementation differences of file systems, some user-mode files opened in 'w' mode can be read by using FD. To perform the read or write operation on a file by using FD, you are advised to open the file in 'rw' mode. The write operations are mutually exclusive. After a write operation is complete, you must call **close** to close the file.
**Required permissions**: ohos.permission.READ_MEDIA or ohos.permission.WRITE_MEDIA **Required permissions**: ohos.permission.READ_MEDIA or ohos.permission.WRITE_MEDIA
...@@ -1235,24 +1250,24 @@ Opens this file asset. This API uses an asynchronous callback to return the resu ...@@ -1235,24 +1250,24 @@ Opens this file asset. This API uses an asynchronous callback to return the resu
| Name | Type | Mandatory | Description | | Name | Type | Mandatory | Description |
| -------- | --------------------------- | ---- | ----------------------------------- | | -------- | --------------------------- | ---- | ----------------------------------- |
| mode | string | Yes | Mode of opening the file, for example, **'r'** (read-only), **'w'** (write-only), and **'rw'** (read-write).| | mode | string | Yes | File open mode, which can be **r** (read-only), **w** (write-only), or **rw** (read-write).|
| callback | AsyncCallback&lt;number&gt; | Yes | Callback used to return the file descriptor. | | callback | AsyncCallback&lt;number&gt; | Yes | Callback invoked to return the FD of the file opened. |
**Example** **Example**
```js ```js
async function example() { async function example() {
let mediaType = mediaLibrary.MediaType.IMAGE; let mediaType = mediaLibrary.MediaType.IMAGE;
let DIR_IMAGE = mediaLibrary.DirectoryType.DIR_IMAGE; let DIR_IMAGE = mediaLibrary.DirectoryType.DIR_IMAGE;
const path = await media.getPublicDirectory(DIR_IMAGE); const path = await media.getPublicDirectory(DIR_IMAGE);
const asset = await media.createAsset(mediaType, 'image00003.jpg', path); const asset = await media.createAsset(mediaType, 'image00003.jpg', path);
asset.open('rw', (error, fd) => { asset.open('rw', (error, fd) => {
if (fd > 0) { if (fd > 0) {
asset.close(fd); asset.close(fd);
} else { } else {
console.error('File Open failed with error: ' + error); console.error('File Open failed with error: ' + error);
} }
}); });
} }
``` ```
...@@ -1262,7 +1277,7 @@ open(mode: string): Promise&lt;number&gt; ...@@ -1262,7 +1277,7 @@ open(mode: string): Promise&lt;number&gt;
Opens this file asset. This API uses a promise to return the result. Opens this file asset. This API uses a promise to return the result.
**NOTE**: When a file is opened in 'w' mode, the returned FD cannot be read. However, due to the implementation differences of file systems, some user-mode files opened in 'w' mode can be read by using FD. To perform the read or write operation on a file by using FD, you are advised to open the file in 'rw' mode. The write operations are mutually exclusive. After a write operation is complete, you must call **close** to release the resource. **NOTE**<br>If a file is opened in 'w' mode, the returned FD cannot be used to read the file. However, due to the implementation differences of file systems, some user-mode files opened in 'w' mode can be read by using FD. To perform the read or write operation on a file by using FD, you are advised to open the file in 'rw' mode. The write operations are mutually exclusive. After a write operation is complete, you must call **close** to close the file.
**Required permissions**: ohos.permission.READ_MEDIA or ohos.permission.WRITE_MEDIA **Required permissions**: ohos.permission.READ_MEDIA or ohos.permission.WRITE_MEDIA
...@@ -1272,27 +1287,27 @@ Opens this file asset. This API uses a promise to return the result. ...@@ -1272,27 +1287,27 @@ Opens this file asset. This API uses a promise to return the result.
| Name | Type | Mandatory | Description | | Name | Type | Mandatory | Description |
| ---- | ------ | ---- | ----------------------------------- | | ---- | ------ | ---- | ----------------------------------- |
| mode | string | Yes | Mode of opening the file, for example, **'r'** (read-only), **'w'** (write-only), and **'rw'** (read-write).| | mode | string | Yes | File open mode, which can be **r** (read-only), **w** (write-only), or **rw** (read-write).|
**Return value** **Return value**
| Type | Description | | Type | Description |
| --------------------- | ------------- | | --------------------- | ------------- |
| Promise&lt;number&gt; | Promise used to return the file descriptor.| | Promise&lt;number&gt; | Promise used to return the FD of the file opened.|
**Example** **Example**
```js ```js
async function example() { async function example() {
let mediaType = mediaLibrary.MediaType.IMAGE; let mediaType = mediaLibrary.MediaType.IMAGE;
let DIR_IMAGE = mediaLibrary.DirectoryType.DIR_IMAGE; let DIR_IMAGE = mediaLibrary.DirectoryType.DIR_IMAGE;
const path = await media.getPublicDirectory(DIR_IMAGE); const path = await media.getPublicDirectory(DIR_IMAGE);
const asset = await media.createAsset(mediaType, 'image00003.jpg', path); const asset = await media.createAsset(mediaType, 'image00003.jpg', path);
asset.open('rw').then((fd) => { asset.open('rw').then((fd) => {
console.info('File open fd: ' + fd); console.info('File open fd: ' + fd);
}).catch((error) => { }).catch((error) => {
console.error('File open failed with error: ' + error); console.error('File open failed with error: ' + error);
}); });
} }
``` ```
...@@ -1300,7 +1315,7 @@ async function example() { ...@@ -1300,7 +1315,7 @@ async function example() {
close(fd: number, callback: AsyncCallback&lt;void&gt;): void close(fd: number, callback: AsyncCallback&lt;void&gt;): void
Closes this file asset. This API uses an asynchronous callback to return the result. Closes a file. This API uses an asynchronous callback to return the result.
**Required permissions**: ohos.permission.READ_MEDIA or ohos.permission.WRITE_MEDIA **Required permissions**: ohos.permission.READ_MEDIA or ohos.permission.WRITE_MEDIA
...@@ -1310,35 +1325,35 @@ Closes this file asset. This API uses an asynchronous callback to return the res ...@@ -1310,35 +1325,35 @@ Closes this file asset. This API uses an asynchronous callback to return the res
| Name | Type | Mandatory | Description | | Name | Type | Mandatory | Description |
| -------- | ------------------------- | ---- | ----- | | -------- | ------------------------- | ---- | ----- |
| fd | number | Yes | File descriptor.| | fd | number | Yes | FD of the file to close.|
| callback | AsyncCallback&lt;void&gt; | Yes | Void callback.| | callback | AsyncCallback&lt;void&gt; | Yes | Callback that returns no value.|
**Example** **Example**
```js ```js
async function example() { async function example() {
let fileKeyObj = mediaLibrary.FileKey; let fileKeyObj = mediaLibrary.FileKey;
let imageType = mediaLibrary.MediaType.IMAGE; let imageType = mediaLibrary.MediaType.IMAGE;
let getImageOp = { let getImageOp = {
selections: fileKeyObj.MEDIA_TYPE + '= ?', selections: fileKeyObj.MEDIA_TYPE + '= ?',
selectionArgs: [imageType.toString()], selectionArgs: [imageType.toString()],
order: fileKeyObj.DATE_ADDED + ' DESC', order: fileKeyObj.DATE_ADDED + ' DESC',
}; };
const fetchFileResult = await media.getFileAssets(getImageOp); const fetchFileResult = await media.getFileAssets(getImageOp);
const asset = await fetchFileResult.getFirstObject(); const asset = await fetchFileResult.getFirstObject();
asset.open('rw').then((fd) => { asset.open('rw').then((fd) => {
console.info('File open fd: ' + fd); console.info('File open fd: ' + fd);
asset.close(fd, (error) => { asset.close(fd, (error) => {
if (error) { if (error) {
console.error('asset.close failed with error: ' + error); console.error('asset.close failed with error: ' + error);
} else { } else {
console.info('asset.close successfully'); console.info('asset.close successfully');
} }
});
}).catch((error) => {
console.error('File open failed with error: ' + error);
}); });
fetchFileResult.close(); }).catch((error) => {
console.error('File open failed with error: ' + error);
});
fetchFileResult.close();
} }
``` ```
...@@ -1346,7 +1361,7 @@ async function example() { ...@@ -1346,7 +1361,7 @@ async function example() {
close(fd: number): Promise&lt;void&gt; close(fd: number): Promise&lt;void&gt;
Closes this file asset. This API uses a promise to return the result. Closes a file. This API uses a promise to return the result.
**Required permissions**: ohos.permission.READ_MEDIA or ohos.permission.WRITE_MEDIA **Required permissions**: ohos.permission.READ_MEDIA or ohos.permission.WRITE_MEDIA
...@@ -1356,38 +1371,38 @@ Closes this file asset. This API uses a promise to return the result. ...@@ -1356,38 +1371,38 @@ Closes this file asset. This API uses a promise to return the result.
| Name | Type | Mandatory | Description | | Name | Type | Mandatory | Description |
| ---- | ------ | ---- | ----- | | ---- | ------ | ---- | ----- |
| fd | number | Yes | File descriptor.| | fd | number | Yes | FD of the file to close.|
**Return value** **Return value**
| Type | Description | | Type | Description |
| ------------------- | ---------- | | ------------------- | ---------- |
| Promise&lt;void&gt; | Void promise.| | Promise&lt;void&gt; | Promise that returns no value.|
**Example** **Example**
```js ```js
async function example() { async function example() {
let fileKeyObj = mediaLibrary.FileKey; let fileKeyObj = mediaLibrary.FileKey;
let imageType = mediaLibrary.MediaType.IMAGE; let imageType = mediaLibrary.MediaType.IMAGE;
let getImageOp = { let getImageOp = {
selections: fileKeyObj.MEDIA_TYPE + '= ?', selections: fileKeyObj.MEDIA_TYPE + '= ?',
selectionArgs: [imageType.toString()], selectionArgs: [imageType.toString()],
order: fileKeyObj.DATE_ADDED + ' DESC', order: fileKeyObj.DATE_ADDED + ' DESC',
}; };
const fetchFileResult = await media.getFileAssets(getImageOp); const fetchFileResult = await media.getFileAssets(getImageOp);
const asset = await fetchFileResult.getFirstObject(); const asset = await fetchFileResult.getFirstObject();
asset.open('rw').then((fd) => { asset.open('rw').then((fd) => {
console.info('File fd!' + fd); console.info('File fd!' + fd);
asset.close(fd).then(() => { asset.close(fd).then(() => {
console.info('asset.close successfully'); console.info('asset.close successfully');
}).catch((closeErr) => { }).catch((closeErr) => {
console.error('asset.close fail, closeErr: ' + closeErr); console.error('asset.close fail, closeErr: ' + closeErr);
});
}).catch((error) => {
console.error('open File failed with error: ' + error);
}); });
fetchFileResult.close(); }).catch((error) => {
console.error('open File failed with error: ' + error);
});
fetchFileResult.close();
} }
``` ```
...@@ -1405,29 +1420,29 @@ Obtains the thumbnail of this file asset. This API uses an asynchronous callback ...@@ -1405,29 +1420,29 @@ Obtains the thumbnail of this file asset. This API uses an asynchronous callback
| Name | Type | Mandatory | Description | | Name | Type | Mandatory | Description |
| -------- | ----------------------------------- | ---- | ---------------- | | -------- | ----------------------------------- | ---- | ---------------- |
| callback | AsyncCallback&lt;image.PixelMap&gt; | Yes | Callback used to return the pixel map of the thumbnail.| | callback | AsyncCallback&lt;image.PixelMap&gt; | Yes | Callback invoked to return the PixelMap of the thumbnail.|
**Example** **Example**
```js ```js
async function example() { async function example() {
let fileKeyObj = mediaLibrary.FileKey; let fileKeyObj = mediaLibrary.FileKey;
let imageType = mediaLibrary.MediaType.IMAGE; let imageType = mediaLibrary.MediaType.IMAGE;
let getImageOp = { let getImageOp = {
selections: fileKeyObj.MEDIA_TYPE + '= ?', selections: fileKeyObj.MEDIA_TYPE + '= ?',
selectionArgs: [imageType.toString()], selectionArgs: [imageType.toString()],
order: fileKeyObj.DATE_ADDED + ' DESC', order: fileKeyObj.DATE_ADDED + ' DESC',
}; };
const fetchFileResult = await media.getFileAssets(getImageOp); const fetchFileResult = await media.getFileAssets(getImageOp);
const asset = await fetchFileResult.getFirstObject(); const asset = await fetchFileResult.getFirstObject();
asset.getThumbnail((error, pixelmap) => { asset.getThumbnail((error, pixelmap) => {
if (error) { if (error) {
console.error('mediaLibrary getThumbnail failed with error: ' + error); console.error('mediaLibrary getThumbnail failed with error: ' + error);
} else { } else {
console.info('mediaLibrary getThumbnail Successful, pixelmap ' + JSON.stringify(pixelmap)); console.info('mediaLibrary getThumbnail Successful, pixelmap ' + JSON.stringify(pixelmap));
} }
}); });
fetchFileResult.close(); fetchFileResult.close();
} }
``` ```
...@@ -1435,7 +1450,7 @@ async function example() { ...@@ -1435,7 +1450,7 @@ async function example() {
getThumbnail(size: Size, callback: AsyncCallback&lt;image.PixelMap&gt;): void getThumbnail(size: Size, callback: AsyncCallback&lt;image.PixelMap&gt;): void
Obtains the thumbnail of this file asset, with the thumbnail size passed. This API uses an asynchronous callback to return the result. Obtains the file thumbnail of the given size. This API uses an asynchronous callback to return the result.
**Required permissions**: ohos.permission.READ_MEDIA **Required permissions**: ohos.permission.READ_MEDIA
...@@ -1446,30 +1461,30 @@ Obtains the thumbnail of this file asset, with the thumbnail size passed. This A ...@@ -1446,30 +1461,30 @@ Obtains the thumbnail of this file asset, with the thumbnail size passed. This A
| Name | Type | Mandatory | Description | | Name | Type | Mandatory | Description |
| -------- | ----------------------------------- | ---- | ---------------- | | -------- | ----------------------------------- | ---- | ---------------- |
| size | [Size](#size8) | Yes | Size of the thumbnail. | | size | [Size](#size8) | Yes | Size of the thumbnail. |
| callback | AsyncCallback&lt;image.PixelMap&gt; | Yes | Callback used to return the pixel map of the thumbnail.| | callback | AsyncCallback&lt;image.PixelMap&gt; | Yes | Callback invoked to return the PixelMap of the thumbnail.|
**Example** **Example**
```js ```js
async function example() { async function example() {
let fileKeyObj = mediaLibrary.FileKey; let fileKeyObj = mediaLibrary.FileKey;
let imageType = mediaLibrary.MediaType.IMAGE; let imageType = mediaLibrary.MediaType.IMAGE;
let getImageOp = { let getImageOp = {
selections: fileKeyObj.MEDIA_TYPE + '= ?', selections: fileKeyObj.MEDIA_TYPE + '= ?',
selectionArgs: [imageType.toString()], selectionArgs: [imageType.toString()],
order: fileKeyObj.DATE_ADDED + ' DESC', order: fileKeyObj.DATE_ADDED + ' DESC',
}; };
let size = { width: 720, height: 720 }; let size = { width: 720, height: 720 };
const fetchFileResult = await media.getFileAssets(getImageOp); const fetchFileResult = await media.getFileAssets(getImageOp);
const asset = await fetchFileResult.getFirstObject(); const asset = await fetchFileResult.getFirstObject();
asset.getThumbnail(size, (error, pixelmap) => { asset.getThumbnail(size, (error, pixelmap) => {
if (error) { if (error) {
console.error('mediaLibrary getThumbnail failed with error: ' + error); console.error('mediaLibrary getThumbnail failed with error: ' + error);
} else { } else {
console.info('mediaLibrary getThumbnail Successful, pixelmap ' + JSON.stringify(pixelmap)); console.info('mediaLibrary getThumbnail Successful, pixelmap ' + JSON.stringify(pixelmap));
} }
}); });
fetchFileResult.close(); fetchFileResult.close();
} }
``` ```
...@@ -1477,7 +1492,7 @@ async function example() { ...@@ -1477,7 +1492,7 @@ async function example() {
getThumbnail(size?: Size): Promise&lt;image.PixelMap&gt; getThumbnail(size?: Size): Promise&lt;image.PixelMap&gt;
Obtains the thumbnail of this file asset, with the thumbnail size passed. This API uses a promise to return the result. Obtains the file thumbnail of the given size. This API uses a promise to return the result.
**Required permissions**: ohos.permission.READ_MEDIA **Required permissions**: ohos.permission.READ_MEDIA
...@@ -1493,28 +1508,28 @@ Obtains the thumbnail of this file asset, with the thumbnail size passed. This A ...@@ -1493,28 +1508,28 @@ Obtains the thumbnail of this file asset, with the thumbnail size passed. This A
| Type | Description | | Type | Description |
| ----------------------------- | --------------------- | | ----------------------------- | --------------------- |
| Promise&lt;image.PixelMap&gt; | Promise to return the pixel map of the thumbnail.| | Promise&lt;image.PixelMap&gt; | Promise used to return the PixelMap of the thumbnail.|
**Example** **Example**
```js ```js
async function example() { async function example() {
let fileKeyObj = mediaLibrary.FileKey; let fileKeyObj = mediaLibrary.FileKey;
let imageType = mediaLibrary.MediaType.IMAGE; let imageType = mediaLibrary.MediaType.IMAGE;
let getImageOp = { let getImageOp = {
selections: fileKeyObj.MEDIA_TYPE + '= ?', selections: fileKeyObj.MEDIA_TYPE + '= ?',
selectionArgs: [imageType.toString()], selectionArgs: [imageType.toString()],
order: fileKeyObj.DATE_ADDED + ' DESC', order: fileKeyObj.DATE_ADDED + ' DESC',
}; };
let size = { width: 720, height: 720 }; let size = { width: 720, height: 720 };
const fetchFileResult = await media.getFileAssets(getImageOp); const fetchFileResult = await media.getFileAssets(getImageOp);
const asset = await fetchFileResult.getFirstObject(); const asset = await fetchFileResult.getFirstObject();
asset.getThumbnail(size).then((pixelmap) => { asset.getThumbnail(size).then((pixelmap) => {
console.info('mediaLibrary getThumbnail Successful, pixelmap ' + JSON.stringify(pixelmap)); console.info('mediaLibrary getThumbnail Successful, pixelmap ' + JSON.stringify(pixelmap));
}).catch((error) => { }).catch((error) => {
console.error('mediaLibrary getThumbnail failed with error: ' + error); console.error('mediaLibrary getThumbnail failed with error: ' + error);
}); });
fetchFileResult.close(); fetchFileResult.close();
} }
``` ```
...@@ -1532,30 +1547,30 @@ Favorites or unfavorites this file asset. This API uses an asynchronous callback ...@@ -1532,30 +1547,30 @@ Favorites or unfavorites this file asset. This API uses an asynchronous callback
| Name | Type | Mandatory | Description | | Name | Type | Mandatory | Description |
| ---------- | ------------------------- | ---- | ---------------------------------- | | ---------- | ------------------------- | ---- | ---------------------------------- |
| isFavorite | boolean | Yes | Whether to favorite or unfavorite the file. The value **true** means to favorite the file, and **false** means to unfavorite the file.| | isFavorite | boolean | Yes | Operation to perform. The value **true** means to favorite the file asset, and **false** means the opposite.|
| callback | AsyncCallback&lt;void&gt; | Yes | Void callback. | | callback | AsyncCallback&lt;void&gt; | Yes | Callback that returns no value. |
**Example** **Example**
```js ```js
async function example() { async function example() {
let fileKeyObj = mediaLibrary.FileKey; let fileKeyObj = mediaLibrary.FileKey;
let imageType = mediaLibrary.MediaType.IMAGE; let imageType = mediaLibrary.MediaType.IMAGE;
let getImageOp = { let getImageOp = {
selections: fileKeyObj.MEDIA_TYPE + '= ?', selections: fileKeyObj.MEDIA_TYPE + '= ?',
selectionArgs: [imageType.toString()], selectionArgs: [imageType.toString()],
order: fileKeyObj.DATE_ADDED + ' DESC', order: fileKeyObj.DATE_ADDED + ' DESC',
}; };
const fetchFileResult = await media.getFileAssets(getImageOp); const fetchFileResult = await media.getFileAssets(getImageOp);
const asset = await fetchFileResult.getFirstObject(); const asset = await fetchFileResult.getFirstObject();
asset.favorite(true,(error) => { asset.favorite(true,(error) => {
if (error) { if (error) {
console.error('mediaLibrary favorite failed with error: ' + error); console.error('mediaLibrary favorite failed with error: ' + error);
} else { } else {
console.info('mediaLibrary favorite Successful'); console.info('mediaLibrary favorite Successful');
} }
}); });
fetchFileResult.close(); fetchFileResult.close();
} }
``` ```
...@@ -1573,33 +1588,33 @@ Favorites or unfavorites this file asset. This API uses a promise to return the ...@@ -1573,33 +1588,33 @@ Favorites or unfavorites this file asset. This API uses a promise to return the
| Name | Type | Mandatory | Description | | Name | Type | Mandatory | Description |
| ---------- | ------- | ---- | ---------------------------------- | | ---------- | ------- | ---- | ---------------------------------- |
| isFavorite | boolean | Yes | Whether to favorite or unfavorite the file. The value **true** means to favorite the file, and **false** means to unfavorite the file.| | isFavorite | boolean | Yes | Operation to perform. The value **true** means to favorite the file asset, and **false** means the opposite.|
**Return value** **Return value**
| Type | Description | | Type | Description |
| ------------------- | ---------- | | ------------------- | ---------- |
| Promise&lt;void&gt; | Void promise.| | Promise&lt;void&gt; | Promise that returns no value.|
**Example** **Example**
```js ```js
async function example() { async function example() {
let fileKeyObj = mediaLibrary.FileKey; let fileKeyObj = mediaLibrary.FileKey;
let imageType = mediaLibrary.MediaType.IMAGE; let imageType = mediaLibrary.MediaType.IMAGE;
let getImageOp = { let getImageOp = {
selections: fileKeyObj.MEDIA_TYPE + '= ?', selections: fileKeyObj.MEDIA_TYPE + '= ?',
selectionArgs: [imageType.toString()], selectionArgs: [imageType.toString()],
order: fileKeyObj.DATE_ADDED + ' DESC', order: fileKeyObj.DATE_ADDED + ' DESC',
}; };
const fetchFileResult = await media.getFileAssets(getImageOp); const fetchFileResult = await media.getFileAssets(getImageOp);
const asset = await fetchFileResult.getFirstObject(); const asset = await fetchFileResult.getFirstObject();
asset.favorite(true).then(() => { asset.favorite(true).then(() => {
console.info('mediaLibrary favorite Successful'); console.info('mediaLibrary favorite Successful');
}).catch((error) => { }).catch((error) => {
console.error('mediaLibrary favorite failed with error: ' + error); console.error('mediaLibrary favorite failed with error: ' + error);
}); });
fetchFileResult.close(); fetchFileResult.close();
} }
``` ```
...@@ -1617,29 +1632,29 @@ Checks whether this file asset is favorited. This API uses an asynchronous callb ...@@ -1617,29 +1632,29 @@ Checks whether this file asset is favorited. This API uses an asynchronous callb
| Name | Type | Mandatory | Description | | Name | Type | Mandatory | Description |
| -------- | ---------------------------- | ---- | ----------- | | -------- | ---------------------------- | ---- | ----------- |
| callback | AsyncCallback&lt;boolean&gt; | Yes | Callback used to return whether the file asset is favorited.| | callback | AsyncCallback&lt;boolean&gt; | Yes | Callback invoked to return the result. The value **true** means the file asset is favorited; the value **false** means the opposite.|
**Example** **Example**
```js ```js
async function example() { async function example() {
let fileKeyObj = mediaLibrary.FileKey; let fileKeyObj = mediaLibrary.FileKey;
let imageType = mediaLibrary.MediaType.IMAGE; let imageType = mediaLibrary.MediaType.IMAGE;
let getImageOp = { let getImageOp = {
selections: fileKeyObj.MEDIA_TYPE + '= ?', selections: fileKeyObj.MEDIA_TYPE + '= ?',
selectionArgs: [imageType.toString()], selectionArgs: [imageType.toString()],
order: fileKeyObj.DATE_ADDED + ' DESC', order: fileKeyObj.DATE_ADDED + ' DESC',
}; };
const fetchFileResult = await media.getFileAssets(getImageOp); const fetchFileResult = await media.getFileAssets(getImageOp);
const asset = await fetchFileResult.getFirstObject(); const asset = await fetchFileResult.getFirstObject();
asset.isFavorite((error, isFavorite) => { asset.isFavorite((error, isFavorite) => {
if (error) { if (error) {
console.error('mediaLibrary favoriisFavoritete failed with error: ' + error); console.error('mediaLibrary favoriisFavoritete failed with error: ' + error);
} else { } else {
console.info('mediaLibrary isFavorite Successful, isFavorite result: ' + isFavorite); console.info('mediaLibrary isFavorite Successful, isFavorite result: ' + isFavorite);
} }
}); });
fetchFileResult.close(); fetchFileResult.close();
} }
``` ```
...@@ -1657,27 +1672,27 @@ Checks whether this file asset is favorited. This API uses a promise to return t ...@@ -1657,27 +1672,27 @@ Checks whether this file asset is favorited. This API uses a promise to return t
| Type | Description | | Type | Description |
| ---------------------- | ------------------ | | ---------------------- | ------------------ |
| Promise&lt;boolean&gt; | Promise used to return whether the file asset is favorited.| | Promise&lt;boolean&gt; | Promise used to return the result. The value **true** means the file asset is favorited; the value **false** means the opposite.|
**Example** **Example**
```js ```js
async function example() { async function example() {
let fileKeyObj = mediaLibrary.FileKey; let fileKeyObj = mediaLibrary.FileKey;
let imageType = mediaLibrary.MediaType.IMAGE; let imageType = mediaLibrary.MediaType.IMAGE;
let getImageOp = { let getImageOp = {
selections: fileKeyObj.MEDIA_TYPE + '= ?', selections: fileKeyObj.MEDIA_TYPE + '= ?',
selectionArgs: [imageType.toString()], selectionArgs: [imageType.toString()],
order: fileKeyObj.DATE_ADDED + ' DESC', order: fileKeyObj.DATE_ADDED + ' DESC',
}; };
const fetchFileResult = await media.getFileAssets(getImageOp); const fetchFileResult = await media.getFileAssets(getImageOp);
const asset = await fetchFileResult.getFirstObject(); const asset = await fetchFileResult.getFirstObject();
asset.isFavorite().then((isFavorite) => { asset.isFavorite().then((isFavorite) => {
console.info('mediaLibrary isFavorite Successful, isFavorite result: ' + isFavorite); console.info('mediaLibrary isFavorite Successful, isFavorite result: ' + isFavorite);
}).catch((error) => { }).catch((error) => {
console.error('mediaLibrary favoriisFavoritete failed with error: ' + error); console.error('mediaLibrary favoriisFavoritete failed with error: ' + error);
}); });
fetchFileResult.close(); fetchFileResult.close();
} }
``` ```
...@@ -1697,30 +1712,30 @@ Files in the trash are not actually deleted. You can set **isTrash** to **false* ...@@ -1697,30 +1712,30 @@ Files in the trash are not actually deleted. You can set **isTrash** to **false*
| Name | Type | Mandatory | Description | | Name | Type | Mandatory | Description |
| -------- | ------------------------- | ---- | --------- | | -------- | ------------------------- | ---- | --------- |
| isTrash | boolean | Yes | Whether to move the file asset to the trash. The value **true** means to move the file asset to the trash, and **false** means the opposite.| | isTrash | boolean | Yes | Whether to move the file asset to the trash.|
| callback | AsyncCallback&lt;void&gt; | Yes | Void callback. | | callback | AsyncCallback&lt;void&gt; | Yes | Callback that returns no value. |
**Example** **Example**
```js ```js
async function example() { async function example() {
let fileKeyObj = mediaLibrary.FileKey; let fileKeyObj = mediaLibrary.FileKey;
let imageType = mediaLibrary.MediaType.IMAGE; let imageType = mediaLibrary.MediaType.IMAGE;
let getImageOp = { let getImageOp = {
selections: fileKeyObj.MEDIA_TYPE + '= ?', selections: fileKeyObj.MEDIA_TYPE + '= ?',
selectionArgs: [imageType.toString()], selectionArgs: [imageType.toString()],
order: fileKeyObj.DATE_ADDED + ' DESC', order: fileKeyObj.DATE_ADDED + ' DESC',
}; };
const fetchFileResult = await media.getFileAssets(getImageOp); const fetchFileResult = await media.getFileAssets(getImageOp);
const asset = await fetchFileResult.getFirstObject(); const asset = await fetchFileResult.getFirstObject();
asset.trash(true, (error) => { asset.trash(true, (error) => {
if (error) { if (error) {
console.error('mediaLibrary trash failed with error: ' + error); console.error('mediaLibrary trash failed with error: ' + error);
} else { } else {
console.info('mediaLibrary trash Successful'); console.info('mediaLibrary trash Successful');
} }
}); });
fetchFileResult.close(); fetchFileResult.close();
} }
``` ```
...@@ -1740,33 +1755,33 @@ Files in the trash are not actually deleted. You can set **isTrash** to **false* ...@@ -1740,33 +1755,33 @@ Files in the trash are not actually deleted. You can set **isTrash** to **false*
| Name | Type | Mandatory | Description | | Name | Type | Mandatory | Description |
| ------- | ------- | ---- | --------- | | ------- | ------- | ---- | --------- |
| isTrash | boolean | Yes | Whether to move the file asset to the trash. The value **true** means to move the file asset to the trash, and **false** means the opposite.| | isTrash | boolean | Yes | Whether to move the file asset to the trash.|
**Return value** **Return value**
| Type | Description | | Type | Description |
| ------------------- | ---------- | | ------------------- | ---------- |
| Promise&lt;void&gt; | Void promise.| | Promise&lt;void&gt; | Promise that returns no value.|
**Example** **Example**
```js ```js
async function example() { async function example() {
let fileKeyObj = mediaLibrary.FileKey; let fileKeyObj = mediaLibrary.FileKey;
let imageType = mediaLibrary.MediaType.IMAGE; let imageType = mediaLibrary.MediaType.IMAGE;
let getImageOp = { let getImageOp = {
selections: fileKeyObj.MEDIA_TYPE + '= ?', selections: fileKeyObj.MEDIA_TYPE + '= ?',
selectionArgs: [imageType.toString()], selectionArgs: [imageType.toString()],
order: fileKeyObj.DATE_ADDED + ' DESC', order: fileKeyObj.DATE_ADDED + ' DESC',
}; };
const fetchFileResult = await media.getFileAssets(getImageOp); const fetchFileResult = await media.getFileAssets(getImageOp);
const asset = await fetchFileResult.getFirstObject(); const asset = await fetchFileResult.getFirstObject();
asset.trash(true).then(() => { asset.trash(true).then(() => {
console.info('trash successfully'); console.info('trash successfully');
}).catch((error) => { }).catch((error) => {
console.error('trash failed with error: ' + error); console.error('trash failed with error: ' + error);
}); });
fetchFileResult.close(); fetchFileResult.close();
} }
``` ```
...@@ -1784,29 +1799,29 @@ Checks whether this file asset is in the trash. This API uses an asynchronous ca ...@@ -1784,29 +1799,29 @@ Checks whether this file asset is in the trash. This API uses an asynchronous ca
| Name | Type | Mandatory | Description | | Name | Type | Mandatory | Description |
| -------- | ---------------------------- | ---- | --------------- | | -------- | ---------------------------- | ---- | --------------- |
| callback | AsyncCallback&lt;boolean&gt; | Yes | Callback used to return whether the file asset is in the trash. If the file asset is in the trash, **true** will be returned; otherwise, **false** will be returned.| | callback | AsyncCallback&lt;boolean&gt; | Yes | Callback invoked to return the result. The value **true** means the file is in the trash; the value **false** means the opposite.|
**Example** **Example**
```js ```js
async function example() { async function example() {
let fileKeyObj = mediaLibrary.FileKey; let fileKeyObj = mediaLibrary.FileKey;
let imageType = mediaLibrary.MediaType.IMAGE; let imageType = mediaLibrary.MediaType.IMAGE;
let getImageOp = { let getImageOp = {
selections: fileKeyObj.MEDIA_TYPE + '= ?', selections: fileKeyObj.MEDIA_TYPE + '= ?',
selectionArgs: [imageType.toString()], selectionArgs: [imageType.toString()],
order: fileKeyObj.DATE_ADDED + ' DESC', order: fileKeyObj.DATE_ADDED + ' DESC',
}; };
const fetchFileResult = await media.getFileAssets(getImageOp); const fetchFileResult = await media.getFileAssets(getImageOp);
const asset = await fetchFileResult.getFirstObject(); const asset = await fetchFileResult.getFirstObject();
asset.isTrash((error, isTrash) => { asset.isTrash((error, isTrash) => {
if (error) { if (error) {
console.error('Failed to get trash state failed with error: ' + error); console.error('Failed to get trash state failed with error: ' + error);
return; return;
} }
console.info('Get trash state successfully, isTrash result: ' + isTrash); console.info('Get trash state successfully, isTrash result: ' + isTrash);
}); });
fetchFileResult.close(); fetchFileResult.close();
} }
``` ```
...@@ -1824,33 +1839,33 @@ Checks whether this file asset is in the trash. This API uses a promise to retur ...@@ -1824,33 +1839,33 @@ Checks whether this file asset is in the trash. This API uses a promise to retur
| Type | Description | | Type | Description |
| ------------------- | -------------------- | | ------------------- | -------------------- |
| Promise&lt;void&gt; | Promise used to return whether the file asset is in the trash. If the file asset is in the trash, **true** will be returned; otherwise, **false** will be returned.| | Promise&lt;void&gt; | Promise used to return the result. The value **true** means the file is in the trash; the value **false** means the opposite.|
**Example** **Example**
```js ```js
async function example() { async function example() {
let fileKeyObj = mediaLibrary.FileKey; let fileKeyObj = mediaLibrary.FileKey;
let imageType = mediaLibrary.MediaType.IMAGE; let imageType = mediaLibrary.MediaType.IMAGE;
let getImageOp = { let getImageOp = {
selections: fileKeyObj.MEDIA_TYPE + '= ?', selections: fileKeyObj.MEDIA_TYPE + '= ?',
selectionArgs: [imageType.toString()], selectionArgs: [imageType.toString()],
order: fileKeyObj.DATE_ADDED + ' DESC', order: fileKeyObj.DATE_ADDED + ' DESC',
}; };
const fetchFileResult = await media.getFileAssets(getImageOp); const fetchFileResult = await media.getFileAssets(getImageOp);
const asset = await fetchFileResult.getFirstObject(); const asset = await fetchFileResult.getFirstObject();
asset.isTrash().then((isTrash) => { asset.isTrash().then((isTrash) => {
console.info('isTrash result: ' + isTrash); console.info('isTrash result: ' + isTrash);
}).catch((error) => { }).catch((error) => {
console.error('isTrash failed with error: ' + error); console.error('isTrash failed with error: ' + error);
}); });
fetchFileResult.close(); fetchFileResult.close();
} }
``` ```
## FetchFileResult<sup>7+</sup> ## FetchFileResult<sup>7+</sup>
Implements the result set of the file retrieval operation. Provides APIs to manage the file retrieval result.
### getCount<sup>7+</sup> ### getCount<sup>7+</sup>
...@@ -1864,23 +1879,23 @@ Obtains the total number of files in the result set. ...@@ -1864,23 +1879,23 @@ Obtains the total number of files in the result set.
| Type | Description | | Type | Description |
| ------ | -------- | | ------ | -------- |
| number | Total number of files.| | number | Returns the total number of files obtained.|
**Example** **Example**
```js ```js
async function example() { async function example() {
let fileKeyObj = mediaLibrary.FileKey; let fileKeyObj = mediaLibrary.FileKey;
let fileType = mediaLibrary.MediaType.FILE; let fileType = mediaLibrary.MediaType.FILE;
let getFileCountOneOp = { let getFileCountOneOp = {
selections: fileKeyObj.MEDIA_TYPE + '= ?', selections: fileKeyObj.MEDIA_TYPE + '= ?',
selectionArgs: [fileType.toString()], selectionArgs: [fileType.toString()],
order: fileKeyObj.DATE_ADDED + ' DESC', order: fileKeyObj.DATE_ADDED + ' DESC',
}; };
let fetchFileResult = await media.getFileAssets(getFileCountOneOp); let fetchFileResult = await media.getFileAssets(getFileCountOneOp);
const fetchCount = fetchFileResult.getCount(); const fetchCount = fetchFileResult.getCount();
console.info('fetchCount result: ' + fetchCount); console.info('fetchCount result: ' + fetchCount);
fetchFileResult.close(); fetchFileResult.close();
} }
``` ```
...@@ -1896,31 +1911,31 @@ Checks whether the cursor is in the last row of the result set. ...@@ -1896,31 +1911,31 @@ Checks whether the cursor is in the last row of the result set.
| Type | Description | | Type | Description |
| ------- | ---------------------------------- | | ------- | ---------------------------------- |
| boolean | Returns **true** if the cursor is in the last row of the result set; returns *false* otherwise.| | boolean | Returns **true** if the cursor is in the last row of the result set; returns **false** otherwise.|
**Example** **Example**
```js ```js
async function example() { async function example() {
let fileKeyObj = mediaLibrary.FileKey; let fileKeyObj = mediaLibrary.FileKey;
let imageType = mediaLibrary.MediaType.IMAGE; let imageType = mediaLibrary.MediaType.IMAGE;
let getImageOp = { let getImageOp = {
selections: fileKeyObj.MEDIA_TYPE + '= ?', selections: fileKeyObj.MEDIA_TYPE + '= ?',
selectionArgs: [imageType.toString()], selectionArgs: [imageType.toString()],
order: fileKeyObj.DATE_ADDED + ' DESC', order: fileKeyObj.DATE_ADDED + ' DESC',
}; };
let fetchFileResult = await media.getFileAssets(getImageOp); let fetchFileResult = await media.getFileAssets(getImageOp);
const fetchCount = fetchFileResult.getCount(); const fetchCount = fetchFileResult.getCount();
console.info('mediaLibrary fetchFileResult.getCount, count:' + fetchCount); console.info('mediaLibrary fetchFileResult.getCount, count:' + fetchCount);
let fileAsset = await fetchFileResult.getFirstObject(); let fileAsset = await fetchFileResult.getFirstObject();
for (var i = 1; i < fetchCount; i++) { for (var i = 1; i < fetchCount; i++) {
fileAsset = await fetchFileResult.getNextObject(); fileAsset = await fetchFileResult.getNextObject();
if(i == fetchCount - 1) { if(i == fetchCount - 1) {
var result = fetchFileResult.isAfterLast(); var result = fetchFileResult.isAfterLast();
console.info('mediaLibrary fileAsset isAfterLast result: ' + result); console.info('mediaLibrary fileAsset isAfterLast result: ' + result);
fetchFileResult.close(); fetchFileResult.close();
}
} }
}
} }
``` ```
...@@ -1928,7 +1943,7 @@ async function example() { ...@@ -1928,7 +1943,7 @@ async function example() {
close(): void close(): void
Releases and invalidates this **FetchFileResult** instance. Other APIs in this instance cannot be invoked after it is released. Releases and invalidates this **FetchFileResult** instance. After this instance is released, the APIs in this instance cannot be invoked.
**System capability**: SystemCapability.Multimedia.MediaLibrary.Core **System capability**: SystemCapability.Multimedia.MediaLibrary.Core
...@@ -1936,15 +1951,15 @@ Releases and invalidates this **FetchFileResult** instance. Other APIs in this i ...@@ -1936,15 +1951,15 @@ Releases and invalidates this **FetchFileResult** instance. Other APIs in this i
```js ```js
async function example() { async function example() {
let fileKeyObj = mediaLibrary.FileKey; let fileKeyObj = mediaLibrary.FileKey;
let imageType = mediaLibrary.MediaType.IMAGE; let imageType = mediaLibrary.MediaType.IMAGE;
let getImageOp = { let getImageOp = {
selections: fileKeyObj.MEDIA_TYPE + '= ?', selections: fileKeyObj.MEDIA_TYPE + '= ?',
selectionArgs: [imageType.toString()], selectionArgs: [imageType.toString()],
order: fileKeyObj.DATE_ADDED + ' DESC', order: fileKeyObj.DATE_ADDED + ' DESC',
}; };
let fetchFileResult = await media.getFileAssets(getImageOp); let fetchFileResult = await media.getFileAssets(getImageOp);
fetchFileResult.close(); fetchFileResult.close();
} }
``` ```
...@@ -1960,28 +1975,28 @@ Obtains the first file asset in the result set. This API uses an asynchronous ca ...@@ -1960,28 +1975,28 @@ Obtains the first file asset in the result set. This API uses an asynchronous ca
| Name | Type | Mandatory| Description | | Name | Type | Mandatory| Description |
| -------- | --------------------------------------------- | ---- | ------------------------------------------- | | -------- | --------------------------------------------- | ---- | ------------------------------------------- |
| callback | AsyncCallback&lt;[FileAsset](#fileasset7)&gt; | Yes | Callback used to return the first file asset.| | callback | AsyncCallback&lt;[FileAsset](#fileasset7)&gt; | Yes | Callback invoked to return the first **FileAsset** object in the result set.|
**Example** **Example**
```js ```js
async function example() { async function example() {
let fileKeyObj = mediaLibrary.FileKey; let fileKeyObj = mediaLibrary.FileKey;
let imageType = mediaLibrary.MediaType.IMAGE; let imageType = mediaLibrary.MediaType.IMAGE;
let getImageOp = { let getImageOp = {
selections: fileKeyObj.MEDIA_TYPE + '= ?', selections: fileKeyObj.MEDIA_TYPE + '= ?',
selectionArgs: [imageType.toString()], selectionArgs: [imageType.toString()],
order: fileKeyObj.DATE_ADDED + ' DESC', order: fileKeyObj.DATE_ADDED + ' DESC',
}; };
let fetchFileResult = await media.getFileAssets(getImageOp); let fetchFileResult = await media.getFileAssets(getImageOp);
fetchFileResult.getFirstObject((error, fileAsset) => { fetchFileResult.getFirstObject((error, fileAsset) => {
if (error) { if (error) {
console.error('fetchFileResult getFirstObject failed with error: ' + error); console.error('fetchFileResult getFirstObject failed with error: ' + error);
return; return;
} }
console.info('getFirstObject successfully, displayName : ' + fileAsset.displayName); console.info('getFirstObject successfully, displayName : ' + fileAsset.displayName);
fetchFileResult.close(); fetchFileResult.close();
}) })
} }
``` ```
...@@ -1997,26 +2012,26 @@ Obtains the first file asset in the result set. This API uses a promise to retur ...@@ -1997,26 +2012,26 @@ Obtains the first file asset in the result set. This API uses a promise to retur
| Type | Description | | Type | Description |
| --------------------------------------- | -------------------------- | | --------------------------------------- | -------------------------- |
| Promise&lt;[FileAsset](#fileasset7)&gt; | Promise used to return the file asset.| | Promise&lt;[FileAsset](#fileasset7)&gt; | Promise used to return the first **FileAsset** object in the result set.|
**Example** **Example**
```js ```js
async function example() { async function example() {
let fileKeyObj = mediaLibrary.FileKey; let fileKeyObj = mediaLibrary.FileKey;
let imageType = mediaLibrary.MediaType.IMAGE; let imageType = mediaLibrary.MediaType.IMAGE;
let getImageOp = { let getImageOp = {
selections: fileKeyObj.MEDIA_TYPE + '= ?', selections: fileKeyObj.MEDIA_TYPE + '= ?',
selectionArgs: [imageType.toString()], selectionArgs: [imageType.toString()],
order: fileKeyObj.DATE_ADDED + ' DESC', order: fileKeyObj.DATE_ADDED + ' DESC',
}; };
let fetchFileResult = await media.getFileAssets(getImageOp); let fetchFileResult = await media.getFileAssets(getImageOp);
fetchFileResult.getFirstObject().then((fileAsset) => { fetchFileResult.getFirstObject().then((fileAsset) => {
console.info('getFirstObject successfully, displayName: ' + fileAsset.displayName); console.info('getFirstObject successfully, displayName: ' + fileAsset.displayName);
fetchFileResult.close(); fetchFileResult.close();
}).catch((error) => { }).catch((error) => {
console.error('getFirstObject failed with error: ' + error); console.error('getFirstObject failed with error: ' + error);
}); });
} }
``` ```
...@@ -2025,9 +2040,8 @@ async function example() { ...@@ -2025,9 +2040,8 @@ async function example() {
getNextObject(callback: AsyncCallback&lt;FileAsset&gt;): void getNextObject(callback: AsyncCallback&lt;FileAsset&gt;): void
Obtains the next file asset in the result set. This API uses an asynchronous callback to return the result. Obtains the next file asset in the result set. This API uses an asynchronous callback to return the result.
> **NOTE**
> > **NOTE**<br>Before using this API, you must use [getFirstObject](#getfirstobject7) to obtain the first file asset and then use [isAfterLast](#isafterlast7) to ensure that the cursor does not point to the last file asset in the result set.
> Before using this API, you must use [getFirstObject](#getfirstobject7) to obtain the first file asset and then use [isAfterLast](#isafterlast7) to ensure that the cursor does not point to the last file asset in the result set.
**System capability**: SystemCapability.Multimedia.MediaLibrary.Core **System capability**: SystemCapability.Multimedia.MediaLibrary.Core
...@@ -2035,32 +2049,32 @@ Obtains the next file asset in the result set. This API uses an asynchronous cal ...@@ -2035,32 +2049,32 @@ Obtains the next file asset in the result set. This API uses an asynchronous cal
| Name | Type | Mandatory| Description | | Name | Type | Mandatory| Description |
| --------- | --------------------------------------------- | ---- | ----------------------------------------- | | --------- | --------------------------------------------- | ---- | ----------------------------------------- |
| callback| AsyncCallback&lt;[FileAsset](#fileasset7)&gt; | Yes | Callback used to return the next file asset.| | callbacke | AsyncCallback&lt;[FileAsset](#fileasset7)&gt; | Yes | Callback invoked to return the next **FileAsset** object in the result set.|
**Example** **Example**
```js ```js
async function example() { async function example() {
let fileKeyObj = mediaLibrary.FileKey; let fileKeyObj = mediaLibrary.FileKey;
let imageType = mediaLibrary.MediaType.IMAGE; let imageType = mediaLibrary.MediaType.IMAGE;
let getImageOp = { let getImageOp = {
selections: fileKeyObj.MEDIA_TYPE + '= ?', selections: fileKeyObj.MEDIA_TYPE + '= ?',
selectionArgs: [imageType.toString()], selectionArgs: [imageType.toString()],
order: fileKeyObj.DATE_ADDED + ' DESC', order: fileKeyObj.DATE_ADDED + ' DESC',
}; };
let fetchFileResult = await media.getFileAssets(getImageOp); let fetchFileResult = await media.getFileAssets(getImageOp);
let fileAsset = await fetchFileResult.getFirstObject(); let fileAsset = await fetchFileResult.getFirstObject();
console.log('fetchFileResult getFirstObject successfully, displayName: ' + fileAsset.displayName); console.log('fetchFileResult getFirstObject successfully, displayName: ' + fileAsset.displayName);
if (!fetchFileResult.isAfterLast()) { if (!fetchFileResult.isAfterLast()) {
fetchFileResult.getNextObject((error, fileAsset) => { fetchFileResult.getNextObject((error, fileAsset) => {
if (error) { if (error) {
console.error('fetchFileResult getNextObject failed with error: ' + error); console.error('fetchFileResult getNextObject failed with error: ' + error);
return; return;
} }
console.log('fetchFileResult getNextObject successfully, displayName: ' + fileAsset.displayName); console.log('fetchFileResult getNextObject successfully, displayName: ' + fileAsset.displayName);
fetchFileResult.close(); fetchFileResult.close();
}) })
} }
} }
``` ```
...@@ -2070,9 +2084,8 @@ async function example() { ...@@ -2070,9 +2084,8 @@ async function example() {
getNextObject(): Promise&lt;FileAsset&gt; getNextObject(): Promise&lt;FileAsset&gt;
Obtains the next file asset in the result set. This API uses a promise to return the result. Obtains the next file asset in the result set. This API uses a promise to return the result.
> **NOTE**
> > **NOTE**<br>Before using this API, you must use [getFirstObject](#getfirstobject7) to obtain the first file asset and then use [isAfterLast](#isafterlast7) to ensure that the cursor does not point to the last file asset in the result set.
> Before using this API, you must use [getFirstObject](#getfirstobject7) to obtain the first file asset and then use [isAfterLast](#isafterlast7) to ensure that the cursor does not point to the last file asset in the result set.
**System capability**: SystemCapability.Multimedia.MediaLibrary.Core **System capability**: SystemCapability.Multimedia.MediaLibrary.Core
...@@ -2080,30 +2093,30 @@ Obtains the next file asset in the result set. This API uses a promise to return ...@@ -2080,30 +2093,30 @@ Obtains the next file asset in the result set. This API uses a promise to return
| Type | Description | | Type | Description |
| --------------------------------------- | ----------------- | | --------------------------------------- | ----------------- |
| Promise&lt;[FileAsset](#fileasset7)&gt; | Promise used to return the next file asset.| | Promise&lt;[FileAsset](#fileasset7)&gt; | Promise used to return the next **FileAsset** object in the result set.|
**Example** **Example**
```js ```js
async function example() { async function example() {
let fileKeyObj = mediaLibrary.FileKey; let fileKeyObj = mediaLibrary.FileKey;
let imageType = mediaLibrary.MediaType.IMAGE; let imageType = mediaLibrary.MediaType.IMAGE;
let getImageOp = { let getImageOp = {
selections: fileKeyObj.MEDIA_TYPE + '= ?', selections: fileKeyObj.MEDIA_TYPE + '= ?',
selectionArgs: [imageType.toString()], selectionArgs: [imageType.toString()],
order: fileKeyObj.DATE_ADDED + ' DESC', order: fileKeyObj.DATE_ADDED + ' DESC',
}; };
let fetchFileResult = await media.getFileAssets(getImageOp); let fetchFileResult = await media.getFileAssets(getImageOp);
let fileAsset = await fetchFileResult.getFirstObject(); let fileAsset = await fetchFileResult.getFirstObject();
console.log('fetchFileResult getFirstObject successfully, displayName: ' + fileAsset.displayName); console.log('fetchFileResult getFirstObject successfully, displayName: ' + fileAsset.displayName);
if (!fetchFileResult.isAfterLast()) { if (!fetchFileResult.isAfterLast()) {
fetchFileResult.getNextObject().then((fileAsset) => { fetchFileResult.getNextObject().then((fileAsset) => {
console.info('fetchFileResult getNextObject successfully, displayName: ' + fileAsset.displayName); console.info('fetchFileResult getNextObject successfully, displayName: ' + fileAsset.displayName);
fetchFileResult.close(); fetchFileResult.close();
}).catch((error) => { }).catch((error) => {
console.error('fetchFileResult getNextObject failed with error: ' + error); console.error('fetchFileResult getNextObject failed with error: ' + error);
}) })
} }
} }
``` ```
...@@ -2119,28 +2132,28 @@ Obtains the last file asset in the result set. This API uses an asynchronous cal ...@@ -2119,28 +2132,28 @@ Obtains the last file asset in the result set. This API uses an asynchronous cal
| Name | Type | Mandatory| Description | | Name | Type | Mandatory| Description |
| -------- | --------------------------------------------- | ---- | --------------------------- | | -------- | --------------------------------------------- | ---- | --------------------------- |
| callback | AsyncCallback&lt;[FileAsset](#fileasset7)&gt; | Yes | Callback used to return the last file asset.| | callback | AsyncCallback&lt;[FileAsset](#fileasset7)&gt; | Yes | Callback invoked to return the last **FileAsset** object in the result set.|
**Example** **Example**
```js ```js
async function example() { async function example() {
let fileKeyObj = mediaLibrary.FileKey; let fileKeyObj = mediaLibrary.FileKey;
let imageType = mediaLibrary.MediaType.IMAGE; let imageType = mediaLibrary.MediaType.IMAGE;
let getImageOp = { let getImageOp = {
selections: fileKeyObj.MEDIA_TYPE + '= ?', selections: fileKeyObj.MEDIA_TYPE + '= ?',
selectionArgs: [imageType.toString()], selectionArgs: [imageType.toString()],
order: fileKeyObj.DATE_ADDED + ' DESC', order: fileKeyObj.DATE_ADDED + ' DESC',
}; };
let fetchFileResult = await media.getFileAssets(getImageOp); let fetchFileResult = await media.getFileAssets(getImageOp);
fetchFileResult.getLastObject((error, fileAsset) => { fetchFileResult.getLastObject((error, fileAsset) => {
if (error) { if (error) {
console.error('getLastObject failed with error: ' + error); console.error('getLastObject failed with error: ' + error);
return; return;
} }
console.info('getLastObject successfully, displayName: ' + fileAsset.displayName); console.info('getLastObject successfully, displayName: ' + fileAsset.displayName);
fetchFileResult.close(); fetchFileResult.close();
}) })
} }
``` ```
...@@ -2156,26 +2169,26 @@ Obtains the last file asset in the result set. This API uses a promise to return ...@@ -2156,26 +2169,26 @@ Obtains the last file asset in the result set. This API uses a promise to return
| Type | Description | | Type | Description |
| --------------------------------------- | ----------------- | | --------------------------------------- | ----------------- |
| Promise&lt;[FileAsset](#fileasset7)&gt; | Promise used to return the next file asset.| | Promise&lt;[FileAsset](#fileasset7)&gt; | Promise used to return the last **FileAsset** object in the result set.|
**Example** **Example**
```js ```js
async function example() { async function example() {
let fileKeyObj = mediaLibrary.FileKey; let fileKeyObj = mediaLibrary.FileKey;
let imageType = mediaLibrary.MediaType.IMAGE; let imageType = mediaLibrary.MediaType.IMAGE;
let getImageOp = { let getImageOp = {
selections: fileKeyObj.MEDIA_TYPE + '= ?', selections: fileKeyObj.MEDIA_TYPE + '= ?',
selectionArgs: [imageType.toString()], selectionArgs: [imageType.toString()],
order: fileKeyObj.DATE_ADDED + ' DESC', order: fileKeyObj.DATE_ADDED + ' DESC',
}; };
let fetchFileResult = await media.getFileAssets(getImageOp); let fetchFileResult = await media.getFileAssets(getImageOp);
fetchFileResult.getLastObject().then((fileAsset) => { fetchFileResult.getLastObject().then((fileAsset) => {
console.info('getLastObject successfully, displayName: ' + fileAsset.displayName); console.info('getLastObject successfully, displayName: ' + fileAsset.displayName);
fetchFileResult.close(); fetchFileResult.close();
}).catch((error) => { }).catch((error) => {
console.error('getLastObject failed with error: ' + error); console.error('getLastObject failed with error: ' + error);
}); });
} }
``` ```
...@@ -2192,28 +2205,28 @@ Obtains a file asset with the specified index in the result set. This API uses a ...@@ -2192,28 +2205,28 @@ Obtains a file asset with the specified index in the result set. This API uses a
| Name | Type | Mandatory | Description | | Name | Type | Mandatory | Description |
| -------- | ---------------------------------------- | ---- | ------------------ | | -------- | ---------------------------------------- | ---- | ------------------ |
| index | number | Yes | Index of the file to obtain. The value starts from 0 and must be smaller than the **count** value of the result set. | | index | number | Yes | Index of the file to obtain. The value starts from 0 and must be smaller than the **count** value of the result set. |
| callback | AsyncCallback&lt;[FileAsset](#fileasset7)&gt; | Yes | Callback used to return the last file asset.| | callback | AsyncCallback&lt;[FileAsset](#fileasset7)&gt; | Yes | Callback invoked to return the **FileAsset** object with the specified index obtained.|
**Example** **Example**
```js ```js
async function example() { async function example() {
let fileKeyObj = mediaLibrary.FileKey; let fileKeyObj = mediaLibrary.FileKey;
let imageType = mediaLibrary.MediaType.IMAGE; let imageType = mediaLibrary.MediaType.IMAGE;
let getImageOp = { let getImageOp = {
selections: fileKeyObj.MEDIA_TYPE + '= ?', selections: fileKeyObj.MEDIA_TYPE + '= ?',
selectionArgs: [imageType.toString()], selectionArgs: [imageType.toString()],
order: fileKeyObj.DATE_ADDED + ' DESC', order: fileKeyObj.DATE_ADDED + ' DESC',
}; };
let fetchFileResult = await media.getFileAssets(getImageOp); let fetchFileResult = await media.getFileAssets(getImageOp);
fetchFileResult.getPositionObject(0, (error, fileAsset) => { fetchFileResult.getPositionObject(0, (error, fileAsset) => {
if (error) { if (error) {
console.error('getPositionObject failed with error: ' + error); console.error('getPositionObject failed with error: ' + error);
return; return;
} }
console.info('getPositionObject successfully, displayName: ' + fileAsset.displayName); console.info('getPositionObject successfully, displayName: ' + fileAsset.displayName);
fetchFileResult.close(); fetchFileResult.close();
}) })
} }
``` ```
...@@ -2235,26 +2248,26 @@ Obtains a file asset with the specified index in the result set. This API uses a ...@@ -2235,26 +2248,26 @@ Obtains a file asset with the specified index in the result set. This API uses a
| Type | Description | | Type | Description |
| --------------------------------------- | ----------------- | | --------------------------------------- | ----------------- |
| Promise&lt;[FileAsset](#fileasset7)&gt; | Promise used to return the next file asset.| | Promise&lt;[FileAsset](#fileasset7)&gt; | Promise used to return the **FileAsset** object with the specified index obtained.|
**Example** **Example**
```js ```js
async function example() { async function example() {
let fileKeyObj = mediaLibrary.FileKey; let fileKeyObj = mediaLibrary.FileKey;
let imageType = mediaLibrary.MediaType.IMAGE; let imageType = mediaLibrary.MediaType.IMAGE;
let getImageOp = { let getImageOp = {
selections: fileKeyObj.MEDIA_TYPE + '= ?', selections: fileKeyObj.MEDIA_TYPE + '= ?',
selectionArgs: [imageType.toString()], selectionArgs: [imageType.toString()],
order: fileKeyObj.DATE_ADDED + ' DESC', order: fileKeyObj.DATE_ADDED + ' DESC',
}; };
let fetchFileResult = await media.getFileAssets(getImageOp); let fetchFileResult = await media.getFileAssets(getImageOp);
fetchFileResult.getPositionObject(0).then((fileAsset) => { fetchFileResult.getPositionObject(0).then((fileAsset) => {
console.info('getPositionObject successfully, displayName: ' + fileAsset.displayName); console.info('getPositionObject successfully, displayName: ' + fileAsset.displayName);
fetchFileResult.close(); fetchFileResult.close();
}).catch((error) => { }).catch((error) => {
console.error('getPositionObject failed with error: ' + error); console.error('getPositionObject failed with error: ' + error);
}); });
} }
``` ```
...@@ -2270,30 +2283,30 @@ Obtains all the file assets in the result set. This API uses an asynchronous cal ...@@ -2270,30 +2283,30 @@ Obtains all the file assets in the result set. This API uses an asynchronous cal
| Name | Type | Mandatory | Description | | Name | Type | Mandatory | Description |
| -------- | ---------------------------------------- | ---- | -------------------- | | -------- | ---------------------------------------- | ---- | -------------------- |
| callback | AsyncCallback&lt;Array&lt;[FileAsset](#fileasset7)&gt;&gt; | Yes | Callback used to return the file assets.| | callback | AsyncCallback&lt;Array&lt;[FileAsset](#fileasset7)&gt;&gt; | Yes | Callback invoked to return all the **FileAsset** objects in the result reset.|
**Example** **Example**
```js ```js
async function example() { async function example() {
let fileKeyObj = mediaLibrary.FileKey; let fileKeyObj = mediaLibrary.FileKey;
let imageType = mediaLibrary.MediaType.IMAGE; let imageType = mediaLibrary.MediaType.IMAGE;
let getImageOp = { let getImageOp = {
selections: fileKeyObj.MEDIA_TYPE + '= ?', selections: fileKeyObj.MEDIA_TYPE + '= ?',
selectionArgs: [imageType.toString()], selectionArgs: [imageType.toString()],
order: fileKeyObj.DATE_ADDED + ' DESC', order: fileKeyObj.DATE_ADDED + ' DESC',
}; };
let fetchFileResult = await media.getFileAssets(getImageOp); let fetchFileResult = await media.getFileAssets(getImageOp);
fetchFileResult.getAllObject((error, fileAssetList) => { fetchFileResult.getAllObject((error, fileAssetList) => {
if (error) { if (error) {
console.error('getAllObject failed with error: ' + error); console.error('getAllObject failed with error: ' + error);
return; return;
} }
for (let i = 0; i < fetchFileResult.getCount(); i++) { for (let i = 0; i < fetchFileResult.getCount(); i++) {
console.info('getAllObject fileAssetList ' + i + ' displayName: ' + fileAssetList[i].displayName); console.info('getAllObject fileAssetList ' + i + ' displayName: ' + fileAssetList[i].displayName);
} }
fetchFileResult.close(); fetchFileResult.close();
}) })
} }
``` ```
...@@ -2309,34 +2322,34 @@ Obtains all the file assets in the result set. This API uses a promise to return ...@@ -2309,34 +2322,34 @@ Obtains all the file assets in the result set. This API uses a promise to return
| Type | Description | | Type | Description |
| ---------------------------------------- | --------------------- | | ---------------------------------------- | --------------------- |
| Promise&lt;Array&lt;[FileAsset](#fileasset7)&gt;&gt; | Promise used to return the file assets.| | Promise&lt;Array&lt;[FileAsset](#fileasset7)&gt;&gt; | Promise used to return all the **FileAsset** objects in the result reset.|
**Example** **Example**
```js ```js
async function example() { async function example() {
let fileKeyObj = mediaLibrary.FileKey; let fileKeyObj = mediaLibrary.FileKey;
let imageType = mediaLibrary.MediaType.IMAGE; let imageType = mediaLibrary.MediaType.IMAGE;
let getImageOp = { let getImageOp = {
selections: fileKeyObj.MEDIA_TYPE + '= ?', selections: fileKeyObj.MEDIA_TYPE + '= ?',
selectionArgs: [imageType.toString()], selectionArgs: [imageType.toString()],
order: fileKeyObj.DATE_ADDED + ' DESC', order: fileKeyObj.DATE_ADDED + ' DESC',
}; };
let fetchFileResult = await media.getFileAssets(getImageOp); let fetchFileResult = await media.getFileAssets(getImageOp);
fetchFileResult.getAllObject().then((fileAssetList) => { fetchFileResult.getAllObject().then((fileAssetList) => {
for (let i = 0; i < fetchFileResult.getCount(); i++) { for (let i = 0; i < fetchFileResult.getCount(); i++) {
console.info('getAllObject fileAssetList ' + i + ' displayName: ' + fileAssetList[i].displayName); console.info('getAllObject fileAssetList ' + i + ' displayName: ' + fileAssetList[i].displayName);
} }
fetchFileResult.close(); fetchFileResult.close();
}).catch((error) => { }).catch((error) => {
console.error('getAllObject failed with error: ' + error); console.error('getAllObject failed with error: ' + error);
}); });
} }
``` ```
## Album<sup>7+</sup> ## Album<sup>7+</sup>
Provides APIs to implement a physical album. Provides APIs to manage albums.
### Attributes ### Attributes
...@@ -2345,9 +2358,9 @@ Provides APIs to implement a physical album. ...@@ -2345,9 +2358,9 @@ Provides APIs to implement a physical album.
| Name | Type | Readable | Writable | Description | | Name | Type | Readable | Writable | Description |
| ------------ | ------ | ---- | ---- | ------- | | ------------ | ------ | ---- | ---- | ------- |
| albumId | number | Yes | No | Album ID. | | albumId | number | Yes | No | Album ID. |
| albumName | string | Yes | Yes | Album name. | | albumName | string | Yes | Yes | Name of the album. |
| albumUri<sup>8+</sup> | string | Yes | No | Album URI. | | albumUri<sup>8+</sup> | string | Yes | No | URI of the album. |
| dateModified | number | Yes | No | Date when the album was modified. | | dateModified | number | Yes | No | Date when the album was last modified. |
| count<sup>8+</sup> | number | Yes | No | Number of files in the album.| | count<sup>8+</sup> | number | Yes | No | Number of files in the album.|
| relativePath<sup>8+</sup> | string | Yes | No | Relative path of the album. | | relativePath<sup>8+</sup> | string | Yes | No | Relative path of the album. |
| coverUri<sup>8+</sup> | string | Yes | No | URI of the cover file of the album.| | coverUri<sup>8+</sup> | string | Yes | No | URI of the cover file of the album.|
...@@ -2356,7 +2369,7 @@ Provides APIs to implement a physical album. ...@@ -2356,7 +2369,7 @@ Provides APIs to implement a physical album.
commitModify(callback: AsyncCallback&lt;void&gt;): void commitModify(callback: AsyncCallback&lt;void&gt;): void
Commits the modification in the album attributes to the database. This API uses an asynchronous callback to return the result. Commits the modification on the album attributes to the database.
**Required permissions**: ohos.permission.READ_MEDIA and ohos.permission.WRITE_MEDIA **Required permissions**: ohos.permission.READ_MEDIA and ohos.permission.WRITE_MEDIA
...@@ -2366,7 +2379,7 @@ Commits the modification in the album attributes to the database. This API uses ...@@ -2366,7 +2379,7 @@ Commits the modification in the album attributes to the database. This API uses
| Name | Type | Mandatory| Description | | Name | Type | Mandatory| Description |
| -------- | ------------------------- | ---- | ---------- | | -------- | ------------------------- | ---- | ---------- |
| callback | AsyncCallback&lt;void&gt; | Yes | Void callback.| | callback | AsyncCallback&lt;void&gt; | Yes | Callback that returns no value.|
**Example** **Example**
...@@ -2386,7 +2399,7 @@ async function example() { ...@@ -2386,7 +2399,7 @@ async function example() {
return; return;
} }
console.info('commitModify successful.'); console.info('commitModify successful.');
}) });
} }
``` ```
...@@ -2394,7 +2407,7 @@ async function example() { ...@@ -2394,7 +2407,7 @@ async function example() {
commitModify(): Promise&lt;void&gt; commitModify(): Promise&lt;void&gt;
Commits the modification in the album attributes to the database. This API uses a promise to return the result. Commits the modification on the album attributes to the database.
**Required permissions**: ohos.permission.READ_MEDIA and ohos.permission.WRITE_MEDIA **Required permissions**: ohos.permission.READ_MEDIA and ohos.permission.WRITE_MEDIA
...@@ -2404,7 +2417,7 @@ Commits the modification in the album attributes to the database. This API uses ...@@ -2404,7 +2417,7 @@ Commits the modification in the album attributes to the database. This API uses
| Type | Description | | Type | Description |
| ------------------- | ------------ | | ------------------- | ------------ |
| Promise&lt;void&gt; | Void promise.| | Promise&lt;void&gt; | Promise that returns no value.|
**Example** **Example**
...@@ -2440,7 +2453,7 @@ Obtains the file assets in this album. This API uses an asynchronous callback to ...@@ -2440,7 +2453,7 @@ Obtains the file assets in this album. This API uses an asynchronous callback to
| Name | Type | Mandatory| Description | | Name | Type | Mandatory| Description |
| -------- | --------------------------------------------------- | ---- | ----------------------------------- | | -------- | --------------------------------------------------- | ---- | ----------------------------------- |
| callback | AsyncCallback<[FetchFileResult](#fetchfileresult7)> | Yes | Callback used to return the file assets.| | callback | AsyncCallback<[FetchFileResult](#fetchfileresult7)> | Yes | Callback invoked to return the file retrieval result set of the album.|
**Example** **Example**
...@@ -2482,7 +2495,7 @@ Obtains the file assets in this album. This API uses an asynchronous callback to ...@@ -2482,7 +2495,7 @@ Obtains the file assets in this album. This API uses an asynchronous callback to
| Name | Type | Mandatory| Description | | Name | Type | Mandatory| Description |
| -------- | --------------------------------------------------- | ---- | ----------------------------------- | | -------- | --------------------------------------------------- | ---- | ----------------------------------- |
| options | [MediaFetchOptions](#mediafetchoptions7) | Yes | Options for fetching the files. | | options | [MediaFetchOptions](#mediafetchoptions7) | Yes | Options for fetching the files. |
| callback | AsyncCallback<[FetchFileResult](#fetchfileresult7)> | Yes | Callback used to return the file assets.| | callback | AsyncCallback<[FetchFileResult](#fetchfileresult7)> | Yes | Callback invoked to return the file retrieval result set of the album.|
**Example** **Example**
...@@ -2496,7 +2509,7 @@ async function example() { ...@@ -2496,7 +2509,7 @@ async function example() {
let fileNoArgsfetchOp = { let fileNoArgsfetchOp = {
selections: '', selections: '',
selectionArgs: [], selectionArgs: [],
} };
// Obtain the albums that meet the retrieval options and return the album list. // Obtain the albums that meet the retrieval options and return the album list.
const albumList = await media.getAlbums(AlbumNoArgsfetchOp); const albumList = await media.getAlbums(AlbumNoArgsfetchOp);
const album = albumList[0]; const album = albumList[0];
...@@ -2533,7 +2546,7 @@ Obtains the file assets in this album. This API uses a promise to return the res ...@@ -2533,7 +2546,7 @@ Obtains the file assets in this album. This API uses a promise to return the res
| Type | Description | | Type | Description |
| --------------------------------------------- | ------------------------- | | --------------------------------------------- | ------------------------- |
| Promise<[FetchFileResult](#fetchfileresult7)> | Promise used to return the file assets.| | Promise<[FetchFileResult](#fetchfileresult7)> | Promise used to return the file retrieval result set of the album.|
**Example** **Example**
...@@ -2564,7 +2577,7 @@ async function example() { ...@@ -2564,7 +2577,7 @@ async function example() {
## PeerInfo<sup>8+</sup> ## PeerInfo<sup>8+</sup>
Describes information about a registered device. Defines information about a registered device.
**System API**: This is a system API. **System API**: This is a system API.
...@@ -2577,8 +2590,6 @@ Describes information about a registered device. ...@@ -2577,8 +2590,6 @@ Describes information about a registered device.
| deviceType | [DeviceType](#devicetype8) | Yes | No | Type of the registered device. | | deviceType | [DeviceType](#devicetype8) | Yes | No | Type of the registered device. |
| isOnline | boolean | Yes | No | Whether the registered device is online. | | isOnline | boolean | Yes | No | Whether the registered device is online. |
## MediaType<sup>8+</sup> ## MediaType<sup>8+</sup>
Enumerates media types. Enumerates media types.
...@@ -2604,8 +2615,8 @@ Enumerates key file information. ...@@ -2604,8 +2615,8 @@ Enumerates key file information.
| Name | Value | Description | | Name | Value | Description |
| ------------- | ------------------- | ---------------------------------------------------------- | | ------------- | ------------------- | ---------------------------------------------------------- |
| ID | 'file_id' | File ID. | | ID | 'file_id' | File ID. |
| RELATIVE_PATH | 'relative_path' | Relative public directory of the file. | | RELATIVE_PATH | 'relative_path' | Relative path of the user directory. |
| DISPLAY_NAME | 'display_name' | Display file name. | | DISPLAY_NAME | 'display_name' | File name displayed. |
| PARENT | 'parent' | Parent directory ID. | | PARENT | 'parent' | Parent directory ID. |
| MIME_TYPE | 'mime_type' | Extended file attributes, such as image/, video/, and file/*. | | MIME_TYPE | 'mime_type' | Extended file attributes, such as image/, video/, and file/*. |
| MEDIA_TYPE | 'media_type' | Media type. | | MEDIA_TYPE | 'media_type' | Media type. |
...@@ -2640,7 +2651,7 @@ Enumerates directory types. ...@@ -2640,7 +2651,7 @@ Enumerates directory types.
## DeviceType<sup>8+</sup> ## DeviceType<sup>8+</sup>
Enumerates device types. Enumerates the device types.
**System API**: This is a system API. **System API**: This is a system API.
...@@ -2658,22 +2669,22 @@ Enumerates device types. ...@@ -2658,22 +2669,22 @@ Enumerates device types.
## MediaFetchOptions<sup>7+</sup> ## MediaFetchOptions<sup>7+</sup>
Describes options for fetching media files. Defines the options for fetching media files.
**System capability**: SystemCapability.Multimedia.MediaLibrary.Core **System capability**: SystemCapability.Multimedia.MediaLibrary.Core
| Name | Type | Readable| Writable| Description | | Name | Type | Readable| Writable| Description |
| ----------------------- | ------------------- | ---- | ---- | ------------------------------------------------------------ | | ----------------------- | ------------------- | ---- | ---- | ------------------------------------------------------------ |
| selections | string | Yes | Yes | Conditions for fetching files. The enumerated values in [FileKey](#filekey8) are used as the column names of the conditions. Example:<br>selections: mediaLibrary.FileKey.MEDIA_TYPE + '= ? OR ' + mediaLibrary.FileKey.MEDIA_TYPE + '= ?', | | selections | string | Yes | Yes | Conditions for fetching files. The enumerated values in [FileKey](#filekey8) are used as the column names when files are fetched. Example:<br>selections: mediaLibrary.FileKey.MEDIA_TYPE + '= ? OR ' + mediaLibrary.FileKey.MEDIA_TYPE + '= ?', |
| selectionArgs | Array&lt;string&gt; | Yes | Yes | Value of the condition, which corresponds to the value of the condition column in **selections**.<br>Example:<br>selectionArgs: [mediaLibrary.MediaType.IMAGE.toString(), mediaLibrary.MediaType.VIDEO.toString()], | | selectionArgs | Array&lt;string&gt; | Yes | Yes | Values of the conditions specified in **selections**.<br>Example:<br>selectionArgs: [mediaLibrary.MediaType.IMAGE.toString(), mediaLibrary.MediaType.VIDEO.toString()], |
| order | string | Yes | Yes | Sorting mode of the search results, which can be ascending or descending. The enumerated values in [FileKey](#filekey8) are used as the columns for sorting the search results. Example:<br>Ascending: order: mediaLibrary.FileKey.DATE_ADDED + ' ASC'<br>Descending: order: mediaLibrary.FileKey.DATE_ADDED + ' DESC'| | order | string | Yes | Yes | Sorting mode of the search results, which can be ascending or descending. The enumerated values in [FileKey](#filekey8) are used as the columns for sorting the search results. Example:<br>Ascending: order: mediaLibrary.FileKey.DATE_ADDED + ' ASC'<br>Descending: order: mediaLibrary.FileKey.DATE_ADDED + ' DESC'|
| uri<sup>8+</sup> | string | Yes | Yes | File URI. | | uri<sup>8+</sup> | string | Yes | Yes | URI of the file. |
| networkId<sup>8+</sup> | string | Yes | Yes | Network ID of the registered device. | | networkId<sup>8+</sup> | string | Yes | Yes | Network ID of the registered device. |
| extendArgs<sup>8+</sup> | string | Yes | Yes | Extended parameters for fetching the files. Currently, no extended parameters are available. | | extendArgs<sup>8+</sup> | string | Yes | Yes | Extended parameters for fetching the files. Currently, no extended parameters are available. |
## Size<sup>8+</sup> ## Size<sup>8+</sup>
Describes the image size. Defines the image size.
**System capability**: SystemCapability.Multimedia.MediaLibrary.Core **System capability**: SystemCapability.Multimedia.MediaLibrary.Core
...@@ -2684,20 +2695,19 @@ Describes the image size. ...@@ -2684,20 +2695,19 @@ Describes the image size.
## MediaAssetOption ## MediaAssetOption
Implements the media asset option. Defines the media asset option.
**System capability**: SystemCapability.Multimedia.MediaLibrary.Core **System capability**: SystemCapability.Multimedia.MediaLibrary.Core
| Name | Type | Readable| Writable| Description | | Name | Type | Readable| Writable| Description |
| ------------ | ------ | ---- | ---- | ------------------------------------------------------------ | | ------------ | ------ | ---- | ---- | ------------------------------------------------------------ |
| src | string | Yes | Yes | Application sandbox oath of the local file. | | src | string | Yes | Yes | Application sandbox oath of the local file. |
| mimeType | string | Yes | Yes | Multipurpose Internet Mail Extensions (MIME) type of the media.<br>The value can be 'image/\*', 'video/\*', 'audio/\*' or 'file\*'.| | mimeType | string | Yes | Yes | Multipurpose Internet Mail Extensions (MIME) type of the media.<br>The value can be 'image/\*', 'video/\*', 'audio/\*' or 'file\*'.|
| relativePath | string | Yes | Yes | Custom path for storing media assets, for example, 'Pictures/'. If this parameter is unspecified, media assets are stored in the default path.<br> Default path of images: 'Pictures/'<br> Default path of videos: 'Videos/'<br> Default path of audios: 'Audios/'<br> Default path of files: 'Documents/'| | relativePath | string | Yes | Yes | Custom path of media assets, for example, **Pictures/**. If this parameter is unspecified, media assets are stored in the default path.<br> Default path of images: **'Pictures/'**<br> Default path of videos: **'Videos/'**<br> Default path of audios: **'Audios/'**<br> Default path of documents: **'Documents/'**|
## MediaSelectOption ## MediaSelectOption
Describes media selection option. Defines media selection option.
**System capability**: SystemCapability.Multimedia.MediaLibrary.Core **System capability**: SystemCapability.Multimedia.MediaLibrary.Core
......
...@@ -903,7 +903,7 @@ async function example() { ...@@ -903,7 +903,7 @@ async function example() {
deleteAssets(uriList: Array&lt;string&gt;, callback: AsyncCallback&lt;void&gt;): void; deleteAssets(uriList: Array&lt;string&gt;, callback: AsyncCallback&lt;void&gt;): void;
Deletes media files. This API uses an asynchronous callback to return the result. The deleted files are moved to the recycle bin. Deletes media files. This API uses an asynchronous callback to return the result. The deleted files are moved to the trash.
**System API**: This is a system API. **System API**: This is a system API.
...@@ -964,7 +964,7 @@ async function example() { ...@@ -964,7 +964,7 @@ async function example() {
deleteAssets(uriList: Array&lt;string&gt;): Promise&lt;void&gt;; deleteAssets(uriList: Array&lt;string&gt;): Promise&lt;void&gt;;
Deletes media files. This API uses a promise to return the result. The deleted files are moved to the recycle bin. Deletes media files. This API uses a promise to return the result. The deleted files are moved to the trash.
**System API**: This is a system API. **System API**: This is a system API.
...@@ -1159,7 +1159,7 @@ async function example() { ...@@ -1159,7 +1159,7 @@ async function example() {
createDeleteRequest(uriList: Array&lt;string&gt;, callback: AsyncCallback&lt;void&gt;): void; createDeleteRequest(uriList: Array&lt;string&gt;, callback: AsyncCallback&lt;void&gt;): void;
Creates a dialog box for deleting photos. This API uses an asynchronous callback to return the result. The deleted photos are moved to the recycle bin. Creates a dialog box for deleting photos. This API uses an asynchronous callback to return the result. The deleted photos are moved to the trash.
**Required permissions**: ohos.permission.WRITE_IMAGEVIDEO **Required permissions**: ohos.permission.WRITE_IMAGEVIDEO
...@@ -1217,7 +1217,7 @@ async function example() { ...@@ -1217,7 +1217,7 @@ async function example() {
createDeleteRequest(uriList: Array&lt;string&gt;): Promise&lt;void&gt;; createDeleteRequest(uriList: Array&lt;string&gt;): Promise&lt;void&gt;;
Creates a dialog box for deleting photos. This API uses a promise to return the result. The deleted photos are moved to the recycle bin. Creates a dialog box for deleting photos. This API uses a promise to return the result. The deleted photos are moved to the trash.
**Required permissions**: ohos.permission.WRITE_IMAGEVIDEO **Required permissions**: ohos.permission.WRITE_IMAGEVIDEO
...@@ -3258,7 +3258,7 @@ async function example() { ...@@ -3258,7 +3258,7 @@ async function example() {
recoverAssets(assets: Array&lt;PhotoAsset&gt;, callback: AsyncCallback&lt;void&gt;): void; recoverAssets(assets: Array&lt;PhotoAsset&gt;, callback: AsyncCallback&lt;void&gt;): void;
Recovers image or video assets from the recycle bin. Before the operation, ensure that the image or video assets exist in the recycle bin. This API uses an asynchronous callback to return the result. Recovers image or video assets from the trash. Before the operation, ensure that the image or video assets exist in the trash. This API uses an asynchronous callback to return the result.
**System API**: This is a system API. **System API**: This is a system API.
...@@ -3316,7 +3316,7 @@ async function example() { ...@@ -3316,7 +3316,7 @@ async function example() {
recoverAssets(assets: Array&lt;PhotoAsset&gt;): Promise&lt;void&gt;; recoverAssets(assets: Array&lt;PhotoAsset&gt;): Promise&lt;void&gt;;
Recovers image or video assets from the recycle bin. Before the operation, ensure that the image or video assets exist in the recycle bin. This API uses a promise to return the result. Recovers image or video assets from the trash. Before the operation, ensure that the image or video assets exist in the trash. This API uses a promise to return the result.
**System API**: This is a system API. **System API**: This is a system API.
...@@ -3377,7 +3377,7 @@ async function example() { ...@@ -3377,7 +3377,7 @@ async function example() {
deleteAssets(assets: Array&lt;PhotoAsset&gt;, callback: AsyncCallback&lt;void&gt;): void; deleteAssets(assets: Array&lt;PhotoAsset&gt;, callback: AsyncCallback&lt;void&gt;): void;
Deletes image or video assets from the recycle bin. Before the operation, ensure that the image or video assets exist in the recycle bin. This API uses an asynchronous callback to return the result. Deletes image or video assets from the trash. Before the operation, ensure that the image or video assets exist in the trash. This API uses an asynchronous callback to return the result.
**CAUTION**<br>This operation is irreversible. The file assets deleted cannot be restored. Exercise caution when performing this operation. **CAUTION**<br>This operation is irreversible. The file assets deleted cannot be restored. Exercise caution when performing this operation.
...@@ -3437,7 +3437,7 @@ async function example() { ...@@ -3437,7 +3437,7 @@ async function example() {
deleteAssets(assets: Array&lt;PhotoAsset&gt;): Promise&lt;void&gt;; deleteAssets(assets: Array&lt;PhotoAsset&gt;): Promise&lt;void&gt;;
Deletes image or video assets from the recycle bin. Before the operation, ensure that the image or video assets exist in the recycle bin. This API uses a promise to return the result. Deletes image or video assets from the trash. Before the operation, ensure that the image or video assets exist in the trash. This API uses a promise to return the result.
**CAUTION**<br>This operation is irreversible. The file assets deleted cannot be restored. Exercise caution when performing this operation. **CAUTION**<br>This operation is irreversible. The file assets deleted cannot be restored. Exercise caution when performing this operation.
...@@ -3568,7 +3568,7 @@ Enumerate the album subtypes. ...@@ -3568,7 +3568,7 @@ Enumerate the album subtypes.
| FAVORITE | 1025 | Favorites.| | FAVORITE | 1025 | Favorites.|
| VIDEO | 1026 | Video album.| | VIDEO | 1026 | Video album.|
| HIDDEN | 1027 | Hidden album. **System API**: This is a system API.| | HIDDEN | 1027 | Hidden album. **System API**: This is a system API.|
| TRASH | 1028 | Recycle bin. **System API**: This is a system API.| | TRASH | 1028 | Trash. **System API**: This is a system API.|
| SCREENSHOT | 1029 | Album for screenshots and screen recording files. **System API**: This is a system API.| | SCREENSHOT | 1029 | Album for screenshots and screen recording files. **System API**: This is a system API.|
| CAMERA | 1030 | Album for photos and videos taken by the camera. **System API**: This is a system API.| | CAMERA | 1030 | Album for photos and videos taken by the camera. **System API**: This is a system API.|
| ANY | 2147483647 | Any album.| | ANY | 2147483647 | Any album.|
......
# File Subsystem Changelog
## cl.file.1 Change of the mediaLibrary Interface Compatibility
Changed the compatibility of some **mediaLibrary** APIs.
**Change Impact**
The compatibility of some [mediaLibrary](../../../application-dev/reference/apis/js-apis-medialibrary.md) APIs is changed.
For applications developed based on earlier versions, pay attention to the iterative update of deprecated APIs.
**Key API/Component Changes**
| Module | Method/Attribute/Enum/Constant | Change Type|
| ------------------------- | ------------------------------------------------------------ | -------- |
| medialibrary | **function** getFileAssets(options: MediaFetchOptions, callback: AsyncCallback&lt;FetchFileResult&gt;): void | Interface compatibility changed |
| medialibrary | **function** getFileAssets(options: MediaFetchOptions): Promise&lt;FetchFileResult&gt; | Interface compatibility changed |
| medialibrary | **function** createAsset(mediaType: MediaType, displayName: string, relativePath: string, callback: AsyncCallback&lt;FileAsset&gt;): void| Interface compatibility changed |
| medialibrary | **function** createAsset(mediaType: MediaType, displayName: string, relativePath: string): Promise&lt;FileAsset&gt;| Interface compatibility changed |
| medialibrary | **function** getAlbums(options: MediaFetchOptions, callback: AsyncCallback&lt;Array&lt;Album&gt;&gt;): void | Interface compatibility changed |
| medialibrary | **function** getAlbums(options: MediaFetchOptions): Promise&lt;Array&lt;Album&gt;&gt; | Interface compatibility changed |
| medialibrary | **function** FileAsset.commitModify(callback: AsyncCallback&lt;void&gt;): void | Interface compatibility changed |
| medialibrary | **function** FileAsset.commitModify(): Promise&lt;void&gt; | Interface compatibility changed |
**Adaptation Guide**
**getFileAssets**
From API version 10, the albums represented by physical directories are replaced by logical albums, which allows multiple files in an album and presence of a file in multiple albums. This design, however, makes **parent**, **albumId**, **albumUri**, and **albumName** incompatible. They cannot be used as parameters of **MediaFetchOptions** in **getFileAssets()**. See the following example:
1. Use [getMediaLibrary](../../../application-dev/reference/apis/js-apis-medialibrary.md#medialibrarygetmedialibrary) to obtain a **MediaLibrary** instance.
2. Use [MediaFetchOptions](../../../application-dev/reference/apis/js-apis-medialibrary.md#mediafetchoptions7) to create the file fetching options.
3. Use [getFileAssets](../../../application-dev/reference/apis/js-apis-medialibrary.md#getfileassets7) to obtain file assets.
**Incorrect code example**:
```js
import mediaLibrary from '@ohos.multimedia.mediaLibrary';
async function example() {
try {
let context = getContext(this);
let media = mediaLibrary.getMediaLibrary(context);
let fileKeyObj = mediaLibrary.FileKey;
let albumId = 1;
let getImageOp = {
selections: fileKeyObj.ALBUM_ID + '= ?', // File assets cannot be obtained based on the parent, albumId, albumUri, and albumName attributes.
selectionArgs: [albumId.toString()],
};
const fetchFileResult = await media.getFileAssets(getImageOp); // The obtained fetchFileResult is empty.
const fileAsset = await fetchFileResult.getFirstObject();
console.info('mediaLibrary fileAsset displayName: ' + fileAsset.displayName);
} catch (err) {
console.error('mediaLibrary fail, err: ' + err);
}
}
```
Use **getFileAssets()** as follows:
**Correct code example**:
```js
import mediaLibrary from '@ohos.multimedia.mediaLibrary';
async function example() {
try {
let context = getContext(this);
let media = mediaLibrary.getMediaLibrary(context);
let fileKeyObj = mediaLibrary.FileKey;
let imageType = mediaLibrary.MediaType.IMAGE;
let getImageOp = {
selections: fileKeyObj.MEDIA_TYPE + '= ?',
selectionArgs: [imageType.toString()], // Query all files of the image type.
};
const fetchFileResult = await media.getFileAssets(getImageOp);
const fileAsset = await fetchFileResult.getFirstObject();
console.info('mediaLibrary fileAsset displayName: ' + fileAsset.displayName);
} catch (err) {
console.error('mediaLibrary fail, err: ' + err);
}
}
```
**createAsset**
From the SDK of API version 10, **relativePath** is no longer associated with an album. After a file is created, the last-level directory of **relativePath** is not displayed as an album.
**getAlbums**
From the SDK of API version 10, **relativePath** is no longer associated with an album. Therefore, **relativePath** cannot be used as a search criterion in **getAlbums**, and the values of **ALBUM_NAME** can be **Camera** and **Screenshots** only. See the following example:
1. Use [getMediaLibrary](../../../application-dev/reference/apis/js-apis-medialibrary.md#medialibrarygetmedialibrary) to obtain a **MediaLibrary** instance.
2. Use [MediaFetchOptions](../../../application-dev/reference/apis/js-apis-medialibrary.md#mediafetchoptions7) to create the album fetching options.
3. Use [getAlbums](../../../application-dev/reference/apis/js-apis-medialibrary.md#getalbums7) to obtain albums.
**Incorrect code example**:
```js
import mediaLibrary from '@ohos.multimedia.mediaLibrary';
async function example() {
try {
let context = getContext(this);
let media = mediaLibrary.getMediaLibrary(context);
let AlbumNoArgsfetchOp = {
selections: mediaLibrary.FileKey.ALBUM_NAME + ' = ?',
selectionArgs:['New album 1'], //Obtain the album named New album 1.
};
const albumList = await media.getAlbums(AlbumNoArgsfetchOp); // The fetchFileResult returned is empty.
for (let i = 0; i < albumList.length; i++) {
console.info('mediaLibrary album albumName: ' + albumList[i].albumName);
}
} catch (err) {
console.error('mediaLibrary fail, err: ' + err);
}
}
```
The following example shows how to obtain **Camera** and **Screenshots** albums:
**Correct code example**:
```js
import mediaLibrary from '@ohos.multimedia.mediaLibrary';
async function example() {
try {
let context = getContext(this);
let media = mediaLibrary.getMediaLibrary(context);
let AlbumNoArgsfetchOp = {
selections: mediaLibrary.FileKey.ALBUM_NAME + ' = ? OR ' + mediaLibrary.FileKey.ALBUM_NAME + ' = ?',
selectionArgs: ['Camera', 'Screenshots'], // Obtain the camera and screenshot albums.
};
const albumList = await media.getAlbums(AlbumNoArgsfetchOp);
for (let i = 0; i < albumList.length; i++) {
console.info('mediaLibrary album albumName: ' + albumList[i].albumName);
}
} catch (err) {
console.error('mediaLibrary fail, err: ' + err);
}
}
```
**FileAsset.commitModify**
The **orientation** attribute for audio is deleted from the SDK of API version 10. When **commitModify** is used, the **orientation** attribute of audio resources cannot be modified.
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册