IT评测·应用市场-qidao123.com

标题: 鸿蒙5.0&next开发【用户相册资源使用】媒体服务 [打印本页]

作者: 美丽的神话    时间: 2025-3-15 06:10
标题: 鸿蒙5.0&next开发【用户相册资源使用】媒体服务
用户相册资源使用指导

photoAccessHelper提供用户相册相干的接口,供开发者创建、删除用户相册,往用户相册中添加和删除图片和视频资源等。
   说明:
  在进行功能开发前,请开发者查阅[开发准备],相识如何获取相册管理模块实例和如何申请相册管理模块功能开发相干权限。 文档中使用到photoAccessHelper的地方默认为使用开发准备中获取的对象,如未添加此段代码报photoAccessHelper未定义的错误请自行添加。
  为了保证应用的运行服从,大部门photoAccessHelper的接口调用都是异步的。以下异步调用的API示例均采用Promise函数。 如无特殊说明,文档中涉及的待获取的资源均视为已经预置且在数据库中存在相应数据。如出现按照示例代码实行出现获取资源为空的环境请确认文件是否已预置,数据库中是否存在该文件的数据。
创建用户相册(仅向系统应用开放)

通过[MediaAlbumChangeRequest.createAlbumRequest]和[PhotoAccessHelper.applyChanges]接口创建用户相册。
待创建的相册名参数规格为:

前提条件

下面以创建一个用户相册为例。
开发步调
  1. import { photoAccessHelper } from '@kit.MediaLibraryKit';
  2. const context = getContext(this);
  3. let phAccessHelper = photoAccessHelper.getPhotoAccessHelper(context);
  4. async function example() {
  5.   try {
  6.     let albumName = 'albumName';
  7.     let albumChangeRequest: photoAccessHelper.MediaAlbumChangeRequest = photoAccessHelper.MediaAlbumChangeRequest.createAlbumRequest(context, albumName);
  8.     await phAccessHelper.applyChanges(albumChangeRequest);
  9.     let album: photoAccessHelper.Album = albumChangeRequest.getAlbum();
  10.     console.info('create album successfully, album name: ' + album.albumName + ' uri: ' + album.albumUri);
  11.   } catch (err) {
  12.     console.error('create album failed with err: ' + err);
  13.   }
  14. }
  15. ts
复制代码
获取用户相册

通过[PhotoAccessHelper.getAlbums]接口获取用户相册。
前提条件

下面以获取一个相册名为’albumName’的用户相册为例。
开发步调
  1. import { dataSharePredicates } from '@kit.ArkData';
  2. import { photoAccessHelper } from '@kit.MediaLibraryKit';
  3. const context = getContext(this);
  4. let phAccessHelper = photoAccessHelper.getPhotoAccessHelper(context);
  5. async function example() {
  6.   let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates();
  7.   let albumName: photoAccessHelper.AlbumKeys = photoAccessHelper.AlbumKeys.ALBUM_NAME;
  8.   predicates.equalTo(albumName, 'albumName');
  9.   let fetchOptions: photoAccessHelper.FetchOptions = {
  10.     fetchColumns: [],
  11.     predicates: predicates
  12.   };
  13.   try {
  14.     let fetchResult: photoAccessHelper.FetchResult<photoAccessHelper.Album> = await phAccessHelper.getAlbums(photoAccessHelper.AlbumType.USER, photoAccessHelper.AlbumSubtype.USER_GENERIC, fetchOptions);
  15.     let album: photoAccessHelper.Album = await fetchResult.getFirstObject();
  16.     console.info('getAlbums successfully, albumName: ' + album.albumName);
  17.     fetchResult.close();
  18.   } catch (err) {
  19.     console.error('getAlbums failed with err: ' + err);
  20.   }
  21. }
  22. ts
复制代码
重定名用户相册

重定名用户相册修改的是相册的Album.albumName属性。
调用[MediaAlbumChangeRequest.setAlbumName]重定名用户相册后再通过[PhotoAccessHelper.applyChanges]更新到数据库中完成修改。
在重定名用户相册之前,必要先获取相册对象,可以通过[FetchResult]中的接口获取对应位置的用户相册。
重定名的相册名参数规格为:

前提条件

