1e41f4b71Sopenharmony_ci# File Subsystem Changelog 2e41f4b71Sopenharmony_ci 3e41f4b71Sopenharmony_ci## cl.file.1 Change of the mediaLibrary Interface Compatibility 4e41f4b71Sopenharmony_ci 5e41f4b71Sopenharmony_ciChanged the compatibility of some **mediaLibrary** APIs. 6e41f4b71Sopenharmony_ci 7e41f4b71Sopenharmony_ci**Change Impact** 8e41f4b71Sopenharmony_ci 9e41f4b71Sopenharmony_ciThe compatibility of some [mediaLibrary](../../../application-dev/reference/apis/js-apis-medialibrary.md) APIs is changed. 10e41f4b71Sopenharmony_ciFor applications developed based on earlier versions, pay attention to the iterative update of deprecated APIs. 11e41f4b71Sopenharmony_ci 12e41f4b71Sopenharmony_ci**Key API/Component Changes** 13e41f4b71Sopenharmony_ci 14e41f4b71Sopenharmony_ci| Module | Method/Attribute/Enum/Constant | Change Type| 15e41f4b71Sopenharmony_ci| ------------------------- | ------------------------------------------------------------ | -------- | 16e41f4b71Sopenharmony_ci| medialibrary | **function** getFileAssets(options: MediaFetchOptions, callback: AsyncCallback<FetchFileResult>): void | Interface compatibility changed | 17e41f4b71Sopenharmony_ci| medialibrary | **function** getFileAssets(options: MediaFetchOptions): Promise<FetchFileResult> | Interface compatibility changed | 18e41f4b71Sopenharmony_ci| medialibrary | **function** createAsset(mediaType: MediaType, displayName: string, relativePath: string, callback: AsyncCallback<FileAsset>): void| Interface compatibility changed | 19e41f4b71Sopenharmony_ci| medialibrary | **function** createAsset(mediaType: MediaType, displayName: string, relativePath: string): Promise<FileAsset>| Interface compatibility changed | 20e41f4b71Sopenharmony_ci| medialibrary | **function** getAlbums(options: MediaFetchOptions, callback: AsyncCallback<Array<Album>>): void | Interface compatibility changed | 21e41f4b71Sopenharmony_ci| medialibrary | **function** getAlbums(options: MediaFetchOptions): Promise<Array<Album>> | Interface compatibility changed | 22e41f4b71Sopenharmony_ci| medialibrary | **function** FileAsset.commitModify(callback: AsyncCallback<void>): void | Interface compatibility changed | 23e41f4b71Sopenharmony_ci| medialibrary | **function** FileAsset.commitModify(): Promise<void> | Interface compatibility changed | 24e41f4b71Sopenharmony_ci| medialibrary | **function** FileAsset.deleteAsset(uri: string, callback: AsyncCallback<void>): void | Interface compatibility changed | 25e41f4b71Sopenharmony_ci| medialibrary | **function** FileAsset.deleteAsset(uri: string): Promise<void> | Interface compatibility changed | 26e41f4b71Sopenharmony_ci 27e41f4b71Sopenharmony_ci**Adaptation Guide** 28e41f4b71Sopenharmony_ci 29e41f4b71Sopenharmony_ci**getFileAssets** 30e41f4b71Sopenharmony_ci 31e41f4b71Sopenharmony_ciFrom API version 10, the albums represented by physical directories are replaced by logical albums, which allow 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()**. The following is an example of incorrect use of the APIs. 32e41f4b71Sopenharmony_ci 33e41f4b71Sopenharmony_ci1. Call [getMediaLibrary](../../../application-dev/reference/apis/js-apis-medialibrary.md#medialibrarygetmedialibrary) to obtain a **MediaLibrary** instance. 34e41f4b71Sopenharmony_ci2. Call [MediaFetchOptions](../../../application-dev/reference/apis/js-apis-medialibrary.md#mediafetchoptions7) to create the file fetching options. 35e41f4b71Sopenharmony_ci3. Call [getFileAssets](../../../application-dev/reference/apis/js-apis-medialibrary.md#getfileassets7) to obtain file assets. 36e41f4b71Sopenharmony_ci 37e41f4b71Sopenharmony_ci**Example (incorrect)**: 38e41f4b71Sopenharmony_ci 39e41f4b71Sopenharmony_ci```js 40e41f4b71Sopenharmony_ciimport mediaLibrary from '@ohos.multimedia.mediaLibrary'; 41e41f4b71Sopenharmony_ci 42e41f4b71Sopenharmony_ciasync function example() { 43e41f4b71Sopenharmony_ci try { 44e41f4b71Sopenharmony_ci let context = getContext(this); 45e41f4b71Sopenharmony_ci let media = mediaLibrary.getMediaLibrary(context); 46e41f4b71Sopenharmony_ci let fileKeyObj = mediaLibrary.FileKey; 47e41f4b71Sopenharmony_ci let albumId = 1; 48e41f4b71Sopenharmony_ci let getImageOp = { 49e41f4b71Sopenharmony_ci selections: fileKeyObj.ALBUM_ID + '= ?', // File assets cannot be obtained based on the parent, albumId, albumUri, and albumName attributes. 50e41f4b71Sopenharmony_ci selectionArgs: [albumId.toString()], 51e41f4b71Sopenharmony_ci }; 52e41f4b71Sopenharmony_ci const fetchFileResult = await media.getFileAssets(getImageOp); // The obtained fetchFileResult is empty. 53e41f4b71Sopenharmony_ci const fileAsset = await fetchFileResult.getFirstObject(); 54e41f4b71Sopenharmony_ci console.info('mediaLibrary fileAsset displayName: ' + fileAsset.displayName); 55e41f4b71Sopenharmony_ci } catch (err) { 56e41f4b71Sopenharmony_ci console.error('mediaLibrary fail, err: ' + err); 57e41f4b71Sopenharmony_ci } 58e41f4b71Sopenharmony_ci} 59e41f4b71Sopenharmony_ci``` 60e41f4b71Sopenharmony_ci 61e41f4b71Sopenharmony_ciUse **getFileAssets()** as follows: 62e41f4b71Sopenharmony_ci 63e41f4b71Sopenharmony_ci**Example (correct)**: 64e41f4b71Sopenharmony_ci 65e41f4b71Sopenharmony_ci```js 66e41f4b71Sopenharmony_ciimport mediaLibrary from '@ohos.multimedia.mediaLibrary'; 67e41f4b71Sopenharmony_ci 68e41f4b71Sopenharmony_ciasync function example() { 69e41f4b71Sopenharmony_ci try { 70e41f4b71Sopenharmony_ci let context = getContext(this); 71e41f4b71Sopenharmony_ci let media = mediaLibrary.getMediaLibrary(context); 72e41f4b71Sopenharmony_ci let fileKeyObj = mediaLibrary.FileKey; 73e41f4b71Sopenharmony_ci let imageType = mediaLibrary.MediaType.IMAGE; 74e41f4b71Sopenharmony_ci let getImageOp = { 75e41f4b71Sopenharmony_ci selections: fileKeyObj.MEDIA_TYPE + '= ?', 76e41f4b71Sopenharmony_ci selectionArgs: [imageType.toString()], // Query all files of the image type. 77e41f4b71Sopenharmony_ci }; 78e41f4b71Sopenharmony_ci const fetchFileResult = await media.getFileAssets(getImageOp); 79e41f4b71Sopenharmony_ci const fileAsset = await fetchFileResult.getFirstObject(); 80e41f4b71Sopenharmony_ci console.info('mediaLibrary fileAsset displayName: ' + fileAsset.displayName); 81e41f4b71Sopenharmony_ci } catch (err) { 82e41f4b71Sopenharmony_ci console.error('mediaLibrary fail, err: ' + err); 83e41f4b71Sopenharmony_ci } 84e41f4b71Sopenharmony_ci} 85e41f4b71Sopenharmony_ci``` 86e41f4b71Sopenharmony_ci 87e41f4b71Sopenharmony_ci**createAsset** 88e41f4b71Sopenharmony_ci 89e41f4b71Sopenharmony_ciSince 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. 90e41f4b71Sopenharmony_ci 91e41f4b71Sopenharmony_ci**getAlbums** 92e41f4b71Sopenharmony_ci 93e41f4b71Sopenharmony_ciSince 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. The following is an example of incorrect use of the APIs. 94e41f4b71Sopenharmony_ci 95e41f4b71Sopenharmony_ci1. Use [getMediaLibrary](../../../application-dev/reference/apis/js-apis-medialibrary.md#medialibrarygetmedialibrary) to obtain a **MediaLibrary** instance. 96e41f4b71Sopenharmony_ci2. Use [MediaFetchOptions](../../../application-dev/reference/apis/js-apis-medialibrary.md#mediafetchoptions7) to create the album fetching options. 97e41f4b71Sopenharmony_ci3. Use [getAlbums](../../../application-dev/reference/apis/js-apis-medialibrary.md#getalbums7) to obtain albums. 98e41f4b71Sopenharmony_ci 99e41f4b71Sopenharmony_ci**Example (incorrect)**: 100e41f4b71Sopenharmony_ci 101e41f4b71Sopenharmony_ci```js 102e41f4b71Sopenharmony_ciimport mediaLibrary from '@ohos.multimedia.mediaLibrary'; 103e41f4b71Sopenharmony_ci 104e41f4b71Sopenharmony_ciasync function example() { 105e41f4b71Sopenharmony_ci try { 106e41f4b71Sopenharmony_ci let context = getContext(this); 107e41f4b71Sopenharmony_ci let media = mediaLibrary.getMediaLibrary(context); 108e41f4b71Sopenharmony_ci let AlbumNoArgsfetchOp = { 109e41f4b71Sopenharmony_ci selections: mediaLibrary.FileKey.ALBUM_NAME + ' = ?', 110e41f4b71Sopenharmony_ci selectionArgs:['New album 1'], //Obtain the album named New album 1. 111e41f4b71Sopenharmony_ci }; 112e41f4b71Sopenharmony_ci const albumList = await media.getAlbums(AlbumNoArgsfetchOp); // The fetchFileResult returned is empty. 113e41f4b71Sopenharmony_ci for (let i = 0; i < albumList.length; i++) { 114e41f4b71Sopenharmony_ci console.info('mediaLibrary album albumName: ' + albumList[i].albumName); 115e41f4b71Sopenharmony_ci } 116e41f4b71Sopenharmony_ci } catch (err) { 117e41f4b71Sopenharmony_ci console.error('mediaLibrary fail, err: ' + err); 118e41f4b71Sopenharmony_ci } 119e41f4b71Sopenharmony_ci} 120e41f4b71Sopenharmony_ci``` 121e41f4b71Sopenharmony_ci 122e41f4b71Sopenharmony_ciThe following example shows how to obtain **Camera** and **Screenshots** albums: 123e41f4b71Sopenharmony_ci 124e41f4b71Sopenharmony_ci**Example (correct)**: 125e41f4b71Sopenharmony_ci 126e41f4b71Sopenharmony_ci```js 127e41f4b71Sopenharmony_ciimport mediaLibrary from '@ohos.multimedia.mediaLibrary'; 128e41f4b71Sopenharmony_ci 129e41f4b71Sopenharmony_ciasync function example() { 130e41f4b71Sopenharmony_ci try { 131e41f4b71Sopenharmony_ci let context = getContext(this); 132e41f4b71Sopenharmony_ci let media = mediaLibrary.getMediaLibrary(context); 133e41f4b71Sopenharmony_ci let AlbumNoArgsfetchOp = { 134e41f4b71Sopenharmony_ci selections: mediaLibrary.FileKey.ALBUM_NAME + ' = ? OR ' + mediaLibrary.FileKey.ALBUM_NAME + ' = ?', 135e41f4b71Sopenharmony_ci selectionArgs: ['Camera', 'Screenshots'], // Obtain the camera and screenshot albums. 136e41f4b71Sopenharmony_ci }; 137e41f4b71Sopenharmony_ci const albumList = await media.getAlbums(AlbumNoArgsfetchOp); 138e41f4b71Sopenharmony_ci for (let i = 0; i < albumList.length; i++) { 139e41f4b71Sopenharmony_ci console.info('mediaLibrary album albumName: ' + albumList[i].albumName); 140e41f4b71Sopenharmony_ci } 141e41f4b71Sopenharmony_ci } catch (err) { 142e41f4b71Sopenharmony_ci console.error('mediaLibrary fail, err: ' + err); 143e41f4b71Sopenharmony_ci } 144e41f4b71Sopenharmony_ci} 145e41f4b71Sopenharmony_ci``` 146e41f4b71Sopenharmony_ci 147e41f4b71Sopenharmony_ci**FileAsset.commitModify** 148e41f4b71Sopenharmony_ci 149e41f4b71Sopenharmony_ciThe **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. 150e41f4b71Sopenharmony_ci 151e41f4b71Sopenharmony_ci**FileAsset.deleteAsset** 152e41f4b71Sopenharmony_ci 153e41f4b71Sopenharmony_ciThe change has resolved a known issue of the file deletion mechanism. This issue may cause a system application to permanently delete a file that is not in the trash using **MediaLibrary.deleteAsset**. 154e41f4b71Sopenharmony_ci 155e41f4b71Sopenharmony_ciThe correct procedure for a system application to permanently delete a file is as follows: 156e41f4b71Sopenharmony_ci 157e41f4b71Sopenharmony_ci1. Call [getFileAssets](../../../application-dev/reference/apis/js-apis-medialibrary.md#getfileassets7) to obtain file assets. 158e41f4b71Sopenharmony_ci2. Call [FileAsset.trash](../../../application-dev/reference/apis/js-apis-medialibrary.md#trash8) to move the file to the trash. 159e41f4b71Sopenharmony_ci3. Call [MediaLibrary.deleteAsset](../../../application-dev/reference/apis/js-apis-medialibrary.md#deleteasset8) to permanently delete the file. 160e41f4b71Sopenharmony_ci 161e41f4b71Sopenharmony_ci**Example (correct)**: 162e41f4b71Sopenharmony_ci 163e41f4b71Sopenharmony_ci```js 164e41f4b71Sopenharmony_ciimport mediaLibrary from '@ohos.multimedia.mediaLibrary'; 165e41f4b71Sopenharmony_ci 166e41f4b71Sopenharmony_ciasync function example() { 167e41f4b71Sopenharmony_ci try { 168e41f4b71Sopenharmony_ci let context = getContext(this); 169e41f4b71Sopenharmony_ci let media = mediaLibrary.getMediaLibrary(context); 170e41f4b71Sopenharmony_ci let fileKeyObj = mediaLibrary.FileKey; 171e41f4b71Sopenharmony_ci let imageType = mediaLibrary.MediaType.IMAGE; 172e41f4b71Sopenharmony_ci let getImageOp = { 173e41f4b71Sopenharmony_ci selections: fileKeyObj.MEDIA_TYPE + '= ?', 174e41f4b71Sopenharmony_ci selectionArgs: [imageType.toString()], 175e41f4b71Sopenharmony_ci }; 176e41f4b71Sopenharmony_ci const fetchFileResult = await media.getFileAssets(getImageOp); 177e41f4b71Sopenharmony_ci const fileAsset = await fetchFileResult.getFirstObject(); 178e41f4b71Sopenharmony_ci // Delete the file (move it to the trash of Gallery). 179e41f4b71Sopenharmony_ci await fileAsset.trash(true); 180e41f4b71Sopenharmony_ci // Delete the file from the system permanently. 181e41f4b71Sopenharmony_ci await media.deleteAsset(fileAsset.uri); 182e41f4b71Sopenharmony_ci } catch (err) { 183e41f4b71Sopenharmony_ci console.error('Failed to delete asset permanently from system, error: ' + err); 184e41f4b71Sopenharmony_ci } 185e41f4b71Sopenharmony_ci} 186e41f4b71Sopenharmony_ci``` 187