1/* 2 * Copyright (c) 2022 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16import {LogUtil} from './LogUtil' 17import {MediaLibraryAccess} from '../access/MediaLibraryAccess'; 18import fileio from '@ohos.fileio'; 19 20const TAG = "OperationUtils" 21export class OperationUtils { 22 23 24 async copy(uri: string):Promise<string> { 25 try { 26 var context = globalThis.noteContext 27 let fileAsset = await this.getFileAssetById(uri, context); 28 if (fileAsset == null || fileAsset == undefined) { 29 LogUtil.warn(TAG, "Failed to get fileAsset") 30 return; 31 } 32 33 let fd = await MediaLibraryAccess.openAsset('RW', fileAsset); 34 if (fd <= 0) { 35 LogUtil.warn(TAG, "fd is invaild") 36 return; 37 } 38 LogUtil.info(TAG, 'copyAsset read end!') 39 40 // 通过Uri拷贝图片到指定指定的包路径下 41 // 1、获取本地路径 42 var dir = context.filesDir 43 // 2、生成本地文件名 44 var time = new Date().getTime() 45 var imagePath = dir + "/" + time.toString() + "_note" + ".jpg" 46 // 3、拷贝 47 await fileio.copyFile(fd, imagePath) 48 // 4、关闭安fd,Asset 49 await MediaLibraryAccess.closeAsset(fd, fileAsset); 50 } catch (error) { 51 LogUtil.info(TAG, "create error: " + error); 52 return; 53 } 54 LogUtil.debug(TAG, 'copy end'); 55 56 return imagePath.toString() 57 } 58 59 saveImageData(dataArray: Uint8Array, imageType: string): string { 60 try { 61 var context = globalThis.noteContext 62 // 1、获取本地路径 63 var dir = context.filesDir 64 var time = new Date().getTime() 65 var imagePath = dir + "/" + time.toString() + "_note" + "." + imageType 66 let fd = fileio.openSync(imagePath, 0o100 | 0o2, 0o666) 67 // 3、把image数据写入本地图片文件中 68 fileio.writeSync(fd, dataArray.buffer) 69 } catch (error) { 70 LogUtil.info(TAG, "create error: " + error); 71 return; 72 } 73 LogUtil.info(TAG, 'save image end'); 74 75 return imagePath.toString() 76 } 77 78 saveImage(imageData: string, imageType: string): string { 79 try { 80 var context = globalThis.noteContext 81 // 1、获取本地路径 82 var dir = context.filesDir 83 // 2、生成本地文件名 84 var time = new Date().getTime() 85 var imagePath = dir + "/" + time.toString() + "_note" + "." + imageType 86 let fd = fileio.openSync(imagePath, 0o100 | 0o2, 0o666) 87 let arrayBuffer = new ArrayBuffer(imageData.length) 88 let ia = new Uint8Array(arrayBuffer) 89 for(let i = 0; i < imageData.length; i++) { 90 ia[i] = imageData.charCodeAt(i) 91 } 92 93 // 3、把image数据写入本地图片文件中 94 fileio.writeSync(fd, ia.buffer) 95 96 } catch (error) { 97 LogUtil.info(TAG, "create error: " + error); 98 return; 99 } 100 LogUtil.debug(TAG, 'save image end'); 101 102 return imagePath.toString() 103 } 104 105 getIdByUri(uri:string): number { 106 let srcIndex = uri.lastIndexOf('/'); 107 let srcEnd = uri.length; 108 let srcId = uri.substring(srcIndex + 1, srcEnd); 109 let numId = new Number(srcId); 110 LogUtil.debug(TAG, `getIdByUri numId: ${numId}`); 111 return numId.valueOf(); 112 } 113 114 async getFileAssetById(imagePath, context){ 115 LogUtil.info(TAG, 'getFileAssetById', imagePath); 116 let fetchOpt = { 117 selections : '', 118 selectionArgs : [], 119 uri:imagePath 120 } 121 122 try { 123 let result = await MediaLibraryAccess.getFirstObject(fetchOpt, context); 124 if (result == null) { 125 LogUtil.info(TAG, 'getFileAssetByUri fail'); 126 return null; 127 } 128 return result.obj; 129 } catch (error) { 130 LogUtil.info(TAG, 'getFileAssetById error'); 131 return null; 132 } 133 } 134} 135 136let mOperationUtils = new OperationUtils(); 137export default mOperationUtils as OperationUtils; 138