HarmonyOS原子化服务开发实战-Js更新显示本地图片 原创

鸿蒙时代
发布于 2021-11-25 11:18
浏览
0收藏

使用js显示本地卡片一般步骤:

1、获取本地图片路径

// 获取相册的图片与视频
private void getImage(){
    HiLog.info(TAG,"打开相册");
    Intent intent = new Intent();
    Operation opt=new Intent.OperationBuilder().withAction("android.intent.action.GET_CONTENT").build();
    intent.setOperation(opt);
    intent.addFlags(Intent.FLAG_NOT_OHOS_COMPONENT);
    intent.setType("image/*");
    intent.setBundle("com.huawei.photos");
    startAbilityForResult(intent, imgRequestCode);
}


// 图片选取回调
@Override
protected void onAbilityResult(int requestCode, int resultCode, Intent resultData) {
    if(requestCode==imgRequestCode) {
        HiLog.info(TAG, "选择图片getUriString:" + resultData.getUriString());
        //选择的Img对应的Uri
        String chooseImgUri = resultData.getUriString();

        //获取选择的Img对应的Id
        String chooseImgId=null;
        //如果是选择文件则getUriString结果为content://com.android.providers.media.documents/document/image%3A30,其中%3A是":"的URL编码结果,后面的数字就是image对应的Id
        //如果选择的是图库则getUriString结果为content://media/external/images/media/30,最后就是image对应的Id
        //这里需要判断是选择了文件还是图库
        if(chooseImgUri.lastIndexOf("%3A")!=-1){
            chooseImgId = chooseImgUri.substring(chooseImgUri.lastIndexOf("%3A")+3);
        }
        else {
            chooseImgId = chooseImgUri.substring(chooseImgUri.lastIndexOf('/')+1);
        }
        //获取图片对应的uri,由于获取到的前缀是content,我们替换成对应的dataability前缀
        Uri uri=Uri.appendEncodedPathToUri(AVStorage.Images.Media.EXTERNAL_DATA_ABILITY_URI,chooseImgId);
        HiLog.info(TAG,"图片uri"+uri);
        setImaData(uri,chooseImgId);
    }
}

2、将本地图片转换成byte[]数据

//定义数据能力帮助对象
DataAbilityHelper helper = DataAbilityHelper.creator(getContext());

FileInputStream inputStream = null;

try {
    inputStream = new FileInputStream(helper.openFile(uri, "r"));
} catch (DataAbilityRemoteException|FileNotFoundException e) {
    e.printStackTrace();
}
//readInputStream将inputStream转换成byte[]
byte[] bytes = readInputStream(inputStream); 
private byte[] readInputStream(InputStream inputStream) {

    ByteArrayOutputStream baos = new ByteArrayOutputStream();

    byte[] buffer = new byte[1024];

    int length = -1;

    try {
        while ((length = inputStream.read(buffer)) != -1) {
            baos.write(buffer, 0, length);
        }
        baos.flush();
    } catch (IOException e) {
        e.printStackTrace();
    }

    byte[] data = baos.toByteArray();

    try {
        inputStream.close();
        baos.close();
    } catch (IOException e) {
        e.printStackTrace();
    }

    return data;
}

3、卡片显示图片

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;

    result.put("avatarIma", picPath);
    formBindingData.addImageData(picName, bytes);

}

try {
    if (this instanceof Ability) {
        for (CardDataTable formtable : getFormId()) { //获取卡片formID
            updateForm(formtable.getFormId(), formBindingData);
        }
    }
} catch (FormException e) {
    HiLog.error(TAG, e.getMessage());
}

©著作权归作者所有,如需转载,请注明出处,否则将追究法律责任
分类
HarmonyOS原子化服务开发实战-Js更新显示本.docx 20.46K 19次下载
1
收藏
回复
举报
回复
    相关推荐