从相册获取:
getAlbum() { const photoSelectOptions = new photoAccessHelper.PhotoSelectOptions(); photoSelectOptions.MIMEType = photoAccessHelper.PhotoViewMIMETypes.IMAGE_TYPE; // 过滤选择媒体文件类型为IMAGE photoSelectOptions.maxSelectNumber = 5; // 选择媒体文件的最大数目 let uris: Array<string> = []; const photoViewPicker = new photoAccessHelper.PhotoViewPicker(); photoViewPicker.select(photoSelectOptions).then((photoSelectResult: photoAccessHelper.PhotoSelectResult) => { uris = photoSelectResult.photoUris; uris.forEach(element => { this.photoList.push(element) }); console.info('photoViewPicker.select to file succeed and uris are:' + uris); }).catch((err: BusinessError) => { console.error(`Invoke photoViewPicker.select failed, code is ${err.code}, message is ${err.message}`); }) }
从相机获取:
invokeCamera(callback: (uri: string) => void) {try {let pickerProfile: cameraPicker.PickerProfile = {cameraPosition: camera.CameraPosition.CAMERA_POSITION_BACK};let pickerResult: cameraPicker.PickerResult = await cameraPicker.pick(getContext(),[cameraPicker.PickerMediaType.PHOTO, cameraPicker.PickerMediaType.VIDEO], pickerProfile);console.log("the pick pickerResult is:" + JSON.stringify(pickerResult));if (callback && pickerResult) {callback(pickerResult.resultUri);}} catch (error) {let err = error as BusinessError;console.error(`the pick call failed. error code: ${err.code}`);}}