HarmonyOS fs open 向createasset创建的图片uri 写入数据流/数据块 失败,只能使用完整buffer写入成功

https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/js-apis-photoaccesshelper-0000001815247558-V5#ZH-CN_TOPIC_0000001881258013__createasset-2

代码:

let file = fs.openSync(photoUri, fs.OpenMode.WRITE_ONLY | fs.OpenMode.APPEND | fs.OpenMode.CREATE); 
let downLoadRequest = DTRequest.create(getContext(this)); 
let downLoadSource: DownloadSource = new DownloadSource(httpUrl); 
downLoadRequest.requestSrc(downLoadSource); 
downLoadRequest.on(RequestEvent.EVENT_PROGRESS_UPDATE, async (requestProgress: RequestProgress)=>{ 
  if (requestProgress instanceof DownloadProgress) { 
    let buffer = requestProgress.body; // 下载的数据块 
    // 按流写入,失败 
    /*await stream.write(buffer).then((number: number) => { 
      console.info(TAG, "write succeed and size is:" + number); 
    }).catch((err: BusinessError) => { 
      console.error(TAG, "write failed with error message: " + err.message + ", error code: " + err.code); 
    });*/ 
    // 按数据块写入,也失败 
    /*if (buffer && buffer.byteLength) { 
      console.log(TAG,"陆续写入:", buffer.byteLength) 
      try { 
        fs.writeSync(file.fd, buffer); 
      } catch (err) { 
        console.log(TAG, "写入错误", err); 
      } 
    }*/ 
  } 
});
HarmonyOS
2024-08-22 20:08:50
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
superinsect

从网络下载图片写入到相册可参考:

//Step1:网络请求下载图片 
let OutData: http.HttpResponse 
let imagePackerApi = image.createImagePacker(); 
let packOpts : image.PackingOption = { format:"image/jpeg", quality:98 }; 
//确保网络正常 
http.createHttp().request(url, 
  { 
    method:http.RequestMethod.GET, 
    connectTimeout:60000, 
    readTimeout:60000 
  }, 
  //Step2:将网络图片转换成pixelmap 
async (error: BusinessError, data: http.HttpResponse) => { 
  if (error) { 
    console.error(`http reqeust failed with. Code: ${error.code}, message: ${error.message}`); 
  } else { 
    OutData = data 
    let code: http.ResponseCode | number = OutData.responseCode 
    if (ResponseCode.ResponseCode.OK === code) { 
      let imageData: ArrayBuffer = OutData.result as ArrayBuffer; 
      let imageSource: image.ImageSource = image.createImageSource(imageData); 
      class tmp { 
        height: number = 100 
        width: number = 100 
      } 
     // Step3:使用imagePackerApi.packing 获取数据流data 
      imageSource.createPixelMap(options).then((pixelMap: PixelMap) => { 
        this.downloadpixelMap = pixelMap 
        this.downloadpixelMap.getImageInfo().then((info: image.ImageInfo) => { 
          console.info('info.width = ' + info.size.width) 
        }).catch((err: BusinessError) =>{ 
          console.error("Faild " + err); 
        }) 
        imagePackerApi.packing(pixelMap, packOpts).then(async (buffer : ArrayBuffer) => { 
         // Step4:获取getPhotoAccessHelper 写入文件将图片保存到相册 
          const context = getContext(this) 
          let helper = photoAccessHelper.getPhotoAccessHelper(context) 
          let uri = await helper.createAsset(photoAccessHelper.PhotoType.IMAGE, 'png') 
          let file = await fs.open(uri, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE) 
          //let buffer = new ArrayBuffer(this.pixelMap!.getPixelBytesNumber()) 
          // await this.image!.readPixelsToBuffer(buffer) 
          // 写入文件 
          // 写入文件 注意加一个packing的过程 
          await fs.write(file.fd, buffer); 
          // 关闭文件 
          await fs.close(file.fd); 
          console.log("44444444454") 
          this.message = "保存成功"
分享
微博
QQ
微信
回复
2024-08-23 17:39:34
相关问题
HarmonyOS 如何解压数据流
325浏览 • 1回复 待解决
数据文件中写入数据方法
286浏览 • 1回复 待解决
PolarDB 行如何写入
2939浏览 • 1回复 待解决
使用http模块实现数据流请求
1012浏览 • 1回复 待解决
innodb数据和binlog谁先写入
2103浏览 • 1回复 待解决
实现文件解压缩和数据流解压缩
1253浏览 • 1回复 待解决
如何将纯文本数据写入系统剪贴板?
290浏览 • 1回复 待解决
web写入customUseragent最佳实践
145浏览 • 1回复 待解决
HarmonyOS skills 功能使用成功
280浏览 • 1回复 待解决
clog写入效率怎么监控?
3791浏览 • 1回复 待解决
KV数据库基本功能使用
978浏览 • 1回复 待解决