HarmonyOS PhotoPickerComponent选取的相册路径fs无法读取

通过PhotoPickerComponent选取的相册路径无法通过fs读取。

file://media/Photo/45/IMG_1720062912_042/1720062812226.png             
let photoSelect = fs.openSync(this.selectUris[0], fs.OpenMode.READ_ONLY);
HarmonyOS
3天前
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
Excelsior_abit

官方提供的demo是可以的,做了简单修改:

import { PhotoPickerComponent, PickerController, PickerOptions, DataType } from '@ohos.file.PhotoPickerComponent'
import { photoAccessHelper } from '@kit.MediaLibraryKit'
import { image } from '@kit.ImageKit';
import fs from '@ohos.file.fs';
@Entry
@Component
struct Index {
  pickerOptions: PickerOptions = new PickerOptions();
  @State pickerController: PickerController = new PickerController();
  @State selectUris: Array<string> = new Array<string>();
  @State pixelMap: image.PixelMap | undefined = undefined
  aboutToAppear() {
    this.pickerOptions.MIMEType = photoAccessHelper.PhotoViewMIMETypes.IMAGE_TYPE;
    this.pickerOptions.maxSelectNumber = 10;
    this.pickerOptions.isSearchSupported = false;
    this.pickerOptions.isPhotoTakingSupported = false;
    // 其他属性.....
  }
  private onSelect(uri: string) {
    // 添加
    if (uri) {
      this.selectUris.push(uri);
    }
  }
  private onDeselect(uri: string): void {
    // 移除
    if (uri) {
      this.selectUris = this.selectUris.filter((item: string) => {
        return item != uri;
      })
    }
  }
  async getPixelMap(){
    let file = fs.openSync(this.selectUris[0], fs.OpenMode.READ_ONLY);
    this.pixelMap = await image.createImageSource(file.fd).createPixelMap();
  }
  build() {
    Stack() {
      PhotoPickerComponent({
        pickerOptions: this.pickerOptions,
        onSelect:(uri: string): void => this.onSelect(uri),
        onDeselect:(uri: string): void => this.onDeselect(uri),
        pickerController: this.pickerController,
      }).height('100%').width('100%')
      // 这里模拟应用侧底部的选择栏
      Column(){
        Row() {
          ForEach(this.selectUris, (uri: string) => {
            Image(uri).height('10%').width('10%').onClick(() => {
              // 点击事件模拟删除操作,通过pickerController向picker组件发送已选择的数据列表,触发picker组件勾选框刷新
              this.selectUris = this.selectUris.filter((item: string) => {
                return item != uri;
              })
              this.pickerController.setData(DataType.SET_SELECTED_URIS, this.selectUris);
            })
          }, (uri: string) => JSON.stringify(uri))
        }
        Button('展示图片').width(80).height(40)
          .onClick(()=>{
            this.getPixelMap()
          })
        Image(this.pixelMap).width("20%").height('20%')
      }
    }
  }
}
分享
微博
QQ
微信
回复
3天前
相关问题
HarmonyOS fs模块读取文件问题
594浏览 • 1回复 待解决
HarmonyOS 读取相册图片无效
26浏览 • 1回复 待解决
如何读取读取分布式路径文件
392浏览 • 1回复 待解决
如何读取相册图片
941浏览 • 1回复 待解决
HarmonyOS 读取相册图片字节流
34浏览 • 1回复 待解决
HarmonyOS怎么获取系统相册路径
451浏览 • 1回复 待解决
HarmonyOS 读取相册文件问题咨询
30浏览 • 1回复 待解决
HarmonyOS 图片资源选取
462浏览 • 1回复 待解决
HarmonyOS 无法读取文件
59浏览 • 1回复 待解决
HarmonyOS C++如何读取指定路径文件
34浏览 • 1回复 待解决