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

HarmonyOS
2024-12-23 15:23:43
浏览
收藏 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%')
  }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.

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

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

分享
微博
QQ
微信
回复
2024-12-23 18:47:30
相关问题
HarmonyOS 动态申请权限申请不了
872浏览 • 1回复 待解决
HarmonyOS 系统权限申请demo——拍照
755浏览 • 1回复 待解决
动态申请权限步骤是怎样
1526浏览 • 1回复 待解决
动态申请权限能否添加描述
1748浏览 • 1回复 待解决
HarmonyOS 相机拍照选择相册功能demo
697浏览 • 1回复 待解决
HarmonyOS 相机拍照完,照片读取失败
709浏览 • 1回复 待解决
HarmonyOS 位置权限申请
1003浏览 • 1回复 待解决