在TV的HarmonyOS鸿蒙Next系统上Image实现显示网络图片
在TV的HarmonyOS鸿蒙Next系统上Image实现显示网络图片 因为是新平台,缺少一些好用的轮子,所以只能自己手动实现加载网络图片。话不多说上代码
1:先添加网络申请权限,
ohos.permission.INTERNET
"reqPermissions": [
{
"name": "ohos.permission.INTERNET",
"reason": "",
"usedScene": {
"ability": [
"com.pengtu.myapplication.MainAbility"
],
"when": "always"
}
}
],
2:加载OKhttp框架,万幸这是一个纯java框架,没有用Android的api
3:简单封装,非常随意
import io.reactivex.annotations.Nullable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.agp.components.Image;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;
import ohos.media.image.ImageSource;
import ohos.media.image.PixelMap;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import java.io.IOException;
import java.io.InputStream;
public class HmOSImageLoader {
private final static HiLogLabel LABEL_LOG = new HiLogLabel(HiLog.LOG_APP, 0, "HmOSImageLoader");
Image image;
String url;
int defImage;
AbilitySlice abilitySlice;
private HmOSImageLoader(AbilitySlice abilitySlice) {
this.abilitySlice = abilitySlice;
}
public static HmOSImageLoader with(AbilitySlice abilitySlice) {
return new HmOSImageLoader(abilitySlice);
}
public HmOSImageLoader load(@Nullable String url) {
this.url = url;
return this;
}
public HmOSImageLoader def(@Nullable int defImage) {
this.defImage = defImage;
return this;
}
public void into(@Nullable Image image) {
this.image = image;
start();
}
private void start() {
if (defImage != 0)
image.setPixelMap(defImage);
Request request = new Request.Builder().url(url).get().build();
new Thread(() -> {
OkHttpClient okHttpClient = new OkHttpClient();
try {
//异步网络请求
Response execute = okHttpClient.newCall(request).execute();
//获取流
InputStream inputStream = execute.body().byteStream();
//利用鸿蒙api将流解码为图片源
ImageSource imageSource = ImageSource.create(inputStream, null);
//根据图片源创建位图
PixelMap pixelMap = imageSource.createPixelmap(null);
//展示到组件上
image.setPixelMap(pixelMap);
//释放位图
pixelMap.release();
} catch (IOException e) {
HiLog.error(LABEL_LOG, " ----- " + e.getMessage());
e.printStackTrace();
}
}).start();
}
}
4:使用方式
HmOSImageLoader.with(this).load(urlString).def(ResourceTable.Media_def).into(image);
更多关于在TV的HarmonyOS鸿蒙Next系统上Image实现显示网络图片的实战教程也可以访问 https://www.itying.com/category-93-b0.html
你确定你这个能加载到网络图片并显示出来?
更多关于在TV的HarmonyOS鸿蒙Next系统上Image实现显示网络图片的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html
在HarmonyOS鸿蒙Next系统上,TV设备可以通过Image
组件显示网络图片。Image
组件支持从网络加载图片,开发者只需指定图片的URL即可。以下是一个简单的示例代码:
import { Image } from '@ohos.multimedia.image';
import { Http } from '@ohos.net.http';
@Entry
@Component
struct NetworkImageExample {
@State imageSrc: string = '';
build() {
Column() {
Image(this.imageSrc)
.width(200)
.height(200)
.onClick(() => {
this.loadImage();
})
}
}
loadImage() {
let http = new Http();
http.request('https://example.com/path/to/image.jpg', (err, data) => {
if (!err) {
this.imageSrc = data;
}
});
}
}
在这个示例中,Image
组件通过imageSrc
属性绑定图片的URL。当用户点击图片时,loadImage
方法会通过HTTP请求加载网络图片,并将图片URL赋值给imageSrc
,从而在Image
组件中显示。
在HarmonyOS Next系统上,使用Image
组件显示网络图片可以通过Image
的src
属性设置图片的URL。首先,确保应用已获取网络权限。然后,使用Image
组件并设置src
为网络图片的URL即可。例如:
<Image
ohos:id="$+id:network_image"
ohos:width="200vp"
ohos:height="200vp"
ohos:src="https://example.com/image.jpg"
/>
确保URL有效且网络连接正常,图片将自动加载并显示。