下面以将一个相册名为’albumName’的用户相册重定名为例。
开发步调
  1. import { dataSharePredicates } from '@kit.ArkData';
  2. import { photoAccessHelper } from '@kit.MediaLibraryKit';
  3. const context = getContext(this);
  4. let phAccessHelper = photoAccessHelper.getPhotoAccessHelper(context);
  5. async function example() {
  6.   let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates();
  7.   let albumName: photoAccessHelper.AlbumKeys = photoAccessHelper.AlbumKeys.ALBUM_NAME;
  8.   predicates.equalTo(albumName, 'albumName');
  9.   let fetchOptions: photoAccessHelper.FetchOptions = {
  10.     fetchColumns: [],
  11.     predicates: predicates
  12.   };
  13.   try {
  14.     let fetchResult: photoAccessHelper.FetchResult<photoAccessHelper.Album> = await phAccessHelper.getAlbums(photoAccessHelper.AlbumType.USER, photoAccessHelper.AlbumSubtype.USER_GENERIC, fetchOptions);
  15.     let album: photoAccessHelper.Album = await fetchResult.getFirstObject();
  16.     console.info('getAlbums successfully, albumName: ' + album.albumName);
  17.     let albumChangeRequest: photoAccessHelper.MediaAlbumChangeRequest = new photoAccessHelper.MediaAlbumChangeRequest(album);
  18.     let newAlbumName: string = 'newAlbumName';
  19.     albumChangeRequest.setAlbumName(newAlbumName);
  20.     await phAccessHelper.applyChanges(albumChangeRequest);
  21.     console.info('setAlbumName successfully, new albumName: ' + album.albumName);
  22.     fetchResult.close();
  23.   } catch (err) {
  24.     console.error('setAlbumName failed with err: ' + err);
  25.   }
  26. }
  27. ts
复制代码
添加图片和视频到用户相册中

先[获取用户相册]对象和必要添加到相册中的图片或视频的对象数组,然后调用MediaAlbumChangeRequest.addAssets和[PhotoAccessHelper.applyChanges]接口往用户相册中添加图片或视频。
前提条件

下面以将往相册名为’albumName’的用户相册中添加一张图片为例。
开发步调
  1. import { dataSharePredicates } from '@kit.ArkData';
  2. import { photoAccessHelper } from '@kit.MediaLibraryKit';
  3. const context = getContext(this);
  4. let phAccessHelper = photoAccessHelper.getPhotoAccessHelper(context);
  5. async function example() {
  6.   let albumPredicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates();
  7.   let albumName: photoAccessHelper.AlbumKeys = photoAccessHelper.AlbumKeys.ALBUM_NAME;
  8.   albumPredicates.equalTo(albumName, 'albumName');
  9.   let albumFetchOptions: photoAccessHelper.FetchOptions = {
  10.     fetchColumns: [],
  11.     predicates: albumPredicates
  12.   };
  13.   let photoPredicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates();
  14.   let photoFetchOptions: photoAccessHelper.FetchOptions = {
  15.     fetchColumns: [],
  16.     predicates: photoPredicates
  17.   };
  18.   try {
  19.     let albumFetchResult: photoAccessHelper.FetchResult<photoAccessHelper.Album> = await phAccessHelper.getAlbums(photoAccessHelper.AlbumType.USER, photoAccessHelper.AlbumSubtype.USER_GENERIC, albumFetchOptions);
  20.     let album: photoAccessHelper.Album = await albumFetchResult.getFirstObject();
  21.     console.info('getAlbums successfully, albumName: ' + album.albumName);
  22.     let photoFetchResult: photoAccessHelper.FetchResult<photoAccessHelper.PhotoAsset> = await phAccessHelper.getAssets(photoFetchOptions);
  23.     let photoAsset: photoAccessHelper.PhotoAsset = await photoFetchResult.getFirstObject();
  24.     console.info('getAssets successfully, albumName: ' + photoAsset.displayName);
  25.     let albumChangeRequest: photoAccessHelper.MediaAlbumChangeRequest = new photoAccessHelper.MediaAlbumChangeRequest(album);
  26.     albumChangeRequest.addAssets([photoAsset]);
  27.     await phAccessHelper.applyChanges(albumChangeRequest);
  28.     console.info('succeed to add ' + photoAsset.displayName + ' to ' + album.albumName);
  29.     albumFetchResult.close();
  30.     photoFetchResult.close();
  31.   } catch (err) {
  32.     console.error('addAssets failed with err: ' + err);
  33.   }
  34. }
  35. ts
复制代码
获取用户相册中的图片和视频

先[获取用户相册]对象,然后调用[Album.getAssets]接口获取用户相册中的图片资源。
前提条件

下面以获取相册名为’albumName’的用户相册中的一张图片为例。
开发步调
  1. import { dataSharePredicates } from '@kit.ArkData';
  2. import { photoAccessHelper } from '@kit.MediaLibraryKit';
  3. const context = getContext(this);
  4. let phAccessHelper = photoAccessHelper.getPhotoAccessHelper(context);
  5. async function example() {
  6.   let albumPredicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates();
  7.   let albumName: photoAccessHelper.AlbumKeys = photoAccessHelper.AlbumKeys.ALBUM_NAME;
  8.   albumPredicates.equalTo(albumName, 'albumName');
  9.   let albumFetchOptions: photoAccessHelper.FetchOptions = {
  10.     fetchColumns: [],
  11.     predicates: albumPredicates
  12.   };
  13.   let photoPredicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates();
  14.   let photoFetchOptions: photoAccessHelper.FetchOptions = {
  15.     fetchColumns: [],
  16.     predicates: photoPredicates
  17.   };
  18.   try {
  19.     let albumFetchResult: photoAccessHelper.FetchResult<photoAccessHelper.Album> = await phAccessHelper.getAlbums(photoAccessHelper.AlbumType.USER, photoAccessHelper.AlbumSubtype.USER_GENERIC, albumFetchOptions);
  20.     let album: photoAccessHelper.Album = await albumFetchResult.getFirstObject();
  21.     console.info('getAlbums successfully, albumName: ' + album.albumName);
  22.     let photoFetchResult = await album.getAssets(photoFetchOptions);
  23.     let photoAsset = await photoFetchResult.getFirstObject();
  24.     console.info('album getAssets successfully, albumName: ' + photoAsset.displayName);
  25.     albumFetchResult.close();
  26.     photoFetchResult.close();
  27.   } catch (err) {
  28.     console.error('album getAssets failed with err: ' + err);
  29.   }
  30. }
  31. ts
