1e41f4b71Sopenharmony_ci# File Subsystem Changelog
2e41f4b71Sopenharmony_ci
3e41f4b71Sopenharmony_ci## cl.file.1 Change of Compatibility of mediaLibrary APIs
4e41f4b71Sopenharmony_ci
5e41f4b71Sopenharmony_ciThe compatibility of some [mediaLibrary](../../../application-dev/reference/apis/js-apis-medialibrary.md) APIs is changed.
6e41f4b71Sopenharmony_ci
7e41f4b71Sopenharmony_ci1. Due to the change of the directory structure of the file subsystem, the **mediaLibrary** APIs will no longer be used to access document directories (**Documents** and **Download**). Use the [FilePicker](../../../application-dev/reference/apis/js-apis-file-picker.md) APIs instead.
8e41f4b71Sopenharmony_ci
9e41f4b71Sopenharmony_ci- Before the change, the following directories can be accessed by using **mediaLibrary** APIs:
10e41f4b71Sopenharmony_ci
11e41f4b71Sopenharmony_ci  **Camera**, **Videos**, **Pictures**, **Audios**, **Documents**, and **Download**
12e41f4b71Sopenharmony_ci
13e41f4b71Sopenharmony_ci- After the change, the following directories can be accessed by using **mediaLibrary** APIs:
14e41f4b71Sopenharmony_ci
15e41f4b71Sopenharmony_ci  **Camera**, **Videos**, **Pictures**, and **Audios**
16e41f4b71Sopenharmony_ci
17e41f4b71Sopenharmony_ci2. The URI format of the corresponding assets is changed. For details, see [File Subsystem ChangeLog](../OpenHarmony_4.0.11.2/changelogs-filemanagement.md).
18e41f4b71Sopenharmony_ci
19e41f4b71Sopenharmony_ci**Change Impact**
20e41f4b71Sopenharmony_ci
21e41f4b71Sopenharmony_ciFor applications developed based on earlier versions, check for the **mediaLibrary** APIs called to access the **Documents** and **Download** directories and use the **FilePicker** APIs.
22e41f4b71Sopenharmony_ci
23e41f4b71Sopenharmony_ci**Key API/Component Changes**
24e41f4b71Sopenharmony_ci
25e41f4b71Sopenharmony_ci| Module                   | Method/Attribute/Enum/Constant                                         | Change Type|
26e41f4b71Sopenharmony_ci| ------------------------- | ------------------------------------------------------------ | -------- |
27e41f4b71Sopenharmony_ci| medialibrary   |  **function** getFileAssets(options: MediaFetchOptions, callback: AsyncCallback<FetchFileResult>): void | The compatibility of the applicable range of the API is changed.    |
28e41f4b71Sopenharmony_ci| medialibrary   |  **function** getFileAssets(options: MediaFetchOptions): Promise<FetchFileResult> | The compatibility of the applicable range of the API is changed.    |
29e41f4b71Sopenharmony_ci| medialibrary   |  **function** createAsset(mediaType: MediaType, displayName: string, relativePath: string, callback: AsyncCallback<FileAsset>): void | The compatibility of the applicable range of the API is changed.    |
30e41f4b71Sopenharmony_ci| medialibrary   |  **function** createAsset(mediaType: MediaType, displayName: string, relativePath: string): Promise<FileAsset> | The compatibility of the applicable range of the API is changed.    |
31e41f4b71Sopenharmony_ci| medialibrary   |  **function** deleteAsset(uri: string, callback: AsyncCallback<void>): void | The compatibility of the applicable range of the API is changed.    |
32e41f4b71Sopenharmony_ci| medialibrary   |  **function** deleteAsset(uri: string): Promise<void> | The compatibility of the applicable range of the API is changed.    |
33e41f4b71Sopenharmony_ci| medialibrary   |  **function** getPublicDirectory(type: DirectoryType, callback: AsyncCallback<string>): void | The compatibility of the applicable range of the API is changed.    |
34e41f4b71Sopenharmony_ci| medialibrary   |  **function** getPublicDirectory(type: DirectoryType): Promise<string> | The compatibility of the applicable range of the API is changed.    |
35e41f4b71Sopenharmony_ci| medialibrary   |  **function** storeMediaAsset(option: MediaAssetOption, callback: AsyncCallback<string>): void | The compatibility of the applicable range of the API is changed.    |
36e41f4b71Sopenharmony_ci| medialibrary   |  **function** storeMediaAsset(option: MediaAssetOption): Promise<string> | The compatibility of the applicable range of the API is changed.    |
37e41f4b71Sopenharmony_ci| medialibrary   |  **interface** DirectoryType | The compatibility of the applicable range of the API is changed.    |
38e41f4b71Sopenharmony_ci| medialibrary   |  **interface** MediaAssetOption | The compatibility of the applicable range of the API is changed.    |
39e41f4b71Sopenharmony_ci
40e41f4b71Sopenharmony_ci**getFileAssets**:
41e41f4b71Sopenharmony_ci
42e41f4b71Sopenharmony_ciThe **getFileAssets** API cannot be used to obtain file assets in the **Documents** and **Download** directories. Use the [FilePicker](../../../application-dev/reference/apis/js-apis-file-picker.md) APIs instead.
43e41f4b71Sopenharmony_ci
44e41f4b71Sopenharmony_ci**createAsset**:
45e41f4b71Sopenharmony_ci
46e41f4b71Sopenharmony_ciIf **relativePath** in **createAsset()** is set to a directory in **Documents** or **Download**), the file asset cannot be created in the specified directory. Use the [FilePicker](../../../application-dev/reference/apis/js-apis-file-picker.md) APIs instead.
47e41f4b71Sopenharmony_ci
48e41f4b71Sopenharmony_ci**deleteAsset**:
49e41f4b71Sopenharmony_ci
50e41f4b71Sopenharmony_ciIf **URI** in **deleteAsset()** specifies a file asset in the **Documents** or **Download** directory, the specified file asset cannot be deleted. Use [fileAccess.delete](../../../application-dev/reference/apis/js-apis-fileAccess.md) APIs instead.
51e41f4b71Sopenharmony_ci
52e41f4b71Sopenharmony_ci**getPublicDirectory**:
53e41f4b71Sopenharmony_ci
54e41f4b71Sopenharmony_ciIf **type** in **getPublicDirectory()** is set to **mediaLibrary.DirectoryType.DIR_DOCUMENTS** or **mediaLibrary.DirectoryType.DIR_DOWNLOAD**, the specified file path cannot be obtained.
55e41f4b71Sopenharmony_ci
56e41f4b71Sopenharmony_ci**storeMediaAsset**:
57e41f4b71Sopenharmony_ci
58e41f4b71Sopenharmony_ciIf **MediaAssetOption.relativePath** in **storeMediaAsset()** is set to a path of the file in the **Documents** or **Download**) directory, the file asset cannot be stored. Use the [FilePicker](../../../application-dev/reference/apis/js-apis-file-picker.md) APIs instead.
59e41f4b71Sopenharmony_ci
60e41f4b71Sopenharmony_ci**DirectoryType**:
61e41f4b71Sopenharmony_ci
62e41f4b71Sopenharmony_ci**DirectoryType** does not support **mediaLibrary.DirectoryType.DIR_DOCUMENTS** or **mediaLibrary.DirectoryType.DIR_DOWNLOAD** in **mediaLibrary** APIs.
63e41f4b71Sopenharmony_ci
64e41f4b71Sopenharmony_ci**MediaAssetOption**:
65e41f4b71Sopenharmony_ci
66e41f4b71Sopenharmony_ciThe **relativePath** in the **MediaAssetOption** attribute cannot be set to the path of a file in the **Documents** or **Download** directory when **storeMediaAsset()** is used.
67e41f4b71Sopenharmony_ci
68e41f4b71Sopenharmony_ci**Adaptation Guide**
69e41f4b71Sopenharmony_ci
70e41f4b71Sopenharmony_ciDo not use the **mediaLibrary** APIs to access or operate the files in the **Documents** and **Download** directories. Use the [FilePicker](../../../application-dev/reference/apis/js-apis-file-picker.md) APIs instead.
71e41f4b71Sopenharmony_ci
72e41f4b71Sopenharmony_ci![fileAccessView](figures/fileAccessView.png)
73e41f4b71Sopenharmony_ci
74e41f4b71Sopenharmony_ciExample:
75e41f4b71Sopenharmony_ci
76e41f4b71Sopenharmony_ciBefore the change, the **mediaLibrary** APIs are used to edit files in the **Documents** and **Download** directories.
77e41f4b71Sopenharmony_ci
78e41f4b71Sopenharmony_ci1. Call **getMediaLibrary** to obtain a **mediaLibrary** instance.
79e41f4b71Sopenharmony_ci2. Create a **MediaFetchOptions** object, and call **getFileAssets** to obtain files in the use file directory.
80e41f4b71Sopenharmony_ci3. Call the **FetchFileResult** APIs to obtain the file asset of the target file.
81e41f4b71Sopenharmony_ci4. Call **fileAsset.open** to open the file and obtain the FD.
82e41f4b71Sopenharmony_ci5. Call [fs.writeSync](../../../application-dev/reference/apis/js-apis-file-fs.md#writesync) to edit the file based on the FD.
83e41f4b71Sopenharmony_ci6. After the edit, call **fileAsset.close** to close the FD of the file.
84e41f4b71Sopenharmony_ci7. Call **fetchFileResult.close** to release the resources occupied by **getFileAssets**.
85e41f4b71Sopenharmony_ci8. Call **release** to release the **mediaLibrary** instance.
86e41f4b71Sopenharmony_ci
87e41f4b71Sopenharmony_ci**Sample code**:
88e41f4b71Sopenharmony_ci
89e41f4b71Sopenharmony_ci```js
90e41f4b71Sopenharmony_ciimport mediaLibrary from '@ohos.multimedia.mediaLibrary';
91e41f4b71Sopenharmony_ciimport fs from '@ohos.file.fs';
92e41f4b71Sopenharmony_ci
93e41f4b71Sopenharmony_ciasync function example() {
94e41f4b71Sopenharmony_ci  try {
95e41f4b71Sopenharmony_ci    let context = getContext(this);
96e41f4b71Sopenharmony_ci    let media = mediaLibrary.getMediaLibrary(context);
97e41f4b71Sopenharmony_ci    let fileKeyObj = mediaLibrary.FileKey;
98e41f4b71Sopenharmony_ci    let imageType = mediaLibrary.MediaType.IMAGE;
99e41f4b71Sopenharmony_ci    let getImageOp = {
100e41f4b71Sopenharmony_ci      selections: fileKeyObj.MEDIA_TYPE + '= ?',
101e41f4b71Sopenharmony_ci      selectionArgs: [imageType.toString()],
102e41f4b71Sopenharmony_ci      order: fileKeyObj.DATE_ADDED + ' DESC',
103e41f4b71Sopenharmony_ci    };
104e41f4b71Sopenharmony_ci    const fetchFileResult = await media.getFileAssets(getImageOp);
105e41f4b71Sopenharmony_ci    const fileAsset = await fetchFileResult.getFirstObject();
106e41f4b71Sopenharmony_ci    console.info('mediaLibrary fileAsset displayName: ' + fileAsset.displayName);
107e41f4b71Sopenharmony_ci    let fd = await fileAsset.open('rw');
108e41f4b71Sopenharmony_ci    console.info('mediaLibrary fileAsset open fd: ' + fd);
109e41f4b71Sopenharmony_ci    let writeLen = fs.writeSync(fd, 'hello, world');
110e41f4b71Sopenharmony_ci    console.info('write data to file succeed and size is: ' + writeLen);
111e41f4b71Sopenharmony_ci    fileAsset.close(fd);
112e41f4b71Sopenharmony_ci    fetchFileResult.close();
113e41f4b71Sopenharmony_ci    media.release();
114e41f4b71Sopenharmony_ci  } catch (err) {
115e41f4b71Sopenharmony_ci    console.error('mediaLibrary fail, err: ' + err);
116e41f4b71Sopenharmony_ci  }
117e41f4b71Sopenharmony_ci}
118e41f4b71Sopenharmony_ci```
119e41f4b71Sopenharmony_ci
120e41f4b71Sopenharmony_ciAfter the change, use **FilePicker** APIs to edit the files in the user file directory.
121e41f4b71Sopenharmony_ci
122e41f4b71Sopenharmony_ci1. Obtain a **DocumentViewPicker** object.
123e41f4b71Sopenharmony_ci2. Call **DocumentViewPicker.select** to select a file.
124e41f4b71Sopenharmony_ci3. After a file is selected, the file URI is returned.
125e41f4b71Sopenharmony_ci4. 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.
126e41f4b71Sopenharmony_ci5. Call [fs.writeSync](../../../application-dev/reference/apis/js-apis-file-fs.md#writesync) to edit the file based on the FD.
127e41f4b71Sopenharmony_ci6. After the edit, call [fs.closeSync](../../../application-dev/reference/apis/js-apis-file-fs.md#closesync) to close the FD.
128e41f4b71Sopenharmony_ci
129e41f4b71Sopenharmony_ci**Sample code**:
130e41f4b71Sopenharmony_ci
131e41f4b71Sopenharmony_ci```js
132e41f4b71Sopenharmony_ciimport picker from '@ohos.file.picker';
133e41f4b71Sopenharmony_ciimport fs from '@ohos.file.fs';
134e41f4b71Sopenharmony_ci
135e41f4b71Sopenharmony_cilet uri;
136e41f4b71Sopenharmony_ci
137e41f4b71Sopenharmony_ciasync function example() {
138e41f4b71Sopenharmony_ci  try {
139e41f4b71Sopenharmony_ci    let DocumentSelectOptions = new picker.DocumentSelectOptions();
140e41f4b71Sopenharmony_ci    let documentPicker = new picker.DocumentViewPicker();
141e41f4b71Sopenharmony_ci    documentPicker.select(DocumentSelectOptions).then((DocumentSelectResult) => {
142e41f4b71Sopenharmony_ci      console.info('DocumentViewPicker.select successfully, DocumentSelectResult uri: ' + JSON.stringify(DocumentSelectResult));
143e41f4b71Sopenharmony_ci      uri = DocumentSelectResult[0];
144e41f4b71Sopenharmony_ci    }).catch((err) => {
145e41f4b71Sopenharmony_ci      console.error('DocumentViewPicker.select failed with err: ' + err);
146e41f4b71Sopenharmony_ci    });
147e41f4b71Sopenharmony_ci  } catch (err) {
148e41f4b71Sopenharmony_ci    console.error('DocumentViewPicker failed with err: ' + err);
149e41f4b71Sopenharmony_ci  }
150e41f4b71Sopenharmony_ci}
151e41f4b71Sopenharmony_ci
152e41f4b71Sopenharmony_ciasync function writeFile() {
153e41f4b71Sopenharmony_ci  try {
154e41f4b71Sopenharmony_ci    let file = fs.openSync(uri, fs.OpenMode.READ_WRITE);
155e41f4b71Sopenharmony_ci    console.info('DocumentViewPicker file fd: ' + file.fd);
156e41f4b71Sopenharmony_ci    let writeLen = fs.writeSync(file.fd, 'hello, world');
157e41f4b71Sopenharmony_ci    console.info('write data to file succeed and size is: ' + writeLen);
158e41f4b71Sopenharmony_ci    fs.closeSync(file);
159e41f4b71Sopenharmony_ci  } catch (err) {
160e41f4b71Sopenharmony_ci    console.error('DocumentViewPicker fail, err: ' + err);
161e41f4b71Sopenharmony_ci  }
162e41f4b71Sopenharmony_ci}
163e41f4b71Sopenharmony_ci```
164e41f4b71Sopenharmony_ci
165e41f4b71Sopenharmony_ciBefore the change, use the **mediaLibrary** APIs to create a file in the **Documents** and **Download** directories.
166e41f4b71Sopenharmony_ci
167e41f4b71Sopenharmony_ci1. Call **getMediaLibrary** to obtain a **mediaLibrary** instance.
168e41f4b71Sopenharmony_ci2. Call **getPublicDirectory** to obtain the path of the user file directory.
169e41f4b71Sopenharmony_ci3. Call **createAsset** to create a file and obtain the file asset.
170e41f4b71Sopenharmony_ci4. Call **fileAsset.open** to open the file and obtain the FD.
171e41f4b71Sopenharmony_ci5. Call **fs.write** to edit the file based on the FD.
172e41f4b71Sopenharmony_ci6. After the edit, call **fileAsset.close** to close the FD of the file.
173e41f4b71Sopenharmony_ci7. Call **release** to release the **mediaLibrary** instance.
174e41f4b71Sopenharmony_ci
175e41f4b71Sopenharmony_ci**Sample code**:
176e41f4b71Sopenharmony_ci
177e41f4b71Sopenharmony_ci```js
178e41f4b71Sopenharmony_ciimport mediaLibrary from '@ohos.multimedia.mediaLibrary';
179e41f4b71Sopenharmony_ciimport fs from '@ohos.file.fs';
180e41f4b71Sopenharmony_ci
181e41f4b71Sopenharmony_ciasync function example() {
182e41f4b71Sopenharmony_ci  try {
183e41f4b71Sopenharmony_ci    let context = getContext(this);
184e41f4b71Sopenharmony_ci    let media = mediaLibrary.getMediaLibrary(context);
185e41f4b71Sopenharmony_ci    let mediaType = mediaLibrary.MediaType.FILE;
186e41f4b71Sopenharmony_ci    let DIR_DOWNLOAD = mediaLibrary.DirectoryType.DIR_DOWNLOAD;
187e41f4b71Sopenharmony_ci    const path = await media.getPublicDirectory(DIR_DOWNLOAD);
188e41f4b71Sopenharmony_ci    const fileAsset = await media.createAsset(mediaType, 'test.txt', path);
189e41f4b71Sopenharmony_ci    console.info('mediaLibrary fileAsset displayName: ' + fileAsset.displayName);
190e41f4b71Sopenharmony_ci    let fd = await fileAsset.open('rw');
191e41f4b71Sopenharmony_ci    console.info('mediaLibrary fileAsset open fd: ' + fd);
192e41f4b71Sopenharmony_ci    let writeLen = fs.writeSync(fd, 'hello, world');
193e41f4b71Sopenharmony_ci    console.info('write data to file succeed and size is: ' + writeLen);
194e41f4b71Sopenharmony_ci    fileAsset.close(fd);
195e41f4b71Sopenharmony_ci    media.release();
196e41f4b71Sopenharmony_ci  } catch (err) {
197e41f4b71Sopenharmony_ci    console.error('mediaLibrary fail, err: ' + err);
198e41f4b71Sopenharmony_ci  }
199e41f4b71Sopenharmony_ci}
200e41f4b71Sopenharmony_ci```
201e41f4b71Sopenharmony_ci
202e41f4b71Sopenharmony_ciAfter the change, use **FilePicker** APIs to create a file in the **Documents** and **Download** directories.
203e41f4b71Sopenharmony_ci
204e41f4b71Sopenharmony_ci1. Obtain a **DocumentViewPicker** object.
205e41f4b71Sopenharmony_ci2. Call **DocumentViewPicker.save** to create and save an empty file.
206e41f4b71Sopenharmony_ci3. After the file is saved, the file URI is returned.
207e41f4b71Sopenharmony_ci4. 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.
208e41f4b71Sopenharmony_ci5. Call [fs.writeSync](../../../application-dev/reference/apis/js-apis-file-fs.md#writesync) to edit the file based on the FD.
209e41f4b71Sopenharmony_ci6. After the edit, call [fs.closeSync](../../../application-dev/reference/apis/js-apis-file-fs.md#closesync) to close the FD.
210e41f4b71Sopenharmony_ci
211e41f4b71Sopenharmony_ci**Sample code**:
212e41f4b71Sopenharmony_ci
213e41f4b71Sopenharmony_ci```js
214e41f4b71Sopenharmony_ciimport picker from '@ohos.file.picker';
215e41f4b71Sopenharmony_ciimport fs from '@ohos.file.fs';
216e41f4b71Sopenharmony_ci
217e41f4b71Sopenharmony_cilet uri;
218e41f4b71Sopenharmony_ci
219e41f4b71Sopenharmony_ciasync function example() {
220e41f4b71Sopenharmony_ci  try {
221e41f4b71Sopenharmony_ci    let DocumentSaveOptions = new picker.DocumentSaveOptions();
222e41f4b71Sopenharmony_ci    DocumentSaveOptions.newFileNames = ['DocumentViewPicker01.txt'];
223e41f4b71Sopenharmony_ci    let documentPicker = new picker.DocumentViewPicker();
224e41f4b71Sopenharmony_ci    documentPicker.save(DocumentSaveOptions).then((DocumentSaveResult) => {
225e41f4b71Sopenharmony_ci      console.info('DocumentViewPicker.save successfully, DocumentSaveResult uri: ' + JSON.stringify(DocumentSaveResult));
226e41f4b71Sopenharmony_ci      uri = DocumentSaveResult[0];
227e41f4b71Sopenharmony_ci    }).catch((err) => {
228e41f4b71Sopenharmony_ci      console.error('DocumentViewPicker.save failed with err: ' + err);
229e41f4b71Sopenharmony_ci    });
230e41f4b71Sopenharmony_ci  } catch (err) {
231e41f4b71Sopenharmony_ci    console.error('DocumentViewPicker failed with err: ' + err);
232e41f4b71Sopenharmony_ci  }
233e41f4b71Sopenharmony_ci}
234e41f4b71Sopenharmony_ci
235e41f4b71Sopenharmony_ciasync function writeFile() {
236e41f4b71Sopenharmony_ci  try {
237e41f4b71Sopenharmony_ci    let file = fs.openSync(uri, fs.OpenMode.READ_WRITE);
238e41f4b71Sopenharmony_ci    console.info('DocumentViewPicker file fd: ' + file.fd);
239e41f4b71Sopenharmony_ci    let writeLen = fs.writeSync(file.fd, 'hello, world');
240e41f4b71Sopenharmony_ci    console.info('write data to file succeed and size is: ' + writeLen);
241e41f4b71Sopenharmony_ci    fs.closeSync(file);
242e41f4b71Sopenharmony_ci  } catch (err) {
243e41f4b71Sopenharmony_ci    console.error('DocumentViewPicker fail, err: ' + err);
244e41f4b71Sopenharmony_ci  }
245e41f4b71Sopenharmony_ci}
246e41f4b71Sopenharmony_ci```
247