HarmonyOS 拍照后对应的图片保存到本地后无法打开

拍照后, 获取到uri, 再存到本地后,去读取文件 , 无法打开 。用Device file Browser 导出保存的问价 , 提示已损坏 。

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

可参考:

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'; 
 
import fs from '@ohos.file.fs'; 
import { util } from '@kit.ArkTS'; 
import { fileUri } from '@kit.CoreFileKit'; 
@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 
  ]; 
  baseDirStr: string = "/soon" 
  imageDirStr: string = "/images" 
  getBaseFileDir() { 
    let path = getContext().filesDir + this.baseDirStr 
    if (fs.accessSync(path) == false) { 
      fs.mkdirSync(path) 
    } 
    return path 
  } 
  getImageFileDir() { 
    let path = this.getBaseFileDir() + this.imageDirStr 
    if (fs.accessSync(path) == false) { 
      fs.mkdirSync(path) 
    } 
    return path 
  } 
  saveImage(buffer: ArrayBuffer,type:string , name?: string): string | undefined { 
    if (!name) { 
      name = util.generateRandomUUID(false) 
    } 
    let path = this.getImageFileDir() + "/" + name 
    return this.saveFile(buffer , type , path) 
  } 
  saveFile(buffer: ArrayBuffer | string, type:string ,uri: string): string | undefined { 
    let file: fs.File | undefined = undefined 
    try { 
      let path = uri + "." + type 
      file = fs.openSync(path, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE) 
      fs.writeSync(file.fd, buffer) 
      fs.closeSync(file) 
      return path 
    } catch (e) { 
      try { 
        if (file) fs.close(file) 
        console.log("whiteImage error" + e) 
      } catch (e) { 
      } 
      return undefined 
    } 
  } 
  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 { 
            // Configure to launch the rear camera 
            let pickerProfile: cameraPicker.PickerProfile = { cameraPosition: this.cameraPosition[1] }; 
            // Configure to photo mode 
            let pickerResult: cameraPicker.PickerResult = await cameraPicker.pick(getContext(this), 
              [this.mediaType[0]], pickerProfile); 
            // Get URI 
            let uri = pickerResult.resultUri 
            let type = uri.split(".").pop() 
            let fd = fs.openSync(uri , fs.OpenMode.READ_ONLY).fd 
            const imagePackerApi: image.ImagePacker = image.createImagePacker(); 
            const imageSourceApi: image.ImageSource = image.createImageSource(fd); 
            let packOpts: image.PackingOption = { format: "image/jpeg", quality: 98 }; 
            imagePackerApi.packing(imageSourceApi, packOpts, (err: BusinessError, data: ArrayBuffer) => { 
              if (err) { 
                console.error('packing failed.'); 
              } else { 
                let res = this.saveImage(data,type??"jpg") 
                this.uri = fileUri.getUriFromPath(res); 
              } 
            }) 
            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) 
  } 
}
分享
微博
QQ
微信
回复
4天前
相关问题
如何获取拍照图片地址
1860浏览 • 1回复 待解决
js相机组件拍照自动保存
4066浏览 • 1回复 待解决
HarmonyOS如何把图片保存到手机相册
371浏览 • 1回复 待解决
怎么下载网络上PDF保存到本地?
3638浏览 • 1回复 待解决
如何将Pixmap保存到本地文件?
348浏览 • 1回复 待解决
HarmonyOS APP无法打开相机拍照
297浏览 • 1回复 待解决
把应用沙箱下图片保存到图库
987浏览 • 1回复 待解决
HarmonyOS PhotoViewPicker 保存到图库
398浏览 • 1回复 待解决
获取网络图片保存到相册
1234浏览 • 1回复 待解决
如何将像素点保存到图片文件
2202浏览 • 1回复 待解决