HarmonyOS 鸿蒙Next image.createImageSource 打不开 photoViewPicker.select 出来的URI

发布于 1周前 作者 ionicwang 来自 鸿蒙OS

HarmonyOS 鸿蒙Next image.createImageSource 打不开 photoViewPicker.select 出来的URI

cke_522.png

我已将请求并拿到了三个权限:[‘ohos.permission.READ_MEDIA’,‘ohos.permission.WRITE_MEDIA’, ‘ohos.permission.MEDIA_LOCATION’],但是imageSource始终是ubdefined

这种情况该怎么办啊
uri的值是:file://media/Photo/1/IMG_1722854354_000/test.png



关于HarmonyOS 鸿蒙Next image.createImageSource 打不开 photoViewPicker.select 出来的URI的问题,您也可以访问:https://www.itying.com/category-93-b0.html 联系官网客服。

9 回复

cke_186.png

该接口仅支持应用沙箱路径。

参考文档:https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/js-apis-image-V5#imagecreateimagesource

需要将其复制到对应沙箱,才能进行相关操作。

因为我要读取的是图库中的图片,图库中的图片不在应用程序的沙箱范围内,怎么把其他路径的文件复制到文件沙箱中?我看了一下,文件相关接口好像都是要求在沙箱中操作的?

期待HarmonyOS能继续优化多屏协同功能,让跨设备体验更加完美。

import picker from ‘@ohos.multimedia.cameraPicker’ import camera from ‘@ohos.multimedia.camera’; import common from ‘@ohos.app.ability.common’; import { BusinessError } from ‘@ohos.base’; import fileuri from ‘@ohos.file.fileuri’; import fs from ‘@ohos.file.fs’; import photoAccessHelper from ‘@ohos.file.photoAccessHelper’;

let context = getContext(this) as common.Context;

class CameraPosition { cameraPosition: camera.CameraPosition saveUri: string

constructor(cameraPosition: camera.CameraPosition, saveUri: string) { this.cameraPosition = cameraPosition this.saveUri = saveUri } }

let pathDir = getContext().filesDir; console.log(‘保存路径为’+pathDir) let filePath = pathDir + ‘/picture.jpg’ fs.createRandomAccessFileSync(filePath, fs.OpenMode.CREATE); let uri = fileuri.getUriFromPath(filePath); async function photo() { try { let pickerProfile = new CameraPosition(camera.CameraPosition.CAMERA_POSITION_BACK, uri) let pickerResult: picker.PickerResult = await picker.pick(context, [picker.PickerMediaType.PHOTO, picker.PickerMediaType.VIDEO], pickerProfile); console.log(“the pick pickerResult is:” + JSON.stringify(pickerResult)); } catch (error) { let err = error as BusinessError; console.error(the pick call failed. error code: ${err.code}); } } async function picture() { let PhotoSelectOptions = new photoAccessHelper.PhotoSelectOptions(); PhotoSelectOptions.MIMEType = photoAccessHelper.PhotoViewMIMETypes.IMAGE_TYPE; PhotoSelectOptions.maxSelectNumber = 1; let photoPicker = new photoAccessHelper.PhotoViewPicker(); photoPicker.select(PhotoSelectOptions).then((PhotoSelectResult: photoAccessHelper.PhotoSelectResult) => { let photouri: Array<string> = PhotoSelectResult.photoUris let file = fs.openSync(photouri[0], fs.OpenMode.READ_ONLY) let file2 = fs.openSync(pathDir+’/picture2.jpg’, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE) fs.copyFileSync(file.fd, file2.fd) fs.closeSync(file); fs.closeSync(file2); }) }

@Entry @Component export struct Index { build() { Column() { Button(‘选择并保存’).onClick(() => { picture() }) Button(‘拍照并保存’).onClick(() => { photo() }) } } }

ok,问题解决了,非常感谢大佬!

权限申请,你试一下添加和我下面一样的,再运行看看

const PERMISSIONS: Array<Permissions> = [
  'ohos.permission.READ_MEDIA',
  'ohos.permission.WRITE_MEDIA',
  'ohos.permission.MEDIA_LOCATION',
  'ohos.permission.MANAGE_MISSIONS',
  'ohos.permission.WRITE_IMAGEVIDEO'
];<button style="position: absolute; padding: 4px 8px 0px; cursor: pointer; top: 8px; right: 8px; font-size: 14px;">复制</button>

尝试了一下,不太行,好像这个API只能读取当前程序沙箱中的文件.

会不会是异步问题,你加上await看一下 类似这样的 // 获取图片源 let imageSource: PixelMap = await image.createImageSource(fd).createPixelMap();

感谢回答,问题已经解决了,应该是沙箱路径的问题,我之前读取的是其他软件沙箱中的文件。

回到顶部