HarmonyOS 鸿蒙Next 下载gif图片报错
HarmonyOS 鸿蒙Next 下载gif图片报错
import http from '@ohos.net.http';
import { BusinessError } from ‘@ohos.base’;
import common from ‘@ohos.app.ability.common’;
import photoAccessHelper from ‘@ohos.file.photoAccessHelper’;
import fs from ‘@ohos.file.fs’;
import promptAction from ‘@ohos.promptAction’;
@Entry
@Component
export struct SaveNetWorkPictures {
  imageBuffer: ArrayBuffer | undefined = undefined; // 图片ArrayBuffer
  async aboutToAppear(): Promise<void> {
    //请求网络图片
  }
  build() {
    Column() {
      Column() {
        SaveButton({ icon: SaveIconStyle.FULL_FILLED })
          .iconSize(60)
          .iconColor("#888888")
          .width(99)
          .height(99)
          .onClick(async () => {
            // if (this.imageBuffer !== undefined) {
            // await this.getPicture();
            await this.saveImage(this.imageBuffer);
            promptAction.showToast({
              message: ‘图片保存成功’,
              duration: 2000
            })
            // }
          })
      }
      .height(‘100%’)
      .padding(100)
    }
  }
  // * 通过http的request方法从网络下载图片资源
  getPicture(): Promise<void> {
    return new Promise((resolve)=>{
      http.createHttp()// 显示网络图片的地址
          (error: BusinessError, data: http.HttpResponse) => {
            if (error) {
              // 下载失败时弹窗提示检查网络,不执行后续逻辑
              promptAction.showToast({
                message: ‘图片加载失败,请检查网络’,
                duration: 2000
              })
              return;
            }
            // this.transcodePixelMap(data);
            // 判断网络获取到的资源是否为ArrayBuffer类型
            if (data.result instanceof ArrayBuffer) {
              this.imageBuffer = data.result as ArrayBuffer;
            }
            resolve();
          }
        )
    })
  }
  // * 保存ArrayBuffer到图库
  async saveImage(buffer: ArrayBuffer | string | undefined): Promise<void> {
    if(this.imageBuffer == undefined ){
      await this.getPicture()
    }
    try {
      const context = getContext(this) as common.UIAbilityContext; // 获取getPhotoAccessHelper需要的context
      const helper = photoAccessHelper.getPhotoAccessHelper(context); // 获取相册管理模块的实例
      const uri = await helper.createAsset(photoAccessHelper.PhotoType.IMAGE, ‘gif’); // 指定待创建的文件类型、后缀和创建选项,创建图片或视频资源
      const file = await fs.open(uri, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE);
      await fs.write(file.fd, this.imageBuffer);
      await fs.close(file.fd);
    } catch (e) {
      console.log(JSON.stringify(e))
    }
  }
}
更多关于HarmonyOS 鸿蒙Next 下载gif图片报错的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html
在HarmonyOS鸿蒙Next系统中下载GIF图片报错,可能涉及多种因素,包括但不限于网络权限、文件读写权限、文件格式支持及API调用错误等。
- 
检查网络权限:确保应用已正确申请并获取网络访问权限,这是下载任何网络资源的前提。 
- 
验证文件读写权限:检查应用是否拥有对存储设备的读写权限,这对于保存下载的文件至关重要。 
- 
文件格式支持:确认HarmonyOS鸿蒙Next系统是否原生支持GIF格式图片的解析与展示。尽管GIF是常见格式,但不同系统或应用可能对格式支持存在差异。 
- 
API调用检查:审查代码中用于下载和保存GIF图片的API调用,确保遵循了HarmonyOS的API规范,且调用顺序、参数设置无误。 
- 
错误日志分析:查看系统或应用生成的错误日志,通常能提供更具体的错误信息,有助于定位问题根源。 
- 
系统兼容性:考虑是否存在因系统版本差异导致的兼容性问题,尝试在不同版本的HarmonyOS设备上重现问题。 
如果问题依旧没法解决请联系官网客服,官网地址是:https://www.itying.com/category-93-b0.html
 
        
       
                   
                   
                  

