HarmonyOS Image加载的网络图片对应的本地存储位置在哪里,有没有办法能够获取到Image下载后的本地位置。?

Image加载的网络图片对应的本地存储位置在哪里,有没有办法能够获取到Image下载后的本地位置。

阅读 515
1 个回答

当前APP是无法感知image组件加载的图片有无缓存的,也获取不到缓存Image组件加载的网络图片会采用内置的LRU策略进行缓存,首次加载网络图片时,需要请求网络资源,非首次加载时,默认从缓存中直接读取当前是缓存在应用沙箱的cache目录,目前通过接口是无法获取到的,只能通过hdc命令查看,另外缓存的文件名也是映射后的例如:/data/app/el2/100/base/com.example.ohosapp/haps/entry/cache说明:不同的设备可能缓存的路径不一样

如果需要对图片做缓存等特殊处理的,建议应用通过http下载图片并解码,缓存处理等操作后再传入image组件显示,参考demo如下:

import http from '@ohos.net.http';
import ResponseCode from '@ohos.net.http';
import image from '@ohos.multimedia.image';
import { BusinessError } from '@ohos.base';
@Entry
@Component
struct DarkModeTest {
  @State image: PixelMap | undefined = undefined;
  httpRequest() {
    http.createHttp().request(
      "https://xxxx/xxx.jpg",
      (error: BusinessError, data: http.HttpResponse) => {
        if (error) {
          console.error(`http reqeust failed with. Code: ${error.code}, message: ${error.message}`);
        } else {
          console.error(`http reqeust success.`);
          let imageData: ArrayBuffer = data.result as ArrayBuffer;
          let imageSource: image.ImageSource = image.createImageSource(imageData);
          console.error(`http reqeust size = ${imageData.byteLength}`);
          class tmp {
            height: number = 100
            width: number = 100
          }
          let options: Record<string, number | boolean | tmp> = {
            'alphaType': 0, // 透明度
            'editable': false, // 是否可编辑
            'pixelFormat': 3, // 像素格式
            'scaleMode': 1, // 缩略值
            'size': { height: 100, width: 100 }
          } // 创建图片大小
          imageSource.createPixelMap(options).then((pixelMap: PixelMap) => {
            this.image = pixelMap
          })
        }
      }
    )
  }
  build() {
    Column(){
      Button("获取网络图片")
        .onClick(() => {
          this.httpRequest()
        })
      Image(this.image)
        .height(100).width(100)

    }.backgroundColor('#CCCCCC').width('100%').height('100%')
  }
}
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进