文档开发者中心
我的
|

HarmonyOS 5

使用FilePick上传uri失败,报错201

问题现象

使用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%') 
  } 
}

参考链接

选择用户文件

点赞
收藏
回复
分享
举报
浏览98 发布于2024-01-24 02:44北京
全部评论
最多点赞
最新发布
最早发布
暂无评论
暂无评论
写回答
新增插入模板功能
一键使用模板,快速填写内容,轻松发帖~
知道了
  • 为了保障您的信息安全,请勿上传您的敏感个人信息(如您的密码等信息)和您的敏感资产信息(如关键源代码、签名私钥、调试安装包、业务日志等信息),且您需自行承担由此产生的信息泄露等安全风险。
  • 如您发布的内容为转载内容,请注明内容来源。

我要发帖子

了解社区公约,与您携手共创和谐专业的开发者社区。