openharmony 鸿蒙 changelogs-mediaLibrary

2023-06-24 浏览 (742)

File Subsystem Changelog

cl.file.1 mediaLibrary APIs Changed

All APIs provided by the mediaLibrary module of the multimedia subsystem are deprecated.

Change Impact

All APIs described in mediaLibrary are deprecated. Third-party applications can only select and save files in the public directory by calling the APIs of FilePicker. For applications developed based on earlier versions, pay attention to the changes of APIs.

Key API/Component Changes

The table below lists the mediaLibrary APIs that can be substituted by the FilePicker APIs.

ModuleMethod/Attribute/Enum/ConstantChange Type
medialibraryfunction getMediaLibrary(context: Context): MediaLibrary;Deprecated
medialibraryfunction getFileAssets(options: MediaFetchOptions, callback: AsyncCallback<FetchFileResult>): voidDeprecated
medialibraryfunction getFileAssets(options: MediaFetchOptions): Promise<FetchFileResult>Deprecated
medialibraryfunction createAsset(mediaType: MediaType, displayName: string, relativePath: string, callback: AsyncCallback<FileAsset>): voidDeprecated
medialibraryfunction createAsset(mediaType: MediaType, displayName: string, relativePath: string): Promise<FileAsset>Deprecated
medialibraryfunction getPublicDirectory(type: DirectoryType, callback: AsyncCallback<string>): voidDeprecated
medialibraryfunction getPublicDirectory(type: DirectoryType): Promise<string>Deprecated
medialibraryfunction release(callback: AsyncCallback<void>): voidDeprecated
medialibraryfunction release(): Promise<void>Deprecated
medialibraryfunction FileAsset.open(mode: string, callback: AsyncCallback<number>): voidDeprecated
medialibraryfunction FileAsset.open(mode: string): Promise<number>Deprecated
medialibraryfunction FileAsset.close(fd: number, callback: AsyncCallback<void>): voidDeprecated
medialibraryfunction FileAsset.close(fd: number): Promise<void>Deprecated
medialibraryfunction FetchFileResult.getCount(): numberDeprecated
medialibraryfunction FetchFileResult.isAfterLast(): booleanDeprecated
medialibraryfunction FetchFileResult.close(): voidDeprecated
medialibraryfunction FetchFileResult.getFirstObject(callback: AsyncCallback<FileAsset>): voidDeprecated
medialibraryfunction FetchFileResult.getFirstObject(): Promise<FileAsset>Deprecated
medialibraryfunction FetchFileResult.getNextObject(callback: AsyncCallback<FileAsset>): voidDeprecated
medialibraryfunction FetchFileResult.getNextObject(): Promise<FileAsset>Deprecated
medialibraryfunction FetchFileResult.getLastObject(callback: AsyncCallback<FileAsset>): voidDeprecated
medialibraryfunction FetchFileResult.getLastObject(): Promise<FileAsset>Deprecated
medialibraryfunction FetchFileResult.getPositionObject(index: number, callback: AsyncCallback<FileAsset>): voidDeprecated
medialibraryfunction FetchFileResult.getPositionObject(index: number): Promise<FileAsset>Deprecated
medialibraryfunction FetchFileResult.getAllObject(callback: AsyncCallback<Array<FileAsset>>): voidDeprecated
medialibraryfunction FetchFileResult.getAllObject(): Promise<Array<FileAsset>>Deprecated
medialibraryfunction Album.getFileAssets(options: MediaFetchOptions, callback: AsyncCallback<FetchFileResult>): voidDeprecated
medialibraryfunction Album.getFileAssets(options?: MediaFetchOptions): Promise<FetchFileResult>Deprecated
medialibraryenum FileKeyDeprecated
medialibraryenum DirectoryTypeDeprecated
medialibraryenum MediaTypeDeprecated
medialibraryinterface MediaFetchOptionsDeprecated
medialibraryinterface FileAssetDeprecated

Adaptation Guide

The following example shows how to use the mediaLibrary APIs to edit a file in the public directory:

  1. Call getMediaLibrary to obtain a mediaLibrary instance.
  2. Create a MediaFetchOptions object, and call getFileAssets to obtain files in the public directory.
  3. Call the FetchFileResult APIs to obtain the file asset of the target file.
  4. Call fileAsset.open to open the file and obtain the FD.
  5. Call fs.writeSync to edit the file through the FD.
  6. After the edit, call fileAsset.close to close the FD of the file.
  7. Call fetchFileResult.close to release the resources occupied by getFileAssets.
  8. Call release to release the mediaLibrary instance.

Example

import mediaLibrary from '@ohos.multimedia.mediaLibrary';
import fs from '@ohos.file.fs';

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()],
      order: fileKeyObj.DATE_ADDED + ' DESC',
    };
    const fetchFileResult = await media.getFileAssets(getImageOp);
    const fileAsset = await fetchFileResult.getFirstObject();
    console.info('mediaLibrary fileAsset displayName: ' + fileAsset.displayName);
    let fd = await fileAsset.open('rw');
    console.info('mediaLibrary fileAsset open fd: ' + fd);
    let writeLen = fs.writeSync(fd, 'hello, world');
    console.info('write data to file succeed and size is: ' + writeLen);
    fileAsset.close(fd);
    fetchFileResult.close();
    media.release();
  } catch (err) {
    console.error('mediaLibrary fail, err: ' + err);
  }
}

