#鸿蒙通关秘籍#如何在HarmonyOS NEXT中从相册选择照片

HarmonyOS
2天前
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
网络小先锋

使用photoAccessHelper模块从相册中选择照片:

import { photoAccessHelper } from '@kit.MediaLibraryKit';

async function selectPhotoOrTakePhoto() {
  const photoSelectOptions = new photoAccessHelper.PhotoSelectOptions();
  photoSelectOptions.MIMEType = photoAccessHelper.PhotoViewMIMETypes.IMAGE_TYPE;
  photoSelectOptions.maxSelectNumber = 1;

  const photoViewPicker = new photoAccessHelper.PhotoViewPicker();
  try {
    const photoSelectResult = await photoViewPicker.select(photoSelectOptions);
    if (photoSelectResult.photoUris.length > 0) {
      const photoUri = photoSelectResult.photoUris[0];
      handleSelectedPhoto(photoUri);
    }
  } catch (error) {
    console.error('Failed to select photo or take photo.', error);
  }
}

function handleSelectedPhoto(photoUri) {
  copyFileToCache(photoUri, context).then((filePath) => {
    uploadPhoto(filePath);
  });
}
分享
微博
QQ
微信
回复
2天前
相关问题