调用showAssetsCreationDialog保存图片报错(HarmonyOS 鸿蒙Next)

调用showAssetsCreationDialog保存图片报错(HarmonyOS 鸿蒙Next) H5调用客户端进行保存图片到图库(所以无法使用原生保存控件)

使用photoAccessHelper.getPhotoAccessHelper(getContext()).showAssetsCreationDialog()

在实际使用中保存失败,返回-3006,沙盒中确认存在图片

photoAccessHelper.getPhotoAccessHelper(getContext()).showAssetsCreationDialog([getContext(this).cacheDir+’/test.jpg’],[{fileNameExtendsion:‘jpg’,photo:photoAccessHelper.PhotoType.IMAGE}])


更多关于调用showAssetsCreationDialog保存图片报错(HarmonyOS 鸿蒙Next)的实战教程也可以访问 https://www.itying.com/category-93-b0.html

6 回复

需要先保存到沙箱路径下,才可以保存到图库

import { common } from '@kit.AbilityKit'
import { BusinessError } from '@kit.BasicServicesKit'
import fs from '@ohos.file.fs';
import { photoAccessHelper } from '@kit.MediaLibraryKit';
import { fileUri } from '@kit.CoreFileKit';
import { buffer } from '@kit.ArkTS';

export function myRawfileCopy(context: common.UIAbilityContext) {
  context.resourceManager.getRawFileContent("background.png", (err: BusinessError, data: Uint8Array) => {
    if (err != null) {
      console.error(`open aaa.txt failed: ${err.message}`)
    } else {
      let buffer = data.buffer
      console.log('myRawfileCopy path' + context.filesDir)
      let file = fs.openSync(context.filesDir + '/test.png', fs.OpenMode.CREATE | fs.OpenMode.READ_WRITE)
      try {
        fs.writeSync(file.fd, buffer) // 拷贝文件到沙箱,为了简便,这里是直接getrawfilecontent然后写入,当文件过大时内存压力会很大,如需优化,可通过buffer进行读取
        fs.close(file.fd)
      } catch (err) {
        console.log('myRawfileCopy error')
      }

    }
  })
}

let context = getContext(this);
let phAccessHelper = photoAccessHelper.getPhotoAccessHelper(context);

async function example(context: common.UIAbilityContext) {
  console.info('ShowAssetsCreationDialogDemo.');

  try {
    // 获取需要保存到媒体库的位于应用沙箱的图片/视频uri
    let srcFileUris: Array<string> = [
      fileUri.getUriFromPath(context.filesDir+ '/test.png')
    ];
    let photoCreationConfigs: Array<photoAccessHelper.PhotoCreationConfig> = [
      {
        title: 'test2', // 可选
        fileNameExtension: 'png',
        photoType: photoAccessHelper.PhotoType.IMAGE,
        subtype: photoAccessHelper.PhotoSubtype.DEFAULT, // 可选
      }
    ];
    let desFileUris: Array<string> = await phAccessHelper.showAssetsCreationDialog(srcFileUris, photoCreationConfigs);
    let file1 = fs.openSync(context.filesDir+ '/test.png', fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE);
    let arrayBuffer = new ArrayBuffer(4096000);
    let readLen = fs.readSync(file1.fd, arrayBuffer);
    let buf = buffer.from(arrayBuffer, 0, readLen);
    console.info(`content of File: ${buf.toString()}`);
    let file2 = fs.openSync(desFileUris[0], fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE);
    let writeLen = fs.writeSync(file2.fd, arrayBuffer);
    fs.closeSync(file2);
    fs.closeSync(file1);
    console.info('showAssetsCreationDialog success, data is ' + desFileUris);
  } catch (err) {
    console.error('showAssetsCreationDialog failed, errCode is ' + err.code + ', errMsg is ' + err.message);
  }
}

@Component
@Entry
struct Index {
  build() {
    Column() {
      Button('拷贝文件到沙箱').onClick(() => {
        myRawfileCopy(getContext(this) as common.UIAbilityContext)
      })
      Button('保存到').onClick(() => {
        example(getContext(this) as common.UIAbilityContext)
      })
    }
  }
}

更多关于调用showAssetsCreationDialog保存图片报错(HarmonyOS 鸿蒙Next)的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


人家都说了确认沙箱里有图片. 就那么几个字你都看不全?

我已经解决了 這裡的報錯是因為 ([./: * ?"’`<>&|{}[]]) title不允許有這些字符

请问,解决了吗,我也遇到这样的问题

项目名称

  • 项目状态:进行中
  • 开始时间:2023-01-01
  • 结束时间:2023-12-31

描述

这是一个示例项目,用于演示如何将HTML内容转换为Markdown。

成员

  • 张三
  • 李四
  • 王五

在HarmonyOS鸿蒙Next中,调用showAssetsCreationDialog保存图片时出现报错,可能是由于以下几个原因导致的:

  1. 权限问题:确保应用已经获取了存储权限。在HarmonyOS中,访问外部存储需要申请ohos.permission.WRITE_USER_STORAGE权限。

  2. 路径问题:检查保存路径是否正确,确保路径存在且可写。鸿蒙系统对文件路径有严格的要求,路径错误可能导致保存失败。

  3. 文件格式问题:确保保存的图片格式是系统支持的格式,如JPEG、PNG等。不支持的格式可能导致保存失败。

  4. API使用问题showAssetsCreationDialog是用于创建资源的对话框,确保在调用时传入的参数正确,如资源类型、保存路径等。

  5. 系统版本兼容性:鸿蒙Next可能存在某些API的变动,确保使用的API与系统版本兼容。

  6. 资源冲突:如果保存的图片名称与已有资源冲突,可能会导致保存失败。确保文件名唯一。

  7. 系统限制:鸿蒙系统可能对某些操作有系统级限制,如文件大小限制、存储空间不足等。

如果以上问题都已排查,仍无法解决,可通过日志进一步分析具体错误信息。

回到顶部