The following example shows how to use the FilePicker APIs to edit a file in the public directory:

  1. Obtain a DocumentViewPicker object.
  2. Call DocumentViewPicker.select to select a file.
  3. After a file is selected, a file URI is returned.
  4. After the UI is returned from DocumentViewPicker, call fs.openSync to open the file based on the URI and obtain the FD.
  5. Call fs.writeSync to edit the file through the FD.
  6. After the edit, call fs.closeSync to close the FD.

Example

import mediaLibrary from '@ohos.multimedia.mediaLibrary';
import picker from '@ohos.file.picker';
import fs from '@ohos.file.fs';

let uri;

async function example() {
  try {
    let DocumentSelectOptions = new picker.DocumentSelectOptions();
    let documentPicker = new picker.DocumentViewPicker();
    documentPicker.select(DocumentSelectOptions).then((DocumentSelectResult) => {
      console.info('DocumentViewPicker.select successfully, DocumentSelectResult uri: ' + JSON.stringify(DocumentSelectResult));
      uri = DocumentSelectResult[0];
    }).catch((err) => {
      console.error('DocumentViewPicker.select failed with err: ' + err);
    });
  } catch (err) {
    console.error('DocumentViewPicker failed with err: ' + err);
  }
}

async function writeFile() {
  try {
    let file = fs.openSync(uri, fs.OpenMode.READ_WRITE);
    console.info('DocumentViewPicker file fd: ' + file.fd);
    let writeLen = fs.writeSync(file.fd, 'hello, world');
    console.info('write data to file succeed and size is: ' + writeLen);
    fs.closeSync(file);
  } catch (err) {
    console.error('DocumentViewPicker fail, err: ' + err);
  }
}

The following example shows how to use the mediaLibrary APIs to create a file in the public directory:

  1. Call getMediaLibrary to obtain a mediaLibrary instance.
  2. Call getPublicDirectory to obtain the path of the public directory.
  3. Call createAsset to create a file and obtain the file asset.
  4. Call fileAsset.open to open the file and obtain the FD.
  5. Call fs.write to edit the file through the FD.
  6. After the edit, call fileAsset.close to close the FD.
  7. Call release to release the mediaLibrary instance.

Example

import mediaLibrary from '@ohos.multimedia.mediaLibrary';
import fs from '@ohos.file.fs';

async function example() {
  try {
    let context = getContext(this);
    let media = mediaLibrary.getMediaLibrary(context);
    let mediaType = mediaLibrary.MediaType.FILE;
    let DIR_DOWNLOAD = mediaLibrary.DirectoryType.DIR_DOWNLOAD;
    const path = await media.getPublicDirectory(DIR_DOWNLOAD);
    const fileAsset = await media.createAsset(mediaType, 'test.txt', path);
    console.info('mediaLibrary fileAsset displayName: ' + fileAsset.displayName);
    let fd = await fileAsset.open('rw');
    console.info('mediaLibrary fileAsset open fd: ' + fd);
    let writeLen = fs.writeSync(fd, 'hello, world');
    console.info('write data to file succeed and size is: ' + writeLen);
    fileAsset.close(fd);
    media.release();
  } catch (err) {
    console.error('mediaLibrary fail, err: ' + err);
  }
}

The following example shows how to use the FilePicker APIs to create a file in the public directory:

  1. Obtain a DocumentViewPicker object.
  2. Call DocumentViewPicker.save to create and save an empty file.
  3. After the file is saved, a file URI is returned.
  4. After the UI is returned from DocumentViewPicker, call fs.openSync to open the file based on the URI and obtain the FD.
  5. Call fs.writeSync to edit the file through the FD.
  6. After the edit, call fs.closeSync to close the FD.

Example

import mediaLibrary from '@ohos.multimedia.mediaLibrary';
import picker from '@ohos.file.picker';
import fs from '@ohos.file.fs';

let uri;

async function example() {
  try {
    let DocumentSaveOptions = new picker.DocumentSaveOptions();
    DocumentSaveOptions.newFileNames = ['DocumentViewPicker01.txt'];
    let documentPicker = new picker.DocumentViewPicker();
    documentPicker.save(DocumentSaveOptions).then((DocumentSaveResult) => {
      console.info('DocumentViewPicker.save successfully, DocumentSaveResult uri: ' + JSON.stringify(DocumentSaveResult));
      uri = DocumentSaveResult[0];
    }).catch((err) => {
      console.error('DocumentViewPicker.save failed with err: ' + err);
    });
  } catch (err) {
    console.error('DocumentViewPicker failed with err: ' + err);
  }
}

