HarmonyOS 拍照和选择照片的例子,包含动态权限申请

HarmonyOS
4天前
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
put_get

图库选择器对象,用来支撑选择图片/视频和保存图片/视频等用户场景。参考地址:

https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/js-apis-file-picker-V5#photoviewpickerdeprecated

参考demo如下:

import picker from '@ohos.file.picker';
import { BusinessError } from '@ohos.base';

@Entry
@Component
struct Index4 {
  @State imgDatas: string[] = [];
  @State picUrl: string = ''
  // 获取照片url集
  getAllImg() {
    try {
      let PhotoSelectOptions:picker.PhotoSelectOptions = new picker.PhotoSelectOptions();
      PhotoSelectOptions.MIMEType = picker.PhotoViewMIMETypes.IMAGE_TYPE;
      PhotoSelectOptions.maxSelectNumber = 5;
      let photoPicker:picker.PhotoViewPicker = new picker.PhotoViewPicker();
      photoPicker.select(PhotoSelectOptions).then((PhotoSelectResult:picker.PhotoSelectResult) => {
        this.imgDatas = PhotoSelectResult.photoUris;
        if (this.imgDatas.length > 0) {
          this.picUrl = this.imgDatas[0]
        }
        console.info('PhotoViewPicker.select successfully, PhotoSelectResult uri: ' + JSON.stringify(PhotoSelectResult));
      }).catch((err:Error) => {
        let message = (err as BusinessError).message;
        let code = (err as BusinessError).code;
        console.error(`PhotoViewPicker.select failed with. Code: ${code}, message: ${message}`);
      });
    } catch (err) {
      let message = (err as BusinessError).message;
      let code = (err as BusinessError).code;
      console.error(`PhotoViewPicker failed with. Code: ${code}, message: ${message}`);    }
  }


  // 使用imgDatas的url加载图片。
  build() {
    Column() {
      Image(this.picUrl)
        .width(200).margin({bottom: 10})
      Button('选择图片').onClick(()=> {
        this.getAllImg();
      }).width(120)
    }.width('100%').height('100%')
  }
}

拉起系统相机 请参考示例代码:

https://developer.huawei.com/consumer/cn/doc/harmonyos-faqs-V5/faqs-ability-60-V5

分享
微博
QQ
微信
回复
4天前
相关问题
HarmonyOS 动态申请权限申请不了
43浏览 • 1回复 待解决
动态申请权限能否添加描述
779浏览 • 1回复 待解决
动态申请权限步骤是怎样
506浏览 • 1回复 待解决
HarmonyOS 相机拍照完,照片读取失败
41浏览 • 1回复 待解决
HarmonyOS 位置权限申请
41浏览 • 1回复 待解决
HarmonyOS 仓颉权限怎么申请
44浏览 • 1回复 待解决
HarmonyOS权限申请问题
456浏览 • 1回复 待解决