HarmonyOS鸿蒙Next中保存本地图片功能要怎么做

HarmonyOS鸿蒙Next中保存本地图片功能要怎么做

代码如下

import { http } from '@kit.NetworkKit';
import { BusinessError } from '@ohos.base';
import { photoAccessHelper } from '@kit.MediaLibraryKit';
import { common } from '@kit.AbilityKit';
import fs from '@ohos.file.fs';

export class SaveImageToAlbum {

downloadAndSaveImg(imgUrl: string) {
http.createHttp() .request(imgUrl, (error: BusinessError, data: http.HttpResponse) => {
if (!error && data.result instanceof ArrayBuffer) {
this.saveImage(data.result as ArrayBuffer)
}
})

}

async saveImage(buffer: ArrayBuffer | string): Promise<void> {
let context = getContext(this) as common.UIAbilityContext;
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);
await fs.write(file.fd, buffer);
await fs.close(file.fd);
}
}

photoAccessHelper需要"ohos.permission.READ_IMAGEVIDEO" 跟 "ohos.permission.WRITE_IMAGEVIDEO"这两个权限?申请了这两个发现是ACL级别的权限,无法申请

怎么把网络图片存储到相册里,需要申请什么权限


更多关于HarmonyOS鸿蒙Next中保存本地图片功能要怎么做的实战教程也可以访问 https://www.itying.com/category-93-b0.html

3 回复

保存图片的两种方式:

  1. 按照你说的申请acl权限,需要到AppGrallery Connect进行申请,相册开发参考链接:@ohos.file.photoAccessHelper (相册管理模块)-ArkTS API-Media Library Kit(媒体文件管理服务)-媒体 - 华为HarmonyOS开发者

  2. 使用保存控件可以获得临时授权,但是样式无法改变,参考链接:SaveButton-安全-ArkTS组件-ArkUI(方舟UI框架)-应用框架 - 华为HarmonyOS开发者

更多关于HarmonyOS鸿蒙Next中保存本地图片功能要怎么做的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


在HarmonyOS Next中保存本地图片可以使用PhotoAccessHelper模块。首先,确保在module.json5中声明ohos.permission.READ_MEDIAohos.permission.WRITE_MEDIA权限。接着,通过PhotoAccessHelper获取图片的URI,将其保存到指定位置。示例代码如下:

import photoAccessHelper from '@ohos.file.photoAccessHelper';
import fs from '@ohos.file.fs';

async function saveImageToLocal(imageUri: string, destinationPath: string) {
  try {
    const phAccessHelper = photoAccessHelper.getPhotoAccessHelper();
    const file = await phAccessHelper.createAsset(destinationPath, fs.OpenMode.CREATE | fs.OpenMode.READ_WRITE);
    const inputStream = await fs.createStream(imageUri, 'r');
    const outputStream = await fs.createStream(file.uri, 'w');
    await fs.copy(inputStream, outputStream);
    await fs.closeStream(inputStream);
    await fs.closeStream(outputStream);
  } catch (error) {
    console.error('Error saving image:', error);
  }
}

确保imageUri是有效的图片URI,destinationPath是目标保存路径。

在HarmonyOS(鸿蒙)Next中,保存本地图片可以使用ImageMediaLibrary API。首先,使用Image组件加载图片资源,然后通过MediaLibrary将图片保存到设备的相册中。以下是一个简单的示例代码:

import mediaLibrary from '@ohos.multimedia.mediaLibrary';
import image from '@ohos.multimedia.image';

async function saveImageToGallery(imageUri) {
  const media = mediaLibrary.getMediaLibrary();
  const imageSource = image.createImageSource(imageUri);
  const imagePacker = image.createImagePacker();
  const imageData = await imageSource.createPixelMap();
  const packedImage = await imagePacker.packing(imageData, { format: 'image/jpeg', quality: 100 });
  
  const fileAsset = await media.createAsset(mediaLibrary.MediaType.IMAGE, 'myImage.jpg', packedImage);
  console.log('Image saved to gallery:', fileAsset.uri);
}

此代码将图片保存到设备的相册中,并生成一个名为myImage.jpg的文件。确保在config.json中声明必要的权限,如ohos.permission.WRITE_MEDIA

回到顶部