HarmonyOS调用PhotoAccessHelper.getAssets问题

HarmonyOS调用PhotoAccessHelper.getAssets,需要申请ohos.permission.READ_IMAGEVIDEO,但是权限不让申请,无法获取多媒体资源

HarmonyOS
2024-08-22 20:25:52
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
superinsect

可以参考如下代码,获取到uri后,使用使用fs.openSync接口,通过uri打开这个文件得到fd,通过picker打开相册,获取uri:

Button('打开相册') 
  .onClick(() => { 
    //创建图库选择器对象实例 
    const photoViewPicker =new photoAccessHelper.PhotoViewPicker(); 
    //调用select()接口拉起图库界面进行文件选择,文件选择成功后,返回PhotoSelectResult结果集 
    photoViewPicker.select().then(async (photoSelectResult: picker.PhotoSelectResult) => { 
      //用一个全局变量存储返回的uri 
      selectUris = photoSelectResult.photoUris; 
      console.info('photoViewPicker.select to file succeed and uris are:' + selectUris); 
    }).catch((err: BusinessError) => { 
      console.error(`Invoke photoViewPicker.select failed, code is ${err.code}, message is ${err.message}`); 
    }) 
  }) 
根据返回的uri创建pixelMap: 
Button('creatPixelMap') 
  .margin({ top: 10 }) 
  .onClick((event: ClickEvent) => { 
    //使用fs.openSync接口,通过uri打开这个文件得到fd 
    let file = fs.openSync(selectUris[0], fs.OpenMode.READ_ONLY); 
    console.info('file fd: ' + file.fd); 
    //根据文件fd创建imagSource 
    const imageSource: image.ImageSource = image.createImageSource(file.fd); 
    //完成后关闭fd 
    fs.closeSync(file); 
    let decodingOptions: image.DecodingOptions = { 
      editable: true, 
      desiredPixelFormat: 3, 
    } 
    //创建pixelMap 
    imageSource.createPixelMap(decodingOptions).then(async (pixelMap1: image.PixelMap) => { 
      this.pixelMap = pixelMap1; 
    }); 
  })Button('打开相册') 
  .onClick(() => { 
    //创建图库选择器对象实例 
    const photoViewPicker =new photoAccessHelper.PhotoViewPicker(); 
    //调用select()接口拉起图库界面进行文件选择,文件选择成功后,返回PhotoSelectResult结果集 
    photoViewPicker.select().then(async (photoSelectResult: picker.PhotoSelectResult) => { 
      //用一个全局变量存储返回的uri 
      selectUris = photoSelectResult.photoUris; 
      console.info('photoViewPicker.select to file succeed and uris are:' + selectUris); 
    }).catch((err: BusinessError) => { 
      console.error(`Invoke photoViewPicker.select failed, code is ${err.code}, message is ${err.message}`); 
    }) 
  }) 
根据返回的uri创建pixelMap: 
Button('creatPixelMap') 
  .margin({ top: 10 }) 
  .onClick((event: ClickEvent) => { 
    //使用fs.openSync接口,通过uri打开这个文件得到fd 
    let file = fs.openSync(selectUris[0], fs.OpenMode.READ_ONLY); 
    console.info('file fd: ' + file.fd); 
    //根据文件fd创建imagSource 
    const imageSource: image.ImageSource = image.createImageSource(file.fd); 
    //完成后关闭fd 
    fs.closeSync(file); 
    let decodingOptions: image.DecodingOptions = { 
      editable: true, 
      desiredPixelFormat: 3, 
    } 
    //创建pixelMap 
    imageSource.createPixelMap(decodingOptions).then(async (pixelMap1: image.PixelMap) => { 
      this.pixelMap = pixelMap1; 
    }); 
  })
分享
微博
QQ
微信
回复
2024-08-23 17:42:04
相关问题
HarmonyOS API调用崩溃问题
362浏览 • 1回复 待解决
HarmonyOS 调用内部资源问题
300浏览 • 1回复 待解决
HarmonyOS @Watch函数调用问题
271浏览 • 1回复 待解决
HarmonyOS WebController存在调用顺序问题
173浏览 • 1回复 待解决
HarmonyOS TextInput调用系统键盘问题
178浏览 • 1回复 待解决
HarmonyOS调用相机设备API获取失败问题
317浏览 • 1回复 待解决
js调用Java,问题求助?
5386浏览 • 1回复 待解决