中国优质的IT技术网站
专业IT技术创作平台
IT职业在线教育平台
微信扫码分享
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); } }
谢谢,这种方法也是一种方法,要是每张图片这样处理就有点复杂了,昨天我在网上搜了一个封装的,用类似android的Glide的方法,用着也可以
老铁666
1、首先要在config.json中配置网络访问权限
2、建立网络连接,获取图像流对象
3、获取流对象,并将其显示在Image组件中
4、完整代码如下,需要注意的是网络请求需要放到子线程中请求,不然会报android.os.NetworkOnMainThreadException错误。
谢谢,这种方法也是一种方法,要是每张图片这样处理就有点复杂了,昨天我在网上搜了一个封装的,用类似android的Glide的方法,用着也可以
老铁666