问题现象使用FilePick上传uri失败,并报错:"Invoke photoViewPicker.select failed, code is 201, message is Permission verification failed"。
解决措施picker中获取的uri,不能在其回调函数中使用fs进行读写操作。参考如下demo:import { image } from '@kit.ImageKit'; import { picker, fileIo } from '@kit.CoreFileKit'; import { BusinessError } from '@kit.BasicServicesKit'; @Entry @Component struct PixelMapToPhotoPage { @State message: string = '选择图片并解析'; @State text: string = ''; private url: string | null = null; onPageShow() { if (!this.url) { let pp = new picker.PhotoSelectOptions(); pp.MIMEType = picker.PhotoViewMIMETypes.IMAGE_TYPE; pp.maxSelectNumber = 1; let photoPicker = new picker.PhotoViewPicker(); photoPicker.select(pp).then((selectResult) => { this.url = selectResult.photoUris[0]; console.info("AAAAAA ****** select photo success : " + this.url) }).catch((err: BusinessError) => { console.info("AAAAAA ****** error is : " + err) }) } } selectPhotoAndSave(url: string | null) { if (this.url) { let file = fileIo.openSync(url, fileIo.OpenMode.READ_ONLY); let source = image.createImageSource(file.fd); source.createPixelMap().then((pixelMap) => { this.text = "图片大小为:" + pixelMap.getPixelBytesNumber() + "B"; }) } else { console.info("AAAAAA ****** url is null") } } build() { Row() { Column() { Text(this.message) .fontSize(20) .fontWeight(FontWeight.Bold) Button("图片解析") .fontSize(20) .margin({ top: 10 }) .onClick(() => { this.selectPhotoAndSave(this.url); }) Text(this.text) .fontSize(20) .width(200) .height(200) .borderWidth(1) .borderRadius(15) .margin({ top: 10 }) } .width('100%') } .height('100%') } }参考链接选择用户文件
解决措施
picker中获取的uri,不能在其回调函数中使用fs进行读写操作。参考如下demo:
参考链接
选择用户文件