HarmonyOS鸿蒙Next中resourceManager.getRawFileContent的使用方法
HarmonyOS鸿蒙Next中resourceManager.getRawFileContent的使用方法
import image from '@ohos.multimedia.image';
@Entry
@Component
struct Index {
@State message: string = '加载图片'
@State pixelMap:PixelMap=null;
async createImage(){
const resourceManager= getContext(this).resourceManager
let buffer=await resourceManager.getRawFileContent("icon.png")
let imageSource=image.CreateIncrementalSource(buffer)
let decodingOptions={
editable: true,
desiredPixelFormat:3,
}
this.pixelMap= await imageSource.createPixelMap(decodingOptions)
}
build() {
Row () {
Column () {
Text(this.message)
.fontSize(50)
.fontWeight(FontWeight.Bold)
.onClick(() => {
this.createImage();
})
if(this.pixelMap!=null) {
Image(this.pixelMap).height('120').width('120')
}
// Image($rawfile("icon.png")).height('120').width('120')
}
.width('100%')
}
.height('100%')
}
}
this.pixelMap是有数据,但就是显示不了图片,从网络读取图片回来又没问题
更多关于HarmonyOS鸿蒙Next中resourceManager.getRawFileContent的使用方法的实战教程也可以访问 https://www.itying.com/category-93-b0.html
5 回复
这个问题通过在线提单进一步解决:https://developer.huawei.com/consumer/cn/support/feedback/#/,感谢您的反馈和支持
更多关于HarmonyOS鸿蒙Next中resourceManager.getRawFileContent的使用方法的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html
await resourceManager.getRawFd("icon.png")
发现用这个也可以,就getRawFileContent不行
在HarmonyOS(鸿蒙)Next中,ResourceManager.getRawFileContent
用于获取raw目录下的文件内容。首先,确保文件放置在resources/rawfile
目录下。然后,通过ResourceManager
实例调用getRawFileContent
方法,传入文件路径,返回一个Uint8Array
或ArrayBuffer
,表示文件内容。示例代码如下:
let context = getContext(this);
let resourceManager = context.resourceManager;
let rawFileContent = resourceManager.getRawFileContent('example.txt');
console.info('File content:', rawFileContent);
此方法适用于读取二进制或文本文件内容。