请问Image控件是怎样设置网络图片的

鸿蒙开发
2021-01-17 23:00:32
浏览
2
收藏 1
回答 3
已解决
回答 3
按赞同
/
按时间
Tuer白晓明
5

1、首先要在config.json中配置网络访问权限

"reqPermissions": [{
       "name": "ohos.permission.INTERNET"
      }
]

2、建立网络连接,获取图像流对象

Image image = (Image) findComponentById(ResourceTable.Id_img_com);

String img_path = "https://dl-harmonyos.51cto.com/images/202101/d9df0603965209541c8044110a2a903128a5eb.jpg";
HttpURLConnection connection = null;
try {
    URL url = new URL(img_path);
    URLConnection urlConnection = url.openConnection();
    if (urlConnection instanceof HttpURLConnection) {
        connection = (HttpURLConnection) urlConnection;
    }
    if (connection != null) {
        connection.connect();
        //TODO 获取流对象、并将其显示在Image组件中
    }
    
} catch (MalformedURLException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
}

3、获取流对象,并将其显示在Image组件中

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(() -> {
    image.setPixelMap(pixelMap);//设置图片显示
    pixelMap.release();
});

4、完整代码如下,需要注意的是网络请求需要放到子线程中请求,不然会报android.os.NetworkOnMainThreadException错误。

package com.ming.hos.tutorials.slice;

import com.ming.hos.tutorials.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Button;
import ohos.agp.components.Image;
import ohos.agp.components.element.ShapeElement;
import ohos.media.image.ImageSource;
import ohos.media.image.PixelMap;
import ohos.media.image.common.PixelFormat;

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

public class ShowImageAbilitySlice extends AbilitySlice {
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_show_image);
        Button button = (Button) findComponentById(ResourceTable.Id_show_btn);
        //使用xml样式
        ShapeElement shapeElement = new ShapeElement(this, ResourceTable.Graphic_background_ability_show_image);
        button.setBackground(shapeElement);
        Image image = (Image) findComponentById(ResourceTable.Id_img_com);

        button.setClickedListener(l -> {
            new Thread(new Runnable(){
                @Override
                public void run() {
                    String img_path = "https://dl-harmonyos.51cto.com/images/202101/d9df0603965209541c8044110a2a903128a5eb.jpg";
                    HttpURLConnection connection = null;
                    try {
                        URL url = new URL(img_path);
                        URLConnection urlConnection = url.openConnection();
                        if (urlConnection instanceof HttpURLConnection) {
                            connection = (HttpURLConnection) urlConnection;
                        }
                        if (connection != null) {
                            connection.connect();

                            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(() -> {
                                image.setPixelMap(pixelMap);//设置图片显示
                                pixelMap.release();
                            });
                        }
                    } catch (MalformedURLException e) {
                        e.printStackTrace();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }).start();
        });

    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}

分享
微博
QQ
微信
回复
2021-01-18 09:37:00
破茧_追梦
4

谢谢,这种方法也是一种方法,要是每张图片这样处理就有点复杂了,昨天我在网上搜了一个封装的,用类似android的Glide的方法,用着也可以

分享
微博
QQ
微信
回复1
2021-01-18 10:11:42
wx6167710b6744c

老铁666

分享
微博
QQ
微信
回复
2021-10-15 13:22:23
相关问题
服务卡片image怎么获取网络图片
2374浏览 • 2回复 待解决
InputMethodAbility用法怎样
3575浏览 • 1回复 待解决
华为Mate40配置怎样
3054浏览 • 1回复 待解决
怎样让用户选择并加载图片
3562浏览 • 1回复 待解决
Java Text控件,如何设置字间距?
1500浏览 • 1回复 待解决
移动边缘计算基本想法怎样
190浏览 • 1回复 待解决
Image怎么替换svg图片
1876浏览 • 1回复 待解决
请问鸿蒙可以用什么控件开发短视频
3202浏览 • 1回复 待解决
请问下鸿蒙webview如何设置请求头?
2006浏览 • 1回复 待解决
java鸿蒙Tab框架 image图片怎么渲染
2241浏览 • 2回复 待解决