async function writeFile() {
  try {
    let file = fs.openSync(uri, fs.OpenMode.READ_WRITE);
    console.info('DocumentViewPicker file fd: ' + file.fd);
    let writeLen = fs.writeSync(file.fd, 'hello, world');
    console.info('write data to file succeed and size is: ' + writeLen);
    fs.closeSync(file);
  } catch (err) {
    console.error('DocumentViewPicker fail, err: ' + err);
  }
}

Key API/Component Changes

The 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.

ModuleMethod/Attribute/Enum/ConstantChange Type
medialibraryfunction getMediaLibrary(): MediaLibrary;Deprecated
medialibraryfunction on(type: 'deviceChange'|'albumChange'|'imageChange'|'audioChange'|'videoChange'|'fileChange'|'remoteFileChange', callback: Callback<void>): voidDeprecated
medialibraryfunction off(type: 'deviceChange'|'albumChange'|'imageChange'|'audioChange'|'videoChange'|'fileChange'|'remoteFileChange', callback?: Callback<void>): voidDeprecated
medialibraryfunction deleteAsset(uri: string): Promise<void>Deprecated
medialibraryfunction deleteAsset(uri: string, callback: AsyncCallback<void>): voidDeprecated
medialibraryfunction storeMediaAsset(option: MediaAssetOption, callback: AsyncCallback<string>): voidDeprecated
medialibraryfunction storeMediaAsset(option: MediaAssetOption): Promise<string>Deprecated
medialibraryfunction startImagePreview(images: Array<string>, index: number, callback: AsyncCallback<void>): voidDeprecated
medialibraryfunction startImagePreview(images: Array<string>, callback: AsyncCallback<void>): voidDeprecated
medialibraryfunction startImagePreview(images: Array<string>, index?: number): Promise<void>Deprecated
medialibraryfunction startMediaSelect(option: MediaSelectOption, callback: AsyncCallback<Array<string>>): voidDeprecated
medialibraryfunction startMediaSelect(option: MediaSelectOption): Promise<Array<string>>Deprecated
medialibraryfunction getActivePeers(): Promise<Array<PeerInfo>>;Deprecated
medialibraryfunction getActivePeers(callback: AsyncCallback<Array<PeerInfo>>): void;Deprecated
medialibraryfunction getAllPeers(): Promise<Array<PeerInfo>>;Deprecated
medialibraryfunction FileAsset.isDirectory(callback: AsyncCallback<boolean>): voidDeprecated
medialibraryfunction FileAsset.isDirectory():Promise<boolean>Deprecated
medialibraryfunction FileAsset.commitModify(callback: AsyncCallback<void>): voidDeprecated
medialibraryfunction FileAsset.commitModify(): Promise<void>Deprecated
medialibraryfunction FileAsset.getThumbnail(callback: AsyncCallback<image.PixelMap>): voidDeprecated
medialibraryfunction FileAsset.getThumbnail(size: Size, callback: AsyncCallback<image.PixelMap>): voidDeprecated
medialibraryfunction FileAsset.getThumbnail(size?: Size): Promise<image.PixelMap>Deprecated
medialibraryfunction FileAsset.favorite(isFavorite: boolean, callback: AsyncCallback<void>): voidDeprecated
medialibraryfunction FileAsset.favorite(isFavorite: boolean): Promise<void>Deprecated
medialibraryfunction FileAsset.isFavorite(callback: AsyncCallback<boolean>): voidDeprecated
medialibraryfunction FileAsset.isFavorite():Promise<boolean>Deprecated
medialibraryfunction FileAsset.trash(isTrash: boolean, callback: AsyncCallback<void>): voidDeprecated
medialibraryfunction FileAsset.trash(isTrash: boolean): Promise<void>Deprecated
medialibraryfunction FileAsset.isTrash(callback: AsyncCallback<boolean>): voidDeprecated
medialibraryfunction FileAsset.isTrash():Promise<boolean>Deprecated
medialibraryfunction getAlbums(options: MediaFetchOptions, callback: AsyncCallback<Array<Album>>): voidDeprecated
medialibraryfunction getAlbums(options: MediaFetchOptions): Promise<Array<Album>>Deprecated
medialibraryfunction Album.commitModify(callback: AsyncCallback<void>): voidDeprecated
medialibraryfunction Album.commitModify(): Promise<void>Deprecated
medialibraryenum DeviceTypeDeprecated
medialibraryinterface PeerInfoDeprecated
medialibraryinterface SizeDeprecated
medialibraryinterface MediaAssetOptionDeprecated
medialibraryinterface MediaSelectOptionDeprecated

你可能感兴趣的鸿蒙文章

harmony 鸿蒙Ability Subsystem Changelog

harmony 鸿蒙Upload and Download Subsystem Changelog

harmony 鸿蒙Telephony Subsystem Changelog

  • 所属分类: 后端技术
  • 本文标签: 软件 鸿蒙
  • 版权声明: 本文链接 https://seaxiang.com/blog/ab0f946249174470ba3da63320d8d5ae