HarmonyOS原子化服务开发实战-卡片刷新图片问题记录

鸿蒙时代
发布于 2021-12-7 11:27
浏览
0收藏

在卡片开发过程中,采用文档的方法显示图片到JS卡片上一直失败,下方是相关代码:

private void UpdataFormIma(){
    HiLog.info(TAG, "更新卡片图片");
    try{
               
         //定义数据能力帮助对象
        DataAbilityHelper helper = DataAbilityHelper.creator(getContext());

        FileInputStream inputStream = null;

        inputStream = new FileInputStream(helper.openFile(uri, "r"));

        byte[] bytes = readInputStream(inputStream);

        ZSONObject result = new ZSONObject();

        FormBindingData formBindingData = new FormBindingData(result);

        if (bytes != null && bytes.length != 0) {

            String picName = new Date().getTime() + ".png";
            String picPath = "memory://" + picName;
            assert result != null;
            result.put("avatarIma", picPath);
            formBindingData.addImageData(picName, bytes);
        }
        if (this instanceof Ability) {
            for (CardDataTable formtable : getFormId()) {
                updateForm(formtable.getFormId(), formBindingData);
            }
        }
    }catch (Exception e){
        HiLog.error(TAG,"不存在");
    }
}

之后在一篇文章上看到,卡片刷新图片的方式是回到桌面才进行刷新,所以我在页面的onStop()和onTonInactive()方法上调用卡片刷新的代码,这样将图片显示到桌面卡片上。

标签
HarmonyOS原子化服务开发实战-卡片刷新图片.docx 15.5K 4次下载
收藏
回复
举报
回复
    相关推荐