harmony-utils之PhotoHelper,相冊相關(guān)工具類
harmony-utils 簡介與說明
harmony-utils 一款功能豐富且極易上手的HarmonyOS工具庫,借助眾多實用工具類,致力于助力開發(fā)者迅速構(gòu)建鴻蒙應用。其封裝的工具涵蓋了APP、設備、屏幕、授權(quán)、通知、線程間通信、彈框、吐司、生物認證、用戶首選項、拍照、相冊、掃碼、文件、日志,異常捕獲、字符、字符串、數(shù)字、集合、日期、隨機、base64、加密、解密、JSON等一系列的功能和操作,能夠滿足各種不同的開發(fā)需求。
picker_utils 是harmony-utils拆分出來的一個子庫,包含PickerUtil、PhotoHelper、ScanUtil。
下載安裝ohpm i @pura/harmony-utils
ohpm i @pura/picker_utils
//全局初始化方法,在UIAbility的onCreate方法中初始化 AppUtil.init()
onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void {
AppUtil.init(this.context);
}
API方法與使用
select 通過選擇模式拉起photoPicker界面,用戶可以選擇一個或多個圖片/視頻
//相冊選擇圖片
PhotoHelper.select().then((result) = > {
let uris = result.photoUris;
let uriStr = `調(diào)用相冊,返回uris:n${uris.join('n')}`;
}).catch((err: BusinessError) = > {
let str = `調(diào)用相冊,異常:n${JSON.stringify(err)}`;
});
selectEasy 通過選擇模式拉起photoPicker界面,用戶可以選擇一個或多個圖片/視頻
//相冊選擇圖片/視頻(多選)
let options: photoAccessHelper.PhotoSelectOptions = {
MIMEType: photoAccessHelper.PhotoViewMIMETypes.IMAGE_VIDEO_TYPE,
maxSelectNumber: 12,
isPhotoTakingSupported: false,
isSearchSupported: false,
isEditSupported: false,
isOriginalSupported: true
}
PhotoHelper.selectEasy(options).then((uris) = > {
let uriStr = `調(diào)用相冊,返回uris:n${uris.join('n')}`;
}).catch((err: BusinessError) = > {
let str = `調(diào)用相冊,異常:n${JSON.stringify(err)}`;
});
//相冊選擇圖片(單選)
let options: photoAccessHelper.PhotoSelectOptions = {
MIMEType: photoAccessHelper.PhotoViewMIMETypes.IMAGE_TYPE,
maxSelectNumber: 1,
isOriginalSupported: true,
isPreviewForSingleSelectionSupported: true //單選模式下是否需要進大圖預覽
}
PhotoHelper.selectEasy(options).then((uris) = > {
let uriStr = `調(diào)用相冊,返回uris:n${uris.join('n')}`;
}).catch((err: BusinessError) = > {
let str = `調(diào)用相冊,異常:n${JSON.stringify(err)}`;
});
save 申請權(quán)限保存,保存圖片或視頻到相冊
//圖片保存進相冊(已申請權(quán)限使用該方法)
let ps: Permissions[] = ['ohos.permission.WRITE_IMAGEVIDEO'];
PermissionUtil.requestPermissions(ps).then((result) = > {
if (result) {
let imgName = `測試圖片_${DateUtil.getTodayTime()}`;
PhotoHelper.save(photoAccessHelper.PhotoType.IMAGE, 'jpg', { title: imgName }).then(async (uri) = > {
if (uri) {
let uriStr = `保存圖片成功,返回uris:n${uri}`;
let file = FileUtil.openSync(uri);
FileUtil.copyFile(this.filePath, file.fd).then(() = > {
FileUtil.close(file.fd);
ToastUtil.showToast("圖片保存成功");
})
}
}).catch((err: BusinessError) = > {
let str = `調(diào)用保存圖片,異常:n${JSON.stringify(err)}`;
})
} else {
ToastUtil.showLong("請在設置中打開權(quán)限");
WantUtil.toAppSetting();
}
});
showAssetsCreationDialog 彈窗授權(quán)保存,調(diào)用接口拉起保存確認彈窗
let pixelMap = await ImageUtil.getPixelMapFromMedia($r("app.media.test_as4"));
let filePath = await ImageUtil.savePixelMap(pixelMap, FileUtil.getFilesDirPath(""), "測試圖片.png");
let uri = FileUtil.getUriFromPath(this.filePath);
let srcFileUris: Array< string >=[uri];
let desFileUris: Array< string > = await PhotoHelper.showAssetsCreationDialog(srcFileUris);
for (let index = 0; index < desFileUris.length; index++) {
//將來源于應用沙箱的照片內(nèi)容寫入媒體庫的目標uri
let srcFile: fs.File = await Utils.open(srcFileUris[index], fs.OpenMode.READ_ONLY);
let desFile: fs.File = await Utils.open(desFileUris[index], fs.OpenMode.WRITE_ONLY);
await Utils.copyFile(srcFile.fd, desFile.fd);
await Utils.close(srcFile);
await Utils.close(desFile);
}
showAssetsCreationDialogEasy 彈窗授權(quán)保存,調(diào)用接口拉起保存確認彈窗,并保存
let pixelMap = await ImageUtil.getPixelMapFromMedia($r("app.media.test_as4"));
let filePath = await ImageUtil.savePixelMap(pixelMap, FileUtil.getFilesDirPath(""), "測試圖片.png");
let uri = FileUtil.getUriFromPath(this.filePath);
PhotoHelper.showAssetsCreationDialogEasy([uri, uri2]).then((result) = > {
let uriStr = `圖片保存成功,返回uris:n${JSON.stringify(result, null, 2)}`;
DialogHelper.showToast("圖片保存成功!");
}).catch((error: BusinessError) = > {
DialogHelper.showToast("圖片保存失敗!");
});
applyChanges 安全控件保存,提交媒體變更請求,插入圖片/視頻
//安全控件保存,圖片保存進相冊。
let pixelMap = await ImageUtil.getPixelMapFromMedia($r("app.media.test_as4"));
let filePath = await ImageUtil.savePixelMap(pixelMap, FileUtil.getFilesDirPath(""), "測試圖片.png");
let uri = FileUtil.getUriFromPath(this.filePath);
PhotoHelper.applyChanges(uri).then((result) = > {
let uriStr = `保存圖片成功:${result.uri}`;
}).catch((err: BusinessError) = > {
let str = `保存圖片失敗:${JSON.stringify(err)}`;
});
getPhotoAsset 獲取對應uri的PhotoAsset對象,用于讀取文件信息
PickerUtil.selectPhoto().then(async (uris) = > {
if (uris && uris.length > 0) {
PhotoHelper.getPhotoAsset(uris[0]).then((photoAsset) = > {
try {
let name = photoAsset?.get(photoAccessHelper.PhotoKeys.DISPLAY_NAME);
let type = photoAsset?.get(photoAccessHelper.PhotoKeys.PHOTO_TYPE);
let title = photoAsset?.get(photoAccessHelper.PhotoKeys.TITLE.toString());
let size = photoAsset?.get(photoAccessHelper.PhotoKeys.SIZE.toString());
let with1 = photoAsset?.get(photoAccessHelper.PhotoKeys.WIDTH.toString());
let height = photoAsset?.get(photoAccessHelper.PhotoKeys.HEIGHT.toString());
let date = photoAsset?.get(photoAccessHelper.PhotoKeys.DATE_TAKEN.toString());
let orientation = photoAsset?.get(photoAccessHelper.PhotoKeys.ORIENTATION.toString());
let uriStr = `圖片信息:n文件名:${name}n文件類型:${type}n文件大小:${size}n圖片寬度:${with1}n圖片高度:${height}n拍攝日期:${date}n文件標題:${title}n圖片文件的方向:${orientation}`
} catch (err) {
LogUtil.error("讀取圖片信息失敗:" + JSON.stringify(err));
}
photoAsset?.getThumbnail((err, pixelMap) = > {
if (err) {
LogUtil.error("縮略圖-異常:" + JSON.stringify(err));
return;
}
// this.pixelMap = pixelMap;
})
}).catch((err: BusinessError) = > {
let str = `讀取圖片異常:n${JSON.stringify(err)}`;
});
} else {
ToastUtil.showToast("請選擇圖片");
}
}).catch((err: BusinessError) = > {
let str = `異常:n${JSON.stringify(err)}`;
});
創(chuàng)作不易,請給童長老點贊
審核編輯 黃宇
-
HarmonyOS
+關(guān)注
關(guān)注
80文章
2148瀏覽量
32535
發(fā)布評論請先 登錄
玩 High API 系列之:智能云相冊
軟件商推谷歌Docs與Outlook同步工具Harmony
Microchip Minutes - MPLAB? Harmony專輯 - 第5集 - MPLAB Harmony中的項目恢復和備份選項
Microchip Minutes - MPLAB? Harmony專輯 - 第5集 - MPLAB Harmony中的項目恢復和備份選項
MPLAB? Harmony圖形設計器

如何使用第三方庫中的@pura/harmony-utils(V1.3.3)申請授權(quán)工具類一

harmony-utils之AppUtil,APP相關(guān)工具類

評論