HarmonyOS 鸿蒙Next 在使用photoHelper.showAssetsCreationDialog保存图片时无法显示预览图的问题

HarmonyOS 鸿蒙Next 在使用photoHelper.showAssetsCreationDialog保存图片时无法显示预览图的问题 得知需要srcFileUris需要传入媒体库uri(https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V13/user-file-uri-intro-V13#媒体文件uri),但是没找到本地文件如何生成对应的媒体库uri,因为要保存的图片文件在应用私有目录里面,怎么将

类似如下的私有目录文件转为媒体库uri。

/data/storage/el2/base/haps/phone/cache/ImageKnife/xxxxx.jpg

更多关于HarmonyOS 鸿蒙Next 在使用photoHelper.showAssetsCreationDialog保存图片时无法显示预览图的问题的实战教程也可以访问 https://www.itying.com/category-93-b0.html

2 回复

沙箱文件保存到相册

import { common } from '@kit.AbilityKit';
import { fileIo as fs, fileUri, ReadOptions, WriteOptions } from '@kit.CoreFileKit'
import { photoAccessHelper } from '@kit.MediaLibraryKit';
import json from '@ohos.util.json';

@Entry
@Component
struct Index {
  @State message: string = 'Hello World';

  filePath: string = '';

  build() {
    Column() {
      Button('沙箱文件保存到相册')
        .onClick(async () => {
          let context1 = getContext(this) as common.UIAbilityContext;
          let pathDir = context1.filesDir; // 应用通用文件路径
          let filePath = pathDir + '/hello.jpg'
          let arrayBuff = context1.resourceManager.getMediaContentSync($r('app.media.hello')).buffer
          let file = fs.openSync(filePath, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE);
          fs.writeSync(file.fd, arrayBuff)
          fs.closeSync(file);
          let dstPath = pathDir + "/hello.jpg";
          this.filePath = await fileUri.getUriFromPath(dstPath) //获取沙箱完整路径
          this.syncToSysAlbum(photoAccessHelper.PhotoType.IMAGE, 'png', this.filePath)
        })
    }
    .height('100%')
    .width('100%')
  }

  async syncToSysAlbum(fileType: photoAccessHelper.PhotoType, extension: string, ...filePath: string[]) {
    let context = getContext(this);
    let phAccessHelper = photoAccessHelper.getPhotoAccessHelper(context);
    try {
      const srcFileUris: string[] = []
      filePath.forEach((path) => {
        srcFileUris.push(path)
      })
      const config: photoAccessHelper.PhotoCreationConfig[] = []
      config.push({
        title: 'background', // 可选
        fileNameExtension: extension,
        photoType: fileType,
      })
      console.log("syncToSysAlarm fileUri:" + json.stringify(srcFileUris) + ",config:" + json.stringify(config))

      const desFileUris = await phAccessHelper.showAssetsCreationDialog(srcFileUris, config)

      console.debug(`目标图片 uri is : ${JSON.stringify(desFileUris)}`)

      if (desFileUris.length > 0) {
        for (let index = 0; index < desFileUris.length; index++) {
          this.copyFileContentTo(srcFileUris[index], desFileUris[index])
        }
      }
    } catch (err) {
      console.log("syncToSysAlarm filePath:" + filePath + ",error:" + json.stringify(err))
    }
  }

  copyFileContentTo(srcFilePath: string, destFilePath: string) {
    let srcFile = fs.openSync(srcFilePath, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE)
    let destFile = fs.openSync(destFilePath, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE)

    // 读取源文件内容并写入至目的文件
    let bufSize = 4096;
    let readSize = 0;
    let buf = new ArrayBuffer(bufSize);
    let readOptions: ReadOptions = {
      offset: readSize,
      length: bufSize
    };
    let readLen = fs.readSync(srcFile.fd, buf, readOptions);
    while (readLen > 0) {
      readSize += readLen;
      let writeOptions: WriteOptions = {
        length: readLen
      };
      fs.writeSync(destFile.fd, buf, writeOptions);
      readOptions.offset = readSize;
      readLen = fs.readSync(srcFile.fd, buf, readOptions);
    }
    // 关闭文件
    fs.closeSync(srcFile);
    fs.closeSync(destFile);
  }
}

更多关于HarmonyOS 鸿蒙Next 在使用photoHelper.showAssetsCreationDialog保存图片时无法显示预览图的问题的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


针对HarmonyOS 鸿蒙Next在使用photoHelper.showAssetsCreationDialog保存图片时无法显示预览图的问题,这通常与图片的加载和显示逻辑有关。以下是一些可能的原因及解决方法,但遵循您的要求,不涉及Java或C语言内容:

  1. 图片路径问题:确保保存图片后返回的路径正确无误,且该路径下的图片文件确实存在。检查路径字符串是否有误,包括文件名和扩展名。

  2. 图片加载逻辑:预览图无法显示可能是因为图片加载逻辑有误。检查用于加载预览图的组件或方法,确保它能正确读取并显示指定路径的图片。

  3. 权限问题:确认应用已获取读写存储空间的权限。在HarmonyOS中,需要用户在运行时授予相应的权限。

  4. UI组件状态:检查用于显示预览图的UI组件(如ImageView)是否已正确设置且处于可见状态。

  5. 系统或框架bug:如果以上均无误,可能是系统或框架本身的bug。尝试更新鸿蒙系统到最新版本,或查看鸿蒙开发者社区是否有相关问题的反馈和解决方案。

如果问题依旧没法解决请联系官网客服,官网地址是:https://www.itying.com/category-93-b0.html

回到顶部