请问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
书中自有颜如玉V
4

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

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

老铁666

分享
微博
QQ
微信
回复
2021-10-15 13:22:23
相关问题
HarmonyOS Image 加载网络图片问题
493浏览 • 1回复 待解决
Image组件如何加载网络图片
2450浏览 • 1回复 待解决
关于Image组件加载网络图片问题
391浏览 • 1回复 待解决
HarmonyOS Image组件网络图片不显示
235浏览 • 1回复 待解决
HarmonyOS Image组件无法显示网络图片
705浏览 • 1回复 待解决
服务卡片image怎么获取网络图片
6839浏览 • 2回复 待解决
HarmonyOS Image Kit 网络图加载
62浏览 • 1回复 待解决
HarmonyOS 请求网络图片
300浏览 • 1回复 待解决
如何获取网络图片尺寸?
333浏览 • 1回复 待解决