HarmonyOS 拍摄的照片/data/storage/el2/distributedfiles/1722837643029.png如何拷贝到应用沙箱中/data/storage/el2/base/files/chat/picture

async demo() {
  try {
    let mContext = getContext(this) as common.Context;
    let filesDir = mContext.distributedFilesDir;
    let filePath: string = filesDir + '/' + systemDateTime.getTime() + '.png';
    console.log('the pick pickerResult is: '+filePath);
    let pickerProfile: cameraPicker.PickerProfile = {
      cameraPosition: camera.CameraPosition.CAMERA_POSITION_BACK,
      videoDuration: 5,
      saveUri: filePath
    };
    let pickerResult: cameraPicker.PickerResult = await cameraPicker.pick(mContext,
      [cameraPicker.PickerMediaType.PHOTO, cameraPicker.PickerMediaType.VIDEO], pickerProfile);
    console.log("the pick pickerResult is:" + JSON.stringify(pickerResult));
    console.log("the pick pickerResult is:" + JSON.stringify(pickerResult.resultUri));
    let uri = fileUri.getUriFromPath(pickerResult.resultUri);
    console.log("the pick pickerResult is:" + uri);
    this.checkPictureList = [{ uri: uri }];
    this.sendImage()
  } catch (error) {
    let err = error as BusinessError;
    console.error(`the pick call failed. error code: ${err.code}`+err.message);
  }
}
HarmonyOS
2天前
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
put_get

保存到entry同级的files下的chat文件夹下,文件路径写死即可,chat为自建文件夹,以保存到files为例:

import picker from '@ohos.multimedia.cameraPicker'
import camera from '@ohos.multimedia.camera';
import common from '@ohos.app.ability.common';
import { BusinessError } from '@ohos.base';
import fileuri from '@ohos.file.fileuri';
import fs from '@ohos.file.fs';
import photoAccessHelper from '@ohos.file.photoAccessHelper';

let context = getContext(this) as common.Context;

class CameraPosition {
  cameraPosition: camera.CameraPosition
  saveUri: string
  constructor(cameraPosition: camera.CameraPosition, saveUri: string) {
    this.cameraPosition = cameraPosition
    this.saveUri = saveUri
  }
}

let pathDir = getContext().filesDir;
console.log('保存路径为' + pathDir)
let filePath = '/data/storage/el2/base/files/picture1.jpg'
fs.createRandomAccessFileSync(filePath, fs.OpenMode.CREATE);
let uri = fileuri.getUriFromPath(filePath);

async function photo() {
  try {
    let pickerProfile = new CameraPosition(camera.CameraPosition.CAMERA_POSITION_BACK, uri)
    let pickerResult: picker.PickerResult = await picker.pick(context,
      [picker.PickerMediaType.PHOTO, picker.PickerMediaType.VIDEO], pickerProfile);
    console.log("the pick pickerResult is:" + JSON.stringify(pickerResult));
  } catch (error) {
    let err = error as BusinessError;
    console.error(`the pick call failed. error code: ${err.code}`);
  }
}

async function picture() {
  let PhotoSelectOptions = new photoAccessHelper.PhotoSelectOptions();
  PhotoSelectOptions.MIMEType = photoAccessHelper.PhotoViewMIMETypes.IMAGE_TYPE;
  PhotoSelectOptions.maxSelectNumber = 1;
  let photoPicker = new photoAccessHelper.PhotoViewPicker();
  photoPicker.select(PhotoSelectOptions).then((PhotoSelectResult: photoAccessHelper.PhotoSelectResult) => {
    let photouri: Array<string> = PhotoSelectResult.photoUris
    let file = fs.openSync(photouri[0], fs.OpenMode.READ_ONLY)
    let file2 = fs.openSync(pathDir + '/picture2.jpg', fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE)
    fs.copyFileSync(file.fd, file2.fd)
    fs.closeSync(file);
    fs.closeSync(file2);
  })
}

@Entry
@Component
export struct Index {
  build() {
    Column() {
      Button('选择并保存').onClick(() => {
        picture()
      })
      Button('拍照并保存').onClick(() => {
        photo()
      })
    }
  }
}
分享
微博
QQ
微信
回复
2天前
相关问题
HarmonyOS Web组件访问el2目录失败
30浏览 • 1回复 待解决
el1与el2文件是否有区别
1058浏览 • 1回复 待解决
HarmonyOS rawfile文件拷贝到沙箱
219浏览 • 1回复 待解决