HarmonyOS 如何保存图片或视频到沙箱指定目录

非用户选择,拿到图片或视频后,如何保存到沙箱指定目录,请给个demo例子

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

1、获取保存图片或者视频的沙箱路径filePath

2、转换后获取uri:

let uri = fileuri.getUriFromPath(filePath);

以同步方法获取文件URI链接:

https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/js-apis-file-fileuri-V5#fileurigeturifrompath

参考示例:

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 = pathDir + '/picture.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()
      })
    }
  }
}

沙箱路径地址获取参考:

https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/application-context-stage-V5#获取应用文件路径

Button()
  .onClick(() => {
    let applicationContext = this.context.getApplicationContext();
    let cacheDir = applicationContext.cacheDir;
    let tempDir = applicationContext.tempDir;
    let filesDir = applicationContext.filesDir;
    let databaseDir = applicationContext.databaseDir;
    let bundleCodeDir = applicationContext.bundleCodeDir;
    let distributedFilesDir = applicationContext.distributedFilesDir;
    let preferencesDir = applicationContext.preferencesDir;
    // 获取应用文件路径
    let filePath = tempDir + 'test.txt';
    hilog.info(DOMAIN_NUMBER, TAG, `filePath: ${filePath}`);
    if (filePath !== null) {
      promptAction.showToast({
        message: filePath
      });
    }
  })
分享
微博
QQ
微信
回复
22h前
相关问题
HarmonyOS 下载文件并保存指定目录
96浏览 • 1回复 待解决
如何保存网络图片相册
770浏览 • 1回复 待解决
HarmonyOS 保存图片相册问题
405浏览 • 1回复 待解决
HarmonyOS 保存图片本地相册
26浏览 • 1回复 待解决
HarmonyOS 文件复制指定目录的demo
36浏览 • 1回复 待解决
HarmonyOS 截图保存图片相册
38浏览 • 1回复 待解决
HarmonyOS 实现保存图片相册
39浏览 • 1回复 待解决
求大佬告知如何保存图片相册
1176浏览 • 1回复 待解决
如何保存本地图片相册中
1133浏览 • 1回复 待解决
如何保存http网络图片本地
0浏览 • 0回复 待解决
HarmonyOS 保存图片相册权限问题
333浏览 • 1回复 待解决
资源目录下的文件沙箱的单向流动
1028浏览 • 1回复 待解决
把应用沙箱下的图片保存到图库
1191浏览 • 1回复 待解决
HarmonyOS保存图片系统相册问题咨询
687浏览 • 1回复 待解决