文档管理中心
我的
|

DevEco Studio

加载网络图片功用于界面显示

加载网络图片功用于界面显示

参考资料

权限开发指导

线程管理

图像开发概述

代码实现

config.json配置

config.json代码如下

    "reqPermissions": [
     
      {"name": "ohos.permission.INTERNET"}
    ],复制
xml代码实现
<?xml version="1.0" encoding="utf-8"?>
<DirectionalLayout
    xmlns:ohos="http://schemas.huawei.com/res/ohos"
    ohos:height="match_parent"
    ohos:width="match_parent"
    ohos:orientation="vertical">
    <Text
        ohos:height="100vp"
        ohos:width="match_parent"
        ohos:id="$+id:LoadImage"
        ohos:text_size="40vp"
        ohos:background_element="#ed6262"
        ohos:text_alignment="center"
        ohos:text="加载图片"
        />
    <Image
        ohos:id="$+id:myImage"
        ohos:height="match_parent"
        ohos:width="match_parent"
        ohos:image_src="$media:icon"/>

</DirectionalLayout>复制

java代码实现

package com.harmony.alliance.mydemo.slice;

import com.harmony.alliance.mydemo.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentProvider;
import ohos.agp.components.Image;
import ohos.media.image.ImageSource;
import ohos.media.image.PixelMap;
import ohos.media.image.common.PixelFormat;

import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;

public class myImageAbilitySlice  extends AbilitySlice {
    private Image myImage;

    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        setUIContent(ResourceTable.Layout_my_image);
        myImage=findComponentById(ResourceTable.Id_myImage);
        findComponentById(ResourceTable.Id_LoadImage).setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                new Thread(){
                    @Override
                    public void run() {
                        super.run();
                        LoadImageData();
                    }
                }.start();
            }
        });

    }
    public  void  LoadImageData(){
        String   urlImage = "https://www.harmonyos.com/resource/image/community/20201009-164134eSpace.jpg";
        HttpURLConnection connection = null;
        try {
            URL url = new URL(urlImage);
            URLConnection urlConnection =   url.openConnection();
            if (urlConnection instanceof   HttpURLConnection) {
                connection =   (HttpURLConnection) urlConnection;
            }
            if (connection != null) {
                connection.connect();
                // 之后可进行url的其他操作
                // 得到服务器返回过来的流对象
                InputStream inputStream =   urlConnection.getInputStream();
                ImageSource imageSource = ImageSource.create(inputStream,   new ImageSource.SourceOptions());
                ImageSource.DecodingOptions   decodingOptions = new ImageSource.DecodingOptions();
                decodingOptions.desiredPixelFormat   = PixelFormat.ARGB_8888;
                // 普通解码叠加旋转、缩放、裁剪
                PixelMap pixelMap = imageSource.createPixelmap(decodingOptions);
                // 普通解码
                getUITaskDispatcher().syncDispatch(()   -> {
                    myImage.setPixelMap(pixelMap);
                    pixelMap.release();
                });
            }
        }   catch (Exception e) {
            e.printStackTrace();
        }
    }


}
复制

运行效果

点赞
收藏
回复
分享
举报
浏览109 发布于2024-07-20 10:01安徽
全部评论
最多点赞
最新发布
最早发布
写回答
新增插入模板功能
一键使用模板,快速填写内容,轻松发帖~
知道了
  • 为了保障您的信息安全,请勿上传您的敏感个人信息(如您的密码等信息)和您的敏感资产信息(如关键源代码、签名私钥、调试安装包、业务日志等信息),且您需自行承担由此产生的信息泄露等安全风险。
  • 如您发布的内容为转载内容,请注明内容来源。

我要发帖子

了解社区公约,与您携手共创和谐专业的开发者社区。