问:在HarmonyOS鸿蒙Next中如何使用rawfile文件夹下icon.jpg的ArrayBuffer去发布图片类型的通知?
问:在HarmonyOS鸿蒙Next中如何使用rawfile文件夹下icon.jpg的ArrayBuffer去发布图片类型的通知?
这儿的代码不满足条件
// 图片构造
const color = new ArrayBuffer(60000);
let bufferArr = new Uint8Array(color);
for (var i = 0; i<bufferArr.byteLength;i++) {
bufferArr[i++] = 60;
bufferArr[i++] = 20;
bufferArr[i++] = 220;
bufferArr[i] = 100;
}
let opts = { editable:true, pixelFormat:"ARGB_8888", size: {height:100, width : 150}};
await image
.createPixelMap(color, opts)
.then(async (pixelmap) => {
await pixelmap.getImageInfo().then(imageInfo => {
console.log("=====size: ====" + JSON.stringify(imageInfo.size));
}).catch(err => {
console.error("Failed to obtain the image pixel map information." + JSON.stringify(err));
return;
})
let notificationRequest = {
id: 1,
content: {
contentType: notify.ContentType.NOTIFICATION_CONTENT_PICTURE,
picture: {
title: 'test_title',
text: 'test_text',
additionalText: 'test_additionalText',
picture: pixelmap,
briefText: 'test_briefText',
expandedTitle: 'test_expandedTitle',
}
},
}
// 发送通知
NotificationManager.publish(notificationRequest, (err) => {
if (err) {
console.error(`[ANS] failed to publish, error[${err}]`);
return;
}
console.info(`[ANS] publish success `);
});
}).catch(err=> {
console.error('create pixelmap failed ==========' + JSON.stringify(err));
return;
})
这是我的代码:
// 图片构造
let context = getContext(this) as common.UIAbilityContext;
// 获取resourceManager资源管理
//获取rawfile文件夹下icon.png的ArrayBuffer。
let resourceMgr = context.resourceManager;
let fileData = await resourceMgr.getRawFileContent('icon.png');
// 获取图片的ArrayBuffer
let buffer = fileData.buffer;
// let imageSource = image.createImageSource(buffer);
let imageSource = await image
.createImageSource(buffer)
let pixelMap = await imageSource.createPixelMap();
let notificationRequest = {
id: 1,
content: {
contentType: notificationManager.ContentType.NOTIFICATION_CONTENT_PICTURE,
picture: {
title: 'test_title',
text: 'test_text',
additionalText: 'test_additionalText',
picture: pixelMap,
briefText: 'test_briefText',
expandedTitle: 'test_expandedTitle',
}
},
}
// 发送通知
notificationManager.publish(notificationRequest, (err) => {
if (err) {
console.error(`[ANS] failed to publish, error[${err}]`);
return;
}
console.info(`[ANS] publish success `);
});
效果是只有文本,没有图片,请问如何修改?
更多关于问:在HarmonyOS鸿蒙Next中如何使用rawfile文件夹下icon.jpg的ArrayBuffer去发布图片类型的通知?的实战教程也可以访问 https://www.itying.com/category-93-b0.html
5 回复
楼主您好,参考方法三:通过资源管理器获取资源文件的ArrayBuffer。
更多关于问:在HarmonyOS鸿蒙Next中如何使用rawfile文件夹下icon.jpg的ArrayBuffer去发布图片类型的通知?的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html
我用的就是3,但是获取不到,
消息展开有吗?
没有,不知道咋修改代码。
在HarmonyOS鸿蒙Next中,你可以通过以下步骤使用rawfile
文件夹下的icon.jpg
的ArrayBuffer
发布图片类型的通知:
- 读取文件:使用
ResourceManager
读取rawfile/icon.jpg
文件,获取ArrayBuffer
。 - 创建通知:使用
NotificationRequest
创建通知,并设置NotificationPictureContent
为通知内容。 - 设置图片:将
ArrayBuffer
转换为PixelMap
,并将其设置为通知的图片内容。 - 发布通知:使用
NotificationHelper
发布通知。
示例代码:
import notification from '@ohos.notification';
import image from '@ohos.multimedia.image';
// 读取rawfile下的icon.jpg
let resourceManager = getContext().resourceManager;
let arrayBuffer = await resourceManager.getRawFileContent('icon.jpg');
// 将ArrayBuffer转换为PixelMap
let imageSource = image.createImageSource(arrayBuffer.buffer);
let pixelMap = await imageSource.createPixelMap();
// 创建图片通知
let pictureContent = {
picture: pixelMap
};
let notificationRequest = {
content: {
contentType: notification.ContentType.NOTIFICATION_CONTENT_PICTURE,
normal: pictureContent
}
};
// 发布通知
notification.publish(notificationRequest);