复制代码
从用户相册中移除图片和视频

先[获取用户相册]对象,然后调用[Album.getAssets]接口获取用户相册中的资源。
选择其中要移除的资源,然后调用[MediaAlbumChangeRequest.removeAssets]和[PhotoAccessHelper.applyChanges]接口移除。
前提条件

下面以将往相册名为’albumName’的用户相册中移除一张图片为例。
开发步调
  1. import { dataSharePredicates } from '@kit.ArkData';
  2. import { photoAccessHelper } from '@kit.MediaLibraryKit';
  3. const context = getContext(this);
  4. let phAccessHelper = photoAccessHelper.getPhotoAccessHelper(context);
  5. async function example() {
  6.   let albumPredicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates();
  7.   let albumName: photoAccessHelper.AlbumKeys = photoAccessHelper.AlbumKeys.ALBUM_NAME;
  8.   albumPredicates.equalTo(albumName, 'albumName');
  9.   let albumFetchOptions: photoAccessHelper.FetchOptions = {
  10.     fetchColumns: [],
  11.     predicates: albumPredicates
  12.   };
  13.   let photoPredicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates();
  14.   let photoFetchOptions: photoAccessHelper.FetchOptions = {
  15.     fetchColumns: [],
  16.     predicates: photoPredicates
  17.   };
  18.   try {
  19.     let albumFetchResult: photoAccessHelper.FetchResult<photoAccessHelper.Album> = await phAccessHelper.getAlbums(photoAccessHelper.AlbumType.USER, photoAccessHelper.AlbumSubtype.USER_GENERIC, albumFetchOptions);
  20.     let album: photoAccessHelper.Album = await albumFetchResult.getFirstObject();
  21.     console.info('getAlbums successfully, albumName: ' + album.albumName);
  22.     let photoFetchResult = await album.getAssets(photoFetchOptions);
  23.     let photoAsset = await photoFetchResult.getFirstObject();
  24.     console.info('album getAssets successfully, albumName: ' + photoAsset.displayName);
  25.     let albumChangeRequest: photoAccessHelper.MediaAlbumChangeRequest = new photoAccessHelper.MediaAlbumChangeRequest(album);
  26.     albumChangeRequest.removeAssets([photoAsset]);
  27.     await phAccessHelper.applyChanges(albumChangeRequest);
  28.     console.info('succeed to remove ' + photoAsset.displayName + ' from ' + album.albumName);
  29.     albumFetchResult.close();
  30.     photoFetchResult.close();
  31.   } catch (err) {
  32.     console.error('removeAssets failed with err: ' + err);
  33.   }
  34. }
  35. ts
复制代码
删除用户相册(仅向系统应用开放)

先[获取用户相册]对象,然后调用[MediaAlbumChangeRequest.deleteAlbums]删除用户相册。
前提条件

下面以删除一个相册名为’albumName’的用户相册为例。
开发步调
  1. import { dataSharePredicates } from '@kit.ArkData';
  2. import { photoAccessHelper } from '@kit.MediaLibraryKit';
  3. const context = getContext(this);
  4. let phAccessHelper = photoAccessHelper.getPhotoAccessHelper(context);
  5. async function example() {
  6.   let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates();
  7.   let albumName: photoAccessHelper.AlbumKeys = photoAccessHelper.AlbumKeys.ALBUM_NAME;
  8.   predicates.equalTo(albumName, 'albumName');
  9.   let fetchOptions: photoAccessHelper.FetchOptions = {
  10.     fetchColumns: [],
  11.     predicates: predicates
  12.   };
  13.   try {
  14.     let fetchResult: photoAccessHelper.FetchResult<photoAccessHelper.Album> = await phAccessHelper.getAlbums(photoAccessHelper.AlbumType.USER, photoAccessHelper.AlbumSubtype.USER_GENERIC, fetchOptions);
  15.     let album: photoAccessHelper.Album = await fetchResult.getFirstObject();
  16.     console.info('getAlbums successfully, albumName: ' + album.albumName);
  17.     await photoAccessHelper.MediaAlbumChangeRequest.deleteAlbums(context, [album]);
  18.     fetchResult.close();
  19.   } catch (err) {
  20.     console.error('deleteAlbums failed with err: ' + err);
  21.   }
  22. }
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。




欢迎光临 IT评测·应用市场-qidao123.com (https://dis.qidao123.com/) Powered by Discuz! X3.4