HarmonyOS 如何将media文件夹中的png图片转为base64字符串?

请问如何将media文件夹中的png图片转为base64字符串?目前我们需要上传一个图片的base64格式,这个图片目前放到了resources-base-media下,但是不知道如何转为base64,拿不到图片地址,我也尝试新建一个文件夹rawfile,然后

let resourceDir = this.context.resourceDir
let resourceDirTest = resourceDir + '/jk_face_photo.png';
  • 1.
  • 2.

但是得到的地址转为base64过程中卡死了。

HarmonyOS
2024-12-23 16:29:26
3003浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
aquaa

可以参考下getMediaContentBase64这个api进行转换,请参考:https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/js-apis-resource-manager-V5#getmediacontentbase649

demo:

import { BusinessError } from '@ohos.base';
import { common } from '@kit.AbilityKit';

//自定义组件
@Component
struct Child {
  @State private text: string = '初始值'
  private controller: ChildController = new ChildController();

  aboutToAppear() {
    if(this.controller) {
      this.controller.changeText = this.changeText
    }
  }

  private changeText = (value: string) =>{
    this.text = value
  }

  build() {
    Column() {
      Text(this.text)
    }
  }
}

class ChildController {
  changeText = (value: string) => {
  }
}

export let ChildRef = new ChildController()

@Entry
@Component
struct Parent {
  // ChildRef = new ChildController()
  @State noShow: boolean = false
  private context = getContext(this) as common.UIAbilityContext;

  aboutToAppear(): void {

    try {
      this.context.resourceManager.getMediaContentBase64($r('app.media.startIcon').id, (error: BusinessError, value: string) => {
        if (error != null) {
          console.error("error is " + error);
        } else {
          let media = value;
          //base64编码的值
          console.info(media)
        }
      });
    } catch (error) {
      let code = (error as BusinessError).code;
      let message = (error as BusinessError).message;
      console.error(`callback getMediaContentBase64 failed, error code: ${code}, message: ${message}.`);
    }
  }

  build() {
    Column() {
      Text('获取Child的exposeMethods!').fontSize('18vp').fontColor(Color.Gray)
      Divider()
      Child({ controller: ChildRef })
      Child()
      Button('Parent调用childe的changeText').onClick(() => {
        ChildRef.changeText('Parent调用childer的changeText')
      })

    }
  }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
分享
微博
QQ
微信
回复
2024-12-23 19:21:23
相关问题
HarmonyOS 图片转为base64字符
879浏览 • 1回复 待解决
如何将PixelMap转图片base64字符
1557浏览 • 1回复 待解决
如何将图片base64字符转PixelMap?
1618浏览 • 1回复 待解决
base64字符如何转为图片并保存
3663浏览 • 1回复 待解决
base64字符保存为图片方法
2077浏览 • 1回复 待解决
PixelMap类型怎么转换成Base64字符
1596浏览 • 1回复 待解决
HarmonyOS 如何将文件转为base64
1190浏览 • 1回复 待解决
HarmonyOS SM2加签返回Base64字符
961浏览 • 1回复 待解决
HarmonyOS如何将图片Base64
1984浏览 • 1回复 待解决
如何将JSON字符串转为Map
1019浏览 • 1回复 待解决
如何将字符串转为Uint8Array?
1866浏览 • 1回复 待解决