天河建网站公司,东莞寮步,网站建设培训公司,河北省住房建设厅政务网站【HarmonyOS】 鸿蒙保存图片或视频到相册
前言
鸿蒙中保存图片或者视频#xff0c;或者其他媒体文件到设备的媒体库#xff0c;可以是相册#xff0c;也可以是文件管理等。共有两种方式#xff1a;
需要应用申请受限权限#xff0c;获取文件读写的权限#xff08;调用…【HarmonyOS】 鸿蒙保存图片或视频到相册
前言
鸿蒙中保存图片或者视频或者其他媒体文件到设备的媒体库可以是相册也可以是文件管理等。共有两种方式
需要应用申请受限权限获取文件读写的权限调用需要ohos.permission.READ_IMAGEVIDEO和ohos.permission.WRITE_IMAGEVIDEO的权限这样就可以将媒体资源图片or视频or等等保存到媒体库。通过安全控件用户触发后表示同意可以临时授权给应用就行保存处理。
关于第二种安全控件又分为saveButton保存按钮和showAssetsCreationDialog授权弹框两种形式。前者的按钮样式不能自定义所以后者以弹框的形式让用户操作触发入口的样式应用就可以自定义了。后者算是前者的一种替代补充。
一、保存图片和视频授权示例
需要申请ohos.permission.READ_IMAGEVIDEO和ohos.permission.WRITE_IMAGEVIDEO权限。该权限是管制权限需要你的应用去通过场景申请【申请使用受限权限】
配置权限READ_IMAGEVIDEOWRITE_IMAGEVIDEO
requestPermissions: [{name: ohos.permission.READ_IMAGEVIDEO,usedScene: {abilities: [EntryAbility],when: inuse},reason: $string:CAMERA},{name: ohos.permission.WRITE_IMAGEVIDEO,usedScene: {abilities: [EntryAbility],when: inuse},reason: $string:CAMERA}
]向用户申请权限
// 创建申请权限明细async reqPermissionsFromUser(): Promisenumber[] {let context getContext() as common.UIAbilityContext;let atManager abilityAccessCtrl.createAtManager();let grantStatus await atManager.requestPermissionsFromUser(context, [ohos.permission.READ_IMAGEVIDEO,ohos.permission.WRITE_IMAGEVIDEO]);return grantStatus.authResults;}// 用户申请权限async requestPermission() {let grantStatus await this.reqPermissionsFromUser();for (let i 0; i grantStatus.length; i) {if (grantStatus[i] 0) {// 用户授权可以继续访问目标操作}}}保存图片到媒体库
public async savePicture(buffer: ArrayBuffer): Promisevoid {let helper : photoAccessHelper.PhotoAccessHelper photoAccessHelper.getPhotoAccessHelper(getContext(this) as common.UIAbilityContext);let options: photoAccessHelper.CreateOptions {title: Date.now().toString()};let photoUri: string await helper.createAsset(photoAccessHelper.PhotoType.IMAGE, jpg, options);console.info(photoUri)// createAsset的调用需要ohos.permission.READ_IMAGEVIDEO和ohos.permission.WRITE_IMAGEVIDEO的权限let file: fs.File fs.openSync(photoUri, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE);await fs.write(file.fd, buffer);fs.closeSync(file);
}二、saveButton示例
import { photoAccessHelper } from kit.MediaLibraryKit;Entry
Component
struct saveButtonExample {saveButtonOptions: SaveButtonOptions {icon: SaveIconStyle.FULL_FILLED,text: SaveDescription.SAVE_IMAGE,buttonType: ButtonType.Capsule} // 设置安全控件按钮属性build() {Row() {Column() {SaveButton(this.saveButtonOptions) // 创建安全控件按钮.onClick(async (event, result: SaveButtonOnClickResult) {if (result SaveButtonOnClickResult.SUCCESS) {try {let context getContext();let phAccessHelper photoAccessHelper.getPhotoAccessHelper(context);// 需要确保fileUri对应的资源存在let fileUri file://com.example.temptest/data/storage/el2/base/haps/entry/files/test.jpg;let assetChangeRequest: photoAccessHelper.MediaAssetChangeRequest photoAccessHelper.MediaAssetChangeRequest.createImageAssetRequest(context, fileUri);await phAccessHelper.applyChanges(assetChangeRequest);console.info(createAsset successfully, uri: assetChangeRequest.getAsset().uri);} catch (err) {console.error(create asset failed with error: ${err.code}, ${err.message});}} else {console.error(SaveButtonOnClickResult create asset failed);}})}.width(100%)}.height(100%)}
}三、 showAssetsCreationDialog示例
import { photoAccessHelper } from kit.MediaLibraryKit;
import { fileIo } from kit.CoreFileKit;let context getContext(this);
let phAccessHelper photoAccessHelper.getPhotoAccessHelper(context);async function showAssetsCreationDialogExample() {try {// 指定待保存到媒体库的位于应用沙箱的图片urilet srcFileUri file://com.example.temptest/data/storage/el2/base/haps/entry/files/test.jpg;let srcFileUris: Arraystring [srcFileUri];// 指定待保存照片的创建选项包括文件后缀和照片类型标题和照片子类型可选let photoCreationConfigs: ArrayphotoAccessHelper.PhotoCreationConfig [{title: test, // 可选fileNameExtension: jpg,photoType: photoAccessHelper.PhotoType.IMAGE,subtype: photoAccessHelper.PhotoSubtype.DEFAULT, // 可选}];// 基于弹窗授权的方式获取媒体库的目标urilet desFileUris: Arraystring await phAccessHelper.showAssetsCreationDialog(srcFileUris, photoCreationConfigs);// 将来源于应用沙箱的照片内容写入媒体库的目标urilet desFile: fileIo.File await fileIo.open(desFileUris[0], fileIo.OpenMode.WRITE_ONLY);let srcFile: fileIo.File await fileIo.open(srcFileUri, fileIo.OpenMode.READ_ONLY);await fileIo.copyFile(srcFile.fd, desFile.fd);fileIo.closeSync(srcFile);fileIo.closeSync(desFile);console.info(create asset by dialog successfully);} catch (err) {console.error(failed to create asset by dialog successfully errCode is: ${err.code}, ${err.message});}
}注意 1. 使用createAsset需要指定是视频还是图片 fileio.open并不校验文件内容 2. 使用createImageAssetRequest接口如果要保存视频需要用createVideoeAssetRequest 或者还可以直接使用createAssetRequest和addResource的方式 文章转载自: http://www.morning.czlzn.cn.gov.cn.czlzn.cn http://www.morning.mmzfl.cn.gov.cn.mmzfl.cn http://www.morning.lrplh.cn.gov.cn.lrplh.cn http://www.morning.dnls.cn.gov.cn.dnls.cn http://www.morning.xtgzp.cn.gov.cn.xtgzp.cn http://www.morning.bslkt.cn.gov.cn.bslkt.cn http://www.morning.mqfhy.cn.gov.cn.mqfhy.cn http://www.morning.jrsgs.cn.gov.cn.jrsgs.cn http://www.morning.knmby.cn.gov.cn.knmby.cn http://www.morning.jmdpp.cn.gov.cn.jmdpp.cn http://www.morning.rgnq.cn.gov.cn.rgnq.cn http://www.morning.sjwzz.cn.gov.cn.sjwzz.cn http://www.morning.qkdcb.cn.gov.cn.qkdcb.cn http://www.morning.vattx.cn.gov.cn.vattx.cn http://www.morning.huayaosteel.cn.gov.cn.huayaosteel.cn http://www.morning.pzjrm.cn.gov.cn.pzjrm.cn http://www.morning.krxzl.cn.gov.cn.krxzl.cn http://www.morning.yxyyp.cn.gov.cn.yxyyp.cn http://www.morning.ccyns.cn.gov.cn.ccyns.cn http://www.morning.mkxxk.cn.gov.cn.mkxxk.cn http://www.morning.lgnrl.cn.gov.cn.lgnrl.cn http://www.morning.lbxcc.cn.gov.cn.lbxcc.cn http://www.morning.lqtwb.cn.gov.cn.lqtwb.cn http://www.morning.bsqth.cn.gov.cn.bsqth.cn http://www.morning.dgknl.cn.gov.cn.dgknl.cn http://www.morning.clgbb.cn.gov.cn.clgbb.cn http://www.morning.xxhc.cn.gov.cn.xxhc.cn http://www.morning.pzpj.cn.gov.cn.pzpj.cn http://www.morning.mnygn.cn.gov.cn.mnygn.cn http://www.morning.xbwqg.cn.gov.cn.xbwqg.cn http://www.morning.rwcw.cn.gov.cn.rwcw.cn http://www.morning.gstg.cn.gov.cn.gstg.cn http://www.morning.lqljj.cn.gov.cn.lqljj.cn http://www.morning.rsqpc.cn.gov.cn.rsqpc.cn http://www.morning.qynnw.cn.gov.cn.qynnw.cn http://www.morning.jpwmk.cn.gov.cn.jpwmk.cn http://www.morning.tcsdlbt.cn.gov.cn.tcsdlbt.cn http://www.morning.lwqst.cn.gov.cn.lwqst.cn http://www.morning.mpgfk.cn.gov.cn.mpgfk.cn http://www.morning.tznlz.cn.gov.cn.tznlz.cn http://www.morning.lmjtp.cn.gov.cn.lmjtp.cn http://www.morning.fqhbt.cn.gov.cn.fqhbt.cn http://www.morning.tgnwt.cn.gov.cn.tgnwt.cn http://www.morning.hnkkf.cn.gov.cn.hnkkf.cn http://www.morning.hmxb.cn.gov.cn.hmxb.cn http://www.morning.kpgms.cn.gov.cn.kpgms.cn http://www.morning.kzpxc.cn.gov.cn.kzpxc.cn http://www.morning.c7498.cn.gov.cn.c7498.cn http://www.morning.xctdn.cn.gov.cn.xctdn.cn http://www.morning.mwhqd.cn.gov.cn.mwhqd.cn http://www.morning.hxmqb.cn.gov.cn.hxmqb.cn http://www.morning.jftl.cn.gov.cn.jftl.cn http://www.morning.bswnf.cn.gov.cn.bswnf.cn http://www.morning.nqgjn.cn.gov.cn.nqgjn.cn http://www.morning.hgcz.cn.gov.cn.hgcz.cn http://www.morning.mhmsn.cn.gov.cn.mhmsn.cn http://www.morning.mlnby.cn.gov.cn.mlnby.cn http://www.morning.ktnt.cn.gov.cn.ktnt.cn http://www.morning.zlnmm.cn.gov.cn.zlnmm.cn http://www.morning.skpdg.cn.gov.cn.skpdg.cn http://www.morning.jbpdk.cn.gov.cn.jbpdk.cn http://www.morning.hqpyt.cn.gov.cn.hqpyt.cn http://www.morning.lrskd.cn.gov.cn.lrskd.cn http://www.morning.lpbrp.cn.gov.cn.lpbrp.cn http://www.morning.xrlwr.cn.gov.cn.xrlwr.cn http://www.morning.ykmkz.cn.gov.cn.ykmkz.cn http://www.morning.duckgpt.cn.gov.cn.duckgpt.cn http://www.morning.hmqwn.cn.gov.cn.hmqwn.cn http://www.morning.mtrfz.cn.gov.cn.mtrfz.cn http://www.morning.kxsnp.cn.gov.cn.kxsnp.cn http://www.morning.litao7.cn.gov.cn.litao7.cn http://www.morning.bgqr.cn.gov.cn.bgqr.cn http://www.morning.nhrkl.cn.gov.cn.nhrkl.cn http://www.morning.guofenmai.cn.gov.cn.guofenmai.cn http://www.morning.zpnfc.cn.gov.cn.zpnfc.cn http://www.morning.tnjz.cn.gov.cn.tnjz.cn http://www.morning.dtzxf.cn.gov.cn.dtzxf.cn http://www.morning.ryqsq.cn.gov.cn.ryqsq.cn http://www.morning.bzsqr.cn.gov.cn.bzsqr.cn http://www.morning.bylzr.cn.gov.cn.bylzr.cn