HarmonyOS 鸿蒙Next 使用SaveButtom组件时提示资源Id不存在

HarmonyOS 鸿蒙Next 使用SaveButtom组件时提示资源Id不存在

@State saveButtonOptions: SaveButtonOptions = { icon: SaveIconStyle.FULL_FILLED, text: SaveDescription.SAVE_IMAGE, buttonType: ButtonType.Capsule }

SaveButton(this.saveButtonOptions) .onClick(async (event, result: SaveButtonOnClickResult) => { if (result == SaveButtonOnClickResult.SUCCESS) { try { console.log('createAsset successfully, event: ’ + event); let context = getContext(); let phAccessHelper = photoAccessHelper.getPhotoAccessHelper(context); // Creating a Media File let uri = await phAccessHelper.createAsset(photoAccessHelper.PhotoType.IMAGE, ‘jpg’); console.log('createAsset successfully, uri: ’ + uri); // Open the created media file and read the local file and convert it to ArrayBuffer for easy filling. let file = await fileIo.open(uri, fileIo.OpenMode.READ_WRITE); let buffer = getContext(this).resourceManager.getMediaContentSync($r(‘app.media.img’).id); // Write the read ArrayBuffer to the new media file. let writeLen = await fileIo.write(file.fd, buffer.buffer); console.log(‘write success,len=’ + writeLen); await fileIo.close(file); } catch (err) { console.log('createAsset failed, message = ', err); } } else { console.log(‘SaveButtonOnClickResult createAsset failed’); } })


更多关于HarmonyOS 鸿蒙Next 使用SaveButtom组件时提示资源Id不存在的实战教程也可以访问 https://www.itying.com/category-93-b0.html

5 回复

检查下资源是不是正常的,我试了下去读取资源ID没问题,可以正常写入相册,下面是试的代码:

import { photoAccessHelper } from '@kit.MediaLibraryKit';
import { fileIo } from '@kit.CoreFileKit';

class Item { img: ResourceStr = '';title?: string = '' }
@Entry
@Component struct Index {
  @State saveButtonOptions: SaveButtonOptions = {
    icon: SaveIconStyle.FULL_FILLED,
    text: SaveDescription.SAVE_IMAGE,
    buttonType: ButtonType.Capsule
  }

  build() {
    Column() {
      SaveButton(this.saveButtonOptions)
        .onClick(async (event, result: SaveButtonOnClickResult) => {
          if (result == SaveButtonOnClickResult.SUCCESS) {
            try {
              console.log('createAsset successfully, event: ' + event);
              let context = getContext();
              let phAccessHelper = photoAccessHelper.getPhotoAccessHelper(context);
              // Creating a Media File
              let uri = await phAccessHelper.createAsset(photoAccessHelper.PhotoType.IMAGE, 'jpg');
              console.log('createAsset successfully, uri: ' + uri);
              // Open the created media file and read the local file and convert it to ArrayBuffer for easy filling.
              let file = await fileIo.open(uri, fileIo.OpenMode.READ_WRITE);
              let buffer = getContext(this).resourceManager.getMediaContentSync($r('app.media.startIcon').id);
              // Write the read ArrayBuffer to the new media file.
              let writeLen = await fileIo.write(file.fd, buffer.buffer);
              console.log('createAsset write success,len=' + writeLen);
              await fileIo.close(file);
            } catch (err) {
              console.log('createAsset failed, message = ', err);
            }
          } else {
            console.log('createAsset SaveButtonOnClickResult createAsset failed');
          }
        })
    }
  }
}

结果:

cke_7347.png

更多关于HarmonyOS 鸿蒙Next 使用SaveButtom组件时提示资源Id不存在的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


我两个项目,在一个项目中能跑,另一个跑不了。。。

元服务的不能跑,app的可以,
元服务的不能跑,app的可以,

| 名称 | 值 |
| --- | --- |
| 项目名称 | Demo |
| 项目版本 | v0.1.0 |
| 作者 | John Doe |
| 邮箱 | john.doe@example.com |
| 描述 | 这是一个示例项目。 |

在HarmonyOS鸿蒙Next系统中使用SaveButton组件时遇到“资源Id不存在”的提示,通常意味着在你的项目资源文件中缺少相应的ID定义,或者代码中引用的ID与实际定义不匹配。

解决步骤如下:

  1. 检查资源文件:确保你的资源文件中(如XML布局文件)已经正确定义了SaveButton组件,并且为其分配了一个唯一的ID。ID通常以+id/开头,后面跟随自定义的名称。

  2. 核对ID引用:在Java或鸿蒙特有的ArkUI(如果使用的是JS/TS框架)代码中,检查你是如何引用这个ID的。确保引用的ID与资源文件中定义的ID完全一致,包括大小写。

  3. 清理和重建项目:有时候IDE的缓存可能会导致此类问题。尝试清理并重建你的项目,看看问题是否得到解决。

  4. 检查组件导入:确保你已经正确导入了SaveButton组件所需的包或模块。在某些情况下,如果组件属于特定库或模块,未导入会导致资源ID无法识别。

如果以上步骤都无法解决问题,可能是IDE或鸿蒙SDK本身的bug,或者是项目配置有误。此时,你可以尝试更新IDE和鸿蒙SDK到最新版本,或者检查项目配置文件(如build.gradlepackage.json等)是否正确设置。

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

回到顶部