HarmonyOS 鸿蒙Next https开头的网络图片以及地址加载不出来

HarmonyOS 鸿蒙Next https开头的网络图片以及地址加载不出来 需求场景:加载https://开头的网络地址

当前困难影响:无法加载https://开头的地址

2 回复

经过确认:目前Image就是会受限制,目前的方案是需要咱们手动用http去下载图片,再用Image去加载即可,具体可参考如下代码:

import rcp from '@hms.collaboration.rcp';
import { common } from '@kit.AbilityKit';
import { JSON } from '@kit.ArkTS';
import fileuri from '[@ohos](/user/ohos).file.fileuri'
import fileIo from '[@ohos](/user/ohos).file.fs';

@Entry
@Component
struct Index {
  downloadUrl: string = 'https://bxynj.linkdood.cn:10443/group1/M00/00/0E/rBAI2WTAyDeEV6HkAAAAAOmIa6I444.jpg';
  context = this.getContext(this) as common.UIAbilityContext
  @State filePath: string = `${this.context.filesDir}/test3.png`
  @State newPath: string = ''

  build() {
    Column() {
      Row() {
        Button('下载文件')
          .onClick(() => {
            //下载文件数据
            this.test(this.downloadUrl)
            console.log('---filePath', this.filePath)
            console.log('---newPath', this.newPath)
            setTimeout(() =>{},1000)
          })
        Image(this.newPath)
          .width(200)
          .onComplete((msg) => {
            console.log('---图片加载成功', JSON.stringify(msg))
          })
          .onError((msg) => {
            console.log('---图片加载失败', JSON.stringify(msg))
          })
      }
      .margin({ top: 5, bottom: 5 })
    }
    .height('100%')
    .width('100%')
  }

  async test(url: string) {
    // 下载到文件描述符
    const securityConfig: rcp.SecurityConfiguration = {
      remoteValidation: "skip",
    };
    //建立session对象
    let session = rcp.createSession({
      //指定与会话关联的HTTP请求的配置
      requestConfiguration: {
        security: securityConfig
      }
    });

    const respMemory = await session.get(url);
    const dataMemory = respMemory.toString();

    if (fileIo.accessSync(this.filePath)) {
      fileIo.unlinkSync(this.filePath);
    }

    const file = fileIo.openSync(this.filePath, fileIo.OpenMode.CREATE | fileIo.OpenMode.WRITE_ONLY);

    const resp = await session.downloadToFile(url, {
      kind: 'file',
      file: file.fd
    });

    this.newPath = fileuri.getUriFromPath(this.filePath)
    fileIo.closeSync(file.fd);
    session.close();
  }
}

或者尝试使用三方库ImageKnife:[https://ohpm.openharmony.cn/#/cn/detail/@ohos%2Fimageknife](https://ohpm.openharmony.cn/#/cn/detail/@ohos%2Fimageknife)

更多关于HarmonyOS 鸿蒙Next https开头的网络图片以及地址加载不出来的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


针对帖子标题“HarmonyOS 鸿蒙Next https开头的网络图片以及地址加载不出来”的问题,以下是专业且直接的回答:

在HarmonyOS鸿蒙系统中,如果遇到https开头的网络图片以及地址加载不出来的情况,可能是由于以下几个原因:

  1. 网络权限设置:检查应用是否已获得访问网络的权限。在鸿蒙系统中,应用需要明确声明并获取网络访问权限才能加载网络资源。

  2. HTTPS证书验证:如果服务器的HTTPS证书存在问题(如过期、被撤销或不受信任),鸿蒙系统可能会阻止加载该资源。确保服务器证书有效且由受信任的证书颁发机构签发。

  3. 系统安全策略:鸿蒙系统可能实施了更严格的安全策略,导致某些网络请求被拦截。检查系统安全设置,确保没有阻止相关请求。

  4. 代码实现问题:检查代码中用于加载网络图片和地址的部分,确保逻辑正确且没有错误。特别是检查URL是否正确编码,以及是否正确处理了网络请求和响应。

如果以上方法均无法解决问题,可能是系统或应用层面的bug。此时,建议联系鸿蒙系统的官方客服以获取进一步的支持。官网客服地址是:https://www.itying.com/category-93-b0.html 如果问题依旧没法解决请联系官网客服。

回到顶部