HarmonyOS 使用picker组件从相册获取到图片之后存储到沙盒中,图片无法使用

通过@ohos.file.picker选图后,通过fs将图片写入到沙盒中,图片无法使用

代码:

第一步picker选图:

pickImageInAlbum(photoSelectOptions: picker.PhotoSelectOptions): Promise<picker.PhotoSelectResult> {
  return new Promise((resolve, reject) => {
  try {
  let photoPicker = new picker.PhotoViewPicker();
  photoPicker.select(photoSelectOptions).then((PhotoSelectResult: picker.PhotoSelectResult) => {
  resolve(PhotoSelectResult)
}).catch((err: BusinessError) => {
  reject(err)
});
} catch (error) {
  let err: BusinessError = error as BusinessError;
  reject(err)
}
})
}
let PhotoSelectOptions = new picker.PhotoSelectOptions();
PhotoSelectOptions.MIMEType = picker.PhotoViewMIMETypes.IMAGE_TYPE;
PhotoSelectOptions.maxSelectNumber = params.count;

第二步,写入图片:

const result: picker.PhotoSelectResult = await this.pickImageInAlbum(PhotoSelectOptions)
const isOriginalPhoto: boolean = result.isOriginalPhoto
const photo = result.photoUris[0]
const pixelMap = await this.getThumbnail(asset)
let pixelBytesNumber = pixelMap.getPixelBytesNumber();
const readBuffer = new ArrayBuffer(pixelBytesNumber);
pixelMap.readPixelsToBuffer(readBuffer).then(() => {
  let pixelmapArrayBuffer: ArrayBuffer = readBuffer;
  let filesDir: string = getContext(ImageCtor.mContext).filesDir
  let filePath = filesDir + "/xxxxcc.jpg";
  let file = fs.openSync(filePath, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE);
  fs.writeSync(file.fd, pixelmapArrayBuffer);
  fs.fsyncSync(file.fd);
  const res = fs.accessSync(filePath)
  fs.closeSync(file);
})

最终看divice file browser中存在该图片文件,但是破损无法使用

HarmonyOS
2天前
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
aquaa

可以参考如下文档实现保存文件:

https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/save-user-file-V5

也可以参考如下demo:

saveImage() {
  let uris: Array<string> = [];
  const photoSelectOptions = new picker.PhotoSelectOptions();

  photoSelectOptions.MIMEType = picker.PhotoViewMIMETypes.IMAGE_TYPE; // 过滤选择媒体文件类型为IMAGE
  photoSelectOptions.maxSelectNumber = 5; // 选择媒体文件的最大数目

  const photoViewPicker = new picker.PhotoViewPicker();
  photoViewPicker.select(photoSelectOptions).then(async (photoSelectResult: picker.PhotoSelectResult) => {
    uris = photoSelectResult.photoUris;
    console.info('photoViewPicker.select to file succeed and uris are:' + uris);
    let context = getContext(this);

    let file1 = fs.openSync(uris[0], fs.OpenMode.READ_ONLY);


    fs.stat(file1.fd).then((stat: fs.Stat) => {
      console.info("get file info succeed, the size of file is " + stat.size);
      let buffer = new ArrayBuffer(stat.size);
      let readLen = fs.readSync(file1.fd, buffer);
      let filePath = context.filesDir + "/test.png";
      console.log("testTag-filePath:" + filePath);
      let file = fs.openSync(filePath, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE);

      let writeLen = fs.writeSync(file.fd, buffer);
      console.info("testTag-write data to file succeed and size is:" + writeLen);
      fs.closeSync(file);

    }).catch((err: BusinessError) => {
      console.error("get file info failed with error message: " + err.message + ", error code: " + err.code);
    });
  })
}
分享
微博
QQ
微信
回复
2天前
相关问题
HarmonyOS 相册图片存储
21浏览 • 1回复 待解决
HarmonyOS 截图保存图片相册
52浏览 • 1回复 待解决
HarmonyOS 实现保存图片相册
86浏览 • 1回复 待解决
使用imageSource如何存储图片
454浏览 • 1回复 待解决
HarmonyOS 关于相册中选择图片上传
87浏览 • 1回复 待解决
HarmonyOS 保存图片本地相册
57浏览 • 1回复 待解决
HarmonyOS 保存图片相册问题
430浏览 • 1回复 待解决
HarmonyOS 保存图片系统相册
39浏览 • 1回复 待解决
如何保存网络图片相册
788浏览 • 1回复 待解决