1# File Subsystem Changelog 2 3## cl.file.1 mediaLibrary APIs Changed 4 5All APIs provided by the mediaLibrary module of the multimedia subsystem are deprecated. 6 7**Change Impact** 8 9All APIs described in [mediaLibrary](../../../application-dev/reference/apis/js-apis-medialibrary.md) are deprecated. Third-party applications can only select and save files in the public directory by calling the APIs of [FilePicker](../../../application-dev/reference/apis/js-apis-file-picker.md). 10For applications developed based on earlier versions, pay attention to the changes of APIs. 11 12**Key API/Component Changes** 13 14The table below lists the **mediaLibrary** APIs that can be substituted by the **FilePicker** APIs. 15 16| Module | Method/Attribute/Enum/Constant | Change Type| 17| ------------------------- | ------------------------------------------------------------ | -------- | 18| medialibrary | **function** getMediaLibrary(context: Context): MediaLibrary; | Deprecated | 19| medialibrary | **function** getFileAssets(options: MediaFetchOptions, callback: AsyncCallback\<FetchFileResult\>): void | Deprecated | 20| medialibrary | **function** getFileAssets(options: MediaFetchOptions): Promise\<FetchFileResult\> | Deprecated | 21| medialibrary | **function** createAsset(mediaType: MediaType, displayName: string, relativePath: string, callback: AsyncCallback\<FileAsset\>): void | Deprecated | 22| medialibrary | **function** createAsset(mediaType: MediaType, displayName: string, relativePath: string): Promise\<FileAsset\> | Deprecated | 23| medialibrary | **function** getPublicDirectory(type: DirectoryType, callback: AsyncCallback\<string\>): void | Deprecated | 24| medialibrary | **function** getPublicDirectory(type: DirectoryType): Promise\<string\> | Deprecated | 25| medialibrary | **function** release(callback: AsyncCallback\<void\>): void | Deprecated | 26| medialibrary | **function** release(): Promise\<void\> | Deprecated | 27| medialibrary | **function** FileAsset.open(mode: string, callback: AsyncCallback\<number\>): void | Deprecated | 28| medialibrary | **function** FileAsset.open(mode: string): Promise\<number\> | Deprecated | 29| medialibrary | **function** FileAsset.close(fd: number, callback: AsyncCallback\<void\>): void | Deprecated | 30| medialibrary | **function** FileAsset.close(fd: number): Promise\<void\> | Deprecated | 31| medialibrary | **function** FetchFileResult.getCount(): number | Deprecated | 32| medialibrary | **function** FetchFileResult.isAfterLast(): boolean | Deprecated | 33| medialibrary | **function** FetchFileResult.close(): void | Deprecated | 34| medialibrary | **function** FetchFileResult.getFirstObject(callback: AsyncCallback\<FileAsset\>): void | Deprecated | 35| medialibrary | **function** FetchFileResult.getFirstObject(): Promise\<FileAsset\> | Deprecated | 36| medialibrary | **function** FetchFileResult.getNextObject(callback: AsyncCallback\<FileAsset\>): void | Deprecated | 37| medialibrary | **function** FetchFileResult.getNextObject(): Promise\<FileAsset\> | Deprecated | 38| medialibrary | **function** FetchFileResult.getLastObject(callback: AsyncCallback\<FileAsset\>): void | Deprecated | 39| medialibrary | **function** FetchFileResult.getLastObject(): Promise\<FileAsset\> | Deprecated | 40| medialibrary | **function** FetchFileResult.getPositionObject(index: number, callback: AsyncCallback\<FileAsset\>): void | Deprecated | 41| medialibrary | **function** FetchFileResult.getPositionObject(index: number): Promise\<FileAsset\> | Deprecated | 42| medialibrary | **function** FetchFileResult.getAllObject(callback: AsyncCallback\<Array\<FileAsset\>\>): void | Deprecated | 43| medialibrary | **function** FetchFileResult.getAllObject(): Promise\<Array\<FileAsset\>\> | Deprecated | 44| medialibrary | **function** Album.getFileAssets(options: MediaFetchOptions, callback: AsyncCallback\<FetchFileResult\>): void | Deprecated | 45| medialibrary | **function** Album.getFileAssets(options?: MediaFetchOptions): Promise\<FetchFileResult\> | Deprecated | 46| medialibrary | **enum** FileKey | Deprecated | 47| medialibrary | **enum** DirectoryType | Deprecated | 48| medialibrary | **enum** MediaType | Deprecated | 49| medialibrary | **interface** MediaFetchOptions | Deprecated | 50| medialibrary | **interface** FileAsset | Deprecated | 51 52**Adaptation Guide** 53 54**The following example shows how to use the mediaLibrary APIs to edit a file in the public directory:** 55 561. Call **getMediaLibrary** to obtain a **mediaLibrary** instance. 572. Create a **MediaFetchOptions** object, and call **getFileAssets** to obtain files in the public directory. 583. Call the **FetchFileResult** APIs to obtain the file asset of the target file. 594. Call **fileAsset.open** to open the file and obtain the FD. 605. Call [fs.writeSync](../../../application-dev/reference/apis/js-apis-file-fs.md#writesync) to edit the file through the FD. 616. After the edit, call **fileAsset.close** to close the FD of the file. 627. Call **fetchFileResult.close** to release the resources occupied by **getFileAssets**. 638. Call **release** to release the **mediaLibrary** instance. 64 65**Example** 66 67```js 68import mediaLibrary from '@ohos.multimedia.mediaLibrary'; 69import fs from '@ohos.file.fs'; 70 71async function example() { 72 try { 73 let context = getContext(this); 74 let media = mediaLibrary.getMediaLibrary(context); 75 let fileKeyObj = mediaLibrary.FileKey; 76 let imageType = mediaLibrary.MediaType.IMAGE; 77 let getImageOp = { 78 selections: fileKeyObj.MEDIA_TYPE + '= ?', 79 selectionArgs: [imageType.toString()], 80 order: fileKeyObj.DATE_ADDED + ' DESC', 81 }; 82 const fetchFileResult = await media.getFileAssets(getImageOp); 83 const fileAsset = await fetchFileResult.getFirstObject(); 84 console.info('mediaLibrary fileAsset displayName: ' + fileAsset.displayName); 85 let fd = await fileAsset.open('rw'); 86 console.info('mediaLibrary fileAsset open fd: ' + fd); 87 let writeLen = fs.writeSync(fd, 'hello, world'); 88 console.info('write data to file succeed and size is: ' + writeLen); 89 fileAsset.close(fd); 90 fetchFileResult.close(); 91 media.release(); 92 } catch (err) { 93 console.error('mediaLibrary fail, err: ' + err); 94 } 95} 96``` 97 98**The following example shows how to use the FilePicker APIs to edit a file in the public directory:** 99 1001. Obtain a **DocumentViewPicker** object. 1012. Call **DocumentViewPicker.select** to select a file. 1023. After a file is selected, a file URI is returned. 1034. After the UI is returned from DocumentViewPicker, call [fs.openSync](../../../application-dev/reference/apis/js-apis-file-fs.md#fsopensync) to open the file based on the URI and obtain the FD. 1045. Call [fs.writeSync](../../../application-dev/reference/apis/js-apis-file-fs.md#writesync) to edit the file through the FD. 1056. After the edit, call [fs.closeSync](../../../application-dev/reference/apis/js-apis-file-fs.md#closesync) to close the FD. 106 107**Example** 108 109```js 110import mediaLibrary from '@ohos.multimedia.mediaLibrary'; 111import picker from '@ohos.file.picker'; 112import fs from '@ohos.file.fs'; 113 114let uri; 115 116async function example() { 117 try { 118 let DocumentSelectOptions = new picker.DocumentSelectOptions(); 119 let documentPicker = new picker.DocumentViewPicker(); 120 documentPicker.select(DocumentSelectOptions).then((DocumentSelectResult) => { 121 console.info('DocumentViewPicker.select successfully, DocumentSelectResult uri: ' + JSON.stringify(DocumentSelectResult)); 122 uri = DocumentSelectResult[0]; 123 }).catch((err) => { 124 console.error('DocumentViewPicker.select failed with err: ' + err); 125 }); 126 } catch (err) { 127 console.error('DocumentViewPicker failed with err: ' + err); 128 } 129} 130 131async function writeFile() { 132 try { 133 let file = fs.openSync(uri, fs.OpenMode.READ_WRITE); 134 console.info('DocumentViewPicker file fd: ' + file.fd); 135 let writeLen = fs.writeSync(file.fd, 'hello, world'); 136 console.info('write data to file succeed and size is: ' + writeLen); 137 fs.closeSync(file); 138 } catch (err) { 139 console.error('DocumentViewPicker fail, err: ' + err); 140 } 141} 142``` 143 144**The following example shows how to use the mediaLibrary APIs to create a file in the public directory:** 145 1461. Call **getMediaLibrary** to obtain a **mediaLibrary** instance. 1472. Call **getPublicDirectory** to obtain the path of the public directory. 1483. Call **createAsset** to create a file and obtain the file asset. 1494. Call **fileAsset.open** to open the file and obtain the FD. 1505. Call **fs.write** to edit the file through the FD. 1516. After the edit, call **fileAsset.close** to close the FD. 1527. Call **release** to release the **mediaLibrary** instance. 153 154**Example** 155 156```js 157import mediaLibrary from '@ohos.multimedia.mediaLibrary'; 158import fs from '@ohos.file.fs'; 159 160async function example() { 161 try { 162 let context = getContext(this); 163 let media = mediaLibrary.getMediaLibrary(context); 164 let mediaType = mediaLibrary.MediaType.FILE; 165 let DIR_DOWNLOAD = mediaLibrary.DirectoryType.DIR_DOWNLOAD; 166 const path = await media.getPublicDirectory(DIR_DOWNLOAD); 167 const fileAsset = await media.createAsset(mediaType, 'test.txt', path); 168 console.info('mediaLibrary fileAsset displayName: ' + fileAsset.displayName); 169 let fd = await fileAsset.open('rw'); 170 console.info('mediaLibrary fileAsset open fd: ' + fd); 171 let writeLen = fs.writeSync(fd, 'hello, world'); 172 console.info('write data to file succeed and size is: ' + writeLen); 173 fileAsset.close(fd); 174 media.release(); 175 } catch (err) { 176 console.error('mediaLibrary fail, err: ' + err); 177 } 178} 179``` 180 181**The following example shows how to use the FilePicker APIs to create a file in the public directory:** 182 1831. Obtain a **DocumentViewPicker** object. 1842. Call **DocumentViewPicker.save** to create and save an empty file. 1853. After the file is saved, a file URI is returned. 1864. After the UI is returned from DocumentViewPicker, call [fs.openSync](../../../application-dev/reference/apis/js-apis-file-fs.md#fsopensync) to open the file based on the URI and obtain the FD. 1875. Call [fs.writeSync](../../../application-dev/reference/apis/js-apis-file-fs.md#writesync) to edit the file through the FD. 1886. After the edit, call [fs.closeSync](../../../application-dev/reference/apis/js-apis-file-fs.md#closesync) to close the FD. 189 190**Example** 191 192```js 193import mediaLibrary from '@ohos.multimedia.mediaLibrary'; 194import picker from '@ohos.file.picker'; 195import fs from '@ohos.file.fs'; 196 197let uri; 198 199async function example() { 200 try { 201 let DocumentSaveOptions = new picker.DocumentSaveOptions(); 202 DocumentSaveOptions.newFileNames = ['DocumentViewPicker01.txt']; 203 let documentPicker = new picker.DocumentViewPicker(); 204 documentPicker.save(DocumentSaveOptions).then((DocumentSaveResult) => { 205 console.info('DocumentViewPicker.save successfully, DocumentSaveResult uri: ' + JSON.stringify(DocumentSaveResult)); 206 uri = DocumentSaveResult[0]; 207 }).catch((err) => { 208 console.error('DocumentViewPicker.save failed with err: ' + err); 209 }); 210 } catch (err) { 211 console.error('DocumentViewPicker failed with err: ' + err); 212 } 213} 214 215async function writeFile() { 216 try { 217 let file = fs.openSync(uri, fs.OpenMode.READ_WRITE); 218 console.info('DocumentViewPicker file fd: ' + file.fd); 219 let writeLen = fs.writeSync(file.fd, 'hello, world'); 220 console.info('write data to file succeed and size is: ' + writeLen); 221 fs.closeSync(file); 222 } catch (err) { 223 console.error('DocumentViewPicker fail, err: ' + err); 224 } 225} 226``` 227 228**Key API/Component Changes** 229 230The table below lists the mediaLibrary APIs that are not open to third-party applications due to function control. There are no substitute APIs for them. 231 232| Module | Method/Attribute/Enum/Constant | Change Type| 233| ------------------------- | ------------------------------------------------------------ | -------- | 234| medialibrary | **function** getMediaLibrary(): MediaLibrary; | Deprecated | 235| medialibrary | **function** on(type: 'deviceChange'\|'albumChange'\|'imageChange'\|'audioChange'\|'videoChange'\|'fileChange'\|'remoteFileChange', callback: Callback\<void\>): void | Deprecated | 236| medialibrary | **function** off(type: 'deviceChange'\|'albumChange'\|'imageChange'\|'audioChange'\|'videoChange'\|'fileChange'\|'remoteFileChange', callback?: Callback\<void\>): void | Deprecated | 237| medialibrary | **function** deleteAsset(uri: string): Promise\<void\> | Deprecated | 238| medialibrary | **function** deleteAsset(uri: string, callback: AsyncCallback\<void\>): void | Deprecated | 239| medialibrary | **function** storeMediaAsset(option: MediaAssetOption, callback: AsyncCallback\<string\>): void | Deprecated | 240| medialibrary | **function** storeMediaAsset(option: MediaAssetOption): Promise\<string\> | Deprecated | 241| medialibrary | **function** startImagePreview(images: Array\<string\>, index: number, callback: AsyncCallback\<void\>): void | Deprecated | 242| medialibrary | **function** startImagePreview(images: Array\<string\>, callback: AsyncCallback\<void\>): void | Deprecated | 243| medialibrary | **function** startImagePreview(images: Array\<string\>, index?: number): Promise\<void\> | Deprecated | 244| medialibrary | **function** startMediaSelect(option: MediaSelectOption, callback: AsyncCallback\<Array\<string\>\>): void | Deprecated | 245| medialibrary | **function** startMediaSelect(option: MediaSelectOption): Promise\<Array\<string\>\> | Deprecated | 246| medialibrary | **function** getActivePeers(): Promise\<Array\<PeerInfo\>\>; | Deprecated | 247| medialibrary | **function** getActivePeers(callback: AsyncCallback\<Array\<PeerInfo\>\>): void; | Deprecated | 248| medialibrary | **function** getAllPeers(): Promise\<Array\<PeerInfo\>\>; | Deprecated | 249| medialibrary | **function** FileAsset.isDirectory(callback: AsyncCallback\<boolean\>): void | Deprecated | 250| medialibrary | **function** FileAsset.isDirectory():Promise\<boolean\> | Deprecated | 251| medialibrary | **function** FileAsset.commitModify(callback: AsyncCallback\<void\>): void | Deprecated | 252| medialibrary | **function** FileAsset.commitModify(): Promise\<void\> | Deprecated | 253| medialibrary | **function** FileAsset.getThumbnail(callback: AsyncCallback\<image.PixelMap\>): void | Deprecated | 254| medialibrary | **function** FileAsset.getThumbnail(size: Size, callback: AsyncCallback\<image.PixelMap\>): void | Deprecated | 255| medialibrary | **function** FileAsset.getThumbnail(size?: Size): Promise\<image.PixelMap\> | Deprecated | 256| medialibrary | **function** FileAsset.favorite(isFavorite: boolean, callback: AsyncCallback\<void\>): void | Deprecated | 257| medialibrary | **function** FileAsset.favorite(isFavorite: boolean): Promise\<void\> | Deprecated | 258| medialibrary | **function** FileAsset.isFavorite(callback: AsyncCallback\<boolean\>): void | Deprecated | 259| medialibrary | **function** FileAsset.isFavorite():Promise\<boolean\> | Deprecated | 260| medialibrary | **function** FileAsset.trash(isTrash: boolean, callback: AsyncCallback\<void\>): void | Deprecated | 261| medialibrary | **function** FileAsset.trash(isTrash: boolean): Promise\<void\> | Deprecated | 262| medialibrary | **function** FileAsset.isTrash(callback: AsyncCallback\<boolean\>): void | Deprecated | 263| medialibrary | **function** FileAsset.isTrash():Promise\<boolean\> | Deprecated | 264| medialibrary | **function** getAlbums(options: MediaFetchOptions, callback: AsyncCallback\<Array\<Album\>\>): void | Deprecated | 265| medialibrary | **function** getAlbums(options: MediaFetchOptions): Promise\<Array\<Album\>\> | Deprecated | 266| medialibrary | **function** Album.commitModify(callback: AsyncCallback\<void\>): void | Deprecated | 267| medialibrary | **function** Album.commitModify(): Promise\<void\> | Deprecated | 268| medialibrary | **enum** DeviceType | Deprecated | 269| medialibrary | **interface** PeerInfo | Deprecated | 270| medialibrary | **interface** Size | Deprecated | 271| medialibrary | **interface** MediaAssetOption | Deprecated | 272| medialibrary | **interface** MediaSelectOption | Deprecated | 273