HarmonyOS鸿蒙Next中相机拍摄照片后,返回的uri生成PixelMap时报错

HarmonyOS鸿蒙Next中相机拍摄照片后,返回的uri生成PixelMap时报错 相机拍摄照片后 , 返回的uri生成PixelMap时报错 “Cannot read property createPixelMap of undefined”

import { cameraPicker } from '@kit.CameraKit';
import { camera } from '@kit.CameraKit';
import { BusinessError } from '@ohos.base';
import { hilog } from '@kit.PerformanceAnalysisKit'
import { CommonConstants as Const } from '../common/constants/CommonConstants';
import { image } from '@kit.ImageKit';

@Entry
@Component
struct ImagePickerPage {
  @State uri: Resource | string | undefined = undefined;
  private cameraPosition: Array<camera.CameraPosition> = [
    camera.CameraPosition.CAMERA_POSITION_UNSPECIFIED, camera.CameraPosition.CAMERA_POSITION_BACK,
    camera.CameraPosition.CAMERA_POSITION_FRONT, camera.CameraPosition.CAMERA_POSITION_FOLD_INNER
  ];
  private mediaType: Array<cameraPicker.PickerMediaType> = [
    cameraPicker.PickerMediaType.PHOTO, cameraPicker.PickerMediaType.VIDEO
  ];

  build() {
    Row() {
      Column() {
        Image(this.uri)
          .height($r('app.float.image_height'))
          .alt($r('app.media.startIcon'))

        Button($r('app.string.capture'))
          .width($r('app.float.button_width'))
          .margin({ top: $r('app.float.margin') })
          .onClick(async () => {
            try {
              let pickerProfile: cameraPicker.PickerProfile = { cameraPosition: this.cameraPosition[1] };
              let pickerResult: cameraPicker.PickerResult = await cameraPicker.pick(getContext(this),
                [this.mediaType[0]], pickerProfile);
              this.uri = pickerResult.resultUri;

              image.createImageSource(this.uri).createPixelMap().then(res=>{
                console.log("-----------")
              }).catch(e:BusinessError=>{
                console.log(e.message)
              })

              hilog.info(0x0000, ' ', "the pick pickerResult is:" + JSON.stringify(pickerResult));
            } catch (error) {
              let err = error as BusinessError;
              hilog.error(0x0000, '', `the pick call failed. error code: ${err.code}`);
            }
          })
      }
      .width(Const.FULL_SIZE)
    }
    .height(Const.FULL_SIZE)
  }
}

更多关于HarmonyOS鸿蒙Next中相机拍摄照片后,返回的uri生成PixelMap时报错的实战教程也可以访问 https://www.itying.com/category-93-b0.html

3 回复

this.uri = pickerResult.resultUri;

createPixelMap的uri可能不支持什么获取这个类型

更多关于HarmonyOS鸿蒙Next中相机拍摄照片后,返回的uri生成PixelMap时报错的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


在HarmonyOS鸿蒙Next中,相机拍摄照片后返回的uri生成PixelMap时报错,可能是由于以下原因之一:

  1. uri格式或路径问题:返回的uri可能不符合预期格式或路径无效,导致无法正确生成PixelMap。
  2. 权限问题:应用可能没有足够的权限访问该uri指向的资源。
  3. 资源未加载完成:在生成PixelMap时,uri指向的资源可能尚未完全加载或已释放。
  4. API使用不当:调用生成PixelMap的API时,参数传递或使用方式可能不正确。

检查uri的有效性、权限设置以及API调用方式,确保资源可用且正确加载。

在HarmonyOS鸿蒙Next中,相机拍摄照片后返回的URI生成PixelMap时报错,可能有以下原因:

  1. URI格式问题:确保URI格式正确,且指向有效的图片文件。
  2. 文件路径权限:检查应用是否有读取该URI对应文件的权限。
  3. 文件不存在或损坏:确认文件确实存在且未损坏。
  4. 内存不足:生成PixelMap时可能因内存不足导致失败,建议优化内存使用。

建议使用ImageSource.createFromUri(uri)创建ImageSource,再通过createPixelmap()生成PixelMap,并捕获异常以获取详细错误信息。

回到顶部