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
保存图片的两种方式:
-
按照你说的申请acl权限,需要到AppGrallery Connect进行申请,相册开发参考链接:@ohos.file.photoAccessHelper (相册管理模块)-ArkTS API-Media Library Kit(媒体文件管理服务)-媒体 - 华为HarmonyOS开发者
-
使用保存控件可以获得临时授权,但是样式无法改变,参考链接:SaveButton-安全-ArkTS组件-ArkUI(方舟UI框架)-应用框架 - 华为HarmonyOS开发者
更多关于HarmonyOS鸿蒙Next中保存本地图片功能要怎么做的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html
在HarmonyOS Next中保存本地图片可以使用PhotoAccessHelper
模块。首先,确保在module.json5
中声明ohos.permission.READ_MEDIA
和ohos.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中,保存本地图片可以使用Image
和MediaLibrary
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
。