HarmonyOS鸿蒙Next中APP跳转相册/相机,选择照片后返回uris,怎么读取出来?
HarmonyOS鸿蒙Next中APP跳转相册/相机,选择照片后返回uris,怎么读取出来?
APP 跳转相册, 选择1张照片, (用户可选择原图 / 非原图), 然后在返回内容,读取到arraybuffer。(目的是上传文件到服务器)
let photoPicker = new picker.PhotoViewPicker();
photoPicker.select(options).then((photo: picker.PhotoSelectResult) => {
// 怎么从photo.uris 读取数据到arraybuffer
})
但接下来, 通过fs 读取arraybuffer, 提示Error: Operation not permitted。 查看文档: 一种是申请ohos.permission.READ_IMAGEVIDEO, 但这个不是normal权限了。 一种是通过phAccessHelper.getAssets(fetchOption), 但取到的是缩略图。 两种方案都不适合我需要的场景。 希望能够获取(用户选择的原图 / 非原图)。
更多关于HarmonyOS鸿蒙Next中APP跳转相册/相机,选择照片后返回uris,怎么读取出来?的实战教程也可以访问 https://www.itying.com/category-93-b0.html
可以参考以下demo:
selectedPhoto() {
const photoSelectOptions = new photoAccessHelper.PhotoSelectOptions();
photoSelectOptions.MIMEType = photoAccessHelper.PhotoViewMIMETypes.IMAGE_TYPE; // 过滤选择媒体文件类型为VIDEO
photoSelectOptions.maxSelectNumber = 1; // 选择媒体文件的最大数目
let uris: Array<string> = [];
const photoViewPicker = new photoAccessHelper.PhotoViewPicker();
photoViewPicker.select(photoSelectOptions).then(async (photoSelectResult: photoAccessHelper.PhotoSelectResult) => {
uris = photoSelectResult.photoUris;
console.info('photoViewPicker.select to file succeed and uris are:' + uris);
let file = fs.openSync(uris[0], fs.OpenMode.READ_ONLY);
console.info('file fd:' + file.fd);
const imageSource: image.ImageSource = image.createImageSource(file.fd);
let decodingOptions: image.DecodingOptions = {
editable: true,
desiredPixelFormat: 3,
}
let selectedImgPixelMap = await imageSource.createPixelMap(decodingOptions);
let imageBuffer = await this.packingPixelMap2Jpg(selectedImgPixelMap)
console.log('ArrayBuffer = ' + imageBuffer.byteLength)
}).catch((err: BusinessError) => {
console.error(`Invoke photoViewPicker.select failed, code is ${err.code}, message is ${err.message}`);
})
}
// 打包 PixelMap 为 jpg 格式
async packingPixelMap2Jpg(pixelMap: PixelMap): Promise<ArrayBuffer> {
// 创建ImagePacker实例
const imagePackerApi = image.createImagePacker();
// 设置打包参数
// format:图片打包格式,只支持 jpg 和 webp
// quality:JPEG 编码输出图片质量
// bufferSize:图片大小,默认 10M
const packOpts: image.PackingOption = { format: "image/jpeg", quality: 100 };
let imageBuffer: ArrayBuffer = new ArrayBuffer(1);
try {
// 图片压缩或重新打包
imageBuffer = await imagePackerApi.packing(pixelMap, packOpts);
} catch (err) {
console.error(`Invoke packingPixelMap2Jpg failed, err: ${JSON.stringify(err)}`);
}
return imageBuffer;
}
更多关于HarmonyOS鸿蒙Next中APP跳转相册/相机,选择照片后返回uris,怎么读取出来?的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html
在HarmonyOS鸿蒙Next中,APP跳转相册或相机选择照片后,返回的uris可以通过photoAccessHelper模块进行读取。首先,确保在config.json文件中声明了ohos.permission.READ_MEDIA权限。然后,使用photoAccessHelper.getPhotoAccessHelper(context)获取PhotoAccessHelper实例。通过PhotoKeys指定查询条件,使用fetchPhotoAssets方法获取FetchResult对象,遍历FetchResult获取PhotoAsset对象。最后,通过PhotoAsset的getUri方法获取照片的URI,并使用FileDescriptor或InputStream读取照片数据。
在HarmonyOS鸿蒙Next中,通过startAbilityForResult启动相册或相机应用后,可以在onAbilityResult回调中获取返回的uris。使用ohos.file.fs模块的FileAsset类,通过PhotoViewHelper或PhotoSelectHelper提供的API,将uris转换为FileAsset对象,进而读取照片数据。具体实现可参考官方文档中的PhotoViewHelper或PhotoSelectHelper示例代码。

