1***@qq.com
1***@qq.com
  • 发布:2024-12-19 11:34
  • 更新:2024-12-19 11:34
  • 阅读:27

【报Bug】uts调用鸿蒙API读取文件ArrayBuffer APP闪退

分类:鸿蒙Next

产品分类: uniapp/App

PC开发环境操作系统: Windows

PC开发环境操作系统版本号: win10

HBuilderX类型: Alpha

HBuilderX版本号: 4.42

手机系统: HarmonyOS NEXT

手机系统版本号: HarmonyOS NEXT Developer Beta2

手机厂商: 华为

手机机型: Mate 60

页面类型: vue

vue版本: vue3

打包方式: 离线

项目创建方式: HBuilderX

示例代码:

import {
ReadFileOptions,
ReadFileCallback
} from '../interface.uts';

import { fileIo as fs,ReadOptions } from '@kit.CoreFileKit';
import { BusinessError } from '@kit.BasicServicesKit';
import { buffer,util } from '@kit.ArkTS';

export function hsReadFile(tempFilePath: string,options: ReadFileCallback) {

try {  

    if (!fs.accessSync(tempFilePath)) {  
        console.log('检测图片不存在')  
        throw new Error(`Temporary image does not exist at path: ${tempFilePath}`);  
    }  

    let sourceFileFd = fs.openSync(tempFilePath, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE);  
    let pathSize = fs.statSync(sourceFileFd.fd).size;  

    if (sourceFileFd.fd >= 0) {  
        try {  

            let arrayBuffer = new ArrayBuffer(pathSize);  

            let buffSize:number = 4096;  

            let buf = new ArrayBuffer(buffSize);  

            let off:number = 0;  
            let len:number = 0;  
            let readedLen:number = 0;  

            const targetBuffer = new Uint8Array(arrayBuffer);  

            while ((len = fs.readSync(sourceFileFd.fd, buf, { offset: off, length: buffSize } as ReadOptions)) > 0) {  

                readedLen += len;  

                const result = new Uint8Array(buf, 0, len);  

                targetBuffer.set(result, off);  

                off += len;  

            }  

            const helper = new util.Base64Helper();  
            let base64 = helper.encodeToStringSync(targetBuffer)  

            let res : ReadFileOptions = {  
                int8array: base64,  
                videoSize: pathSize,  
                readLen: readedLen  
            };  

            options?.(res);  

        } finally {  
            // 确保在操作完成后关闭文件描述符  
            fs.closeSync(sourceFileFd);  
        }  
    } else {  
        // 如果文件打开失败(例如,由于路径无效或权限问题),则抛出异常  
        throw new Error('Failed to open files for copying');  
    }  

} catch (error) {  
    // 捕获并处理异常  
    console.error('Error saving temporary image to local storage:', error);  
    throw new Error(error); // 重新抛出异常,以便调用者可以处理  
}  

}

操作步骤:

uni.chooseImage({
count: 1,
sizeType: ['original', 'compressed'],
sourceType: ['album'],
success: res => {
console.log("res" , res)
let tempFilePath = res.tempFilePaths[0];
uni.saveFile({
tempFilePath: tempFilePath ,
success: (res) => {

                                hsReadFile(res.savedFilePath,(res)=>{});                  
                                  }  

                           })  

})

预期结果:

正常返回

实际结果:

返回一半或闪退

bug描述:

uts 调用鸿蒙API读取图片ArrayBuffer APP闪退,手机上选择压缩图片读取返回base64,图片只显示一半。返回ArrayBuffer 闪退。选择原图无论返回base64还是ArrayBuffer 都闪退

2024-12-19 11:34 负责人:无 分享
已邀请:

要回复问题请先登录注册