HarmonyOS鸿蒙Next中使用rcp下载图片到fileDir后Image怎么显示图片

HarmonyOS鸿蒙Next中使用rcp下载图片到fileDir后Image怎么显示图片 我的下载地址是https://106.55.11.188:8682/ccr/meeting/files/20250424/鸿哲黑白二维码_1745488987559.jpg

使用rcp下载了文件

import { rcp } from '@kit.RemoteCommunicationKit';
import { BusinessError } from '@kit.BasicServicesKit';
import { Environment } from '@kit.CoreFileKit';

export function downloadFile(url:string){
  const session = rcp.createSession({
    requestConfiguration: {
      proxy: 'system',
      security: {
        remoteValidation: 'skip'
      }
    }
  })

  const DOWNLOAD_TO_PATH = getContext().filesDir
  console.info(DOWNLOAD_TO_PATH)
  let downloadToFile: rcp.DownloadToFile = {
    kind: 'folder',
    path: DOWNLOAD_TO_PATH
  }
  return new Promise<(value:string) => void,reject:(value?:string) => void>((resolve,reject)=>{
    session.downloadToFile(url, downloadToFile)
      .then((response: rcp.Response) => {
        console.info(`Succeeded in getting the response ${response}`)
        resolve('download success')
      }).catch((err: BusinessError) => {
      console.error(`Failed, the error message is ${JSON.stringify(err)}`)
      reject('download fail')
    })
  })

}

文件下载成功了,看到存储到设备的地址是data/app/el2/100/base/com.example.meetingreservation/haps/default/files/106.55.11.188/crr/meeting/files/20250424/文件名(不知道是文件名称解码错误导致的乱码还是什么问题)

图片的保存和命名是不是有误,怎么使用Image来显示这张图片


更多关于HarmonyOS鸿蒙Next中使用rcp下载图片到fileDir后Image怎么显示图片的实战教程也可以访问 https://www.itying.com/category-93-b0.html

1 回复

更多关于HarmonyOS鸿蒙Next中使用rcp下载图片到fileDir后Image怎么显示图片的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


在HarmonyOS鸿蒙Next中,使用RCP下载图片到fileDir后,可以通过Image组件显示图片。首先,确保图片已成功下载到指定目录。然后,使用Image组件的src属性,将路径设置为下载的图片文件路径。例如:

Image image = new Image(context);
image.setPixelMap("file://" + fileDir + "/image.jpg");

确保路径正确,且文件存在。这样,Image组件就会显示下载的图片。

回到顶部