HarmonyOS鸿蒙Next中如何选择本地图片显示在界面上

HarmonyOS鸿蒙Next中如何选择本地图片显示在界面上

今天有小伙伴问我,有没有打来本地图库,选择一张图片的代码,话都说到这儿了,我就来帮你看一下,

由于我阳了,手里只有一个nova10,刚好支持HarmonyOS3

于是我用api8的fa模型给他做了演示。下面是完整代码。

选择一张图片的

import mediaLibrary from '@ohos.multimedia.mediaLibrary';

@Entry
@Component
struct Index {
  @State message: string = '选择图片'
  @State img: string = ''

  private openGally() {
    mediaLibrary.getMediaLibrary().startMediaSelect({
      type: 'image',
      count: 1
    }).then((value) => {
      this.img = value[0];
    })
      .catch((err) => {
        console.log("An error occurred when selecting media resources.");
      });
  }

  build() {
    Row() {
      Column() {
        Text(this.message)
          .fontSize(50)
          .fontWeight(FontWeight.Bold).onClick(() => {
          this.openGally()
        })
        //显示一张
        Image(this.img).width(50).height(50)
      }
      .width('100%')
    }
    .height('100%').backgroundColor(Color.Orange)
  }
}

顺便我也帮着做了

显示多张图片

下面是完整代码

import mediaLibrary from '@ohos.multimedia.mediaLibrary';

@Entry
@Component
struct Index {
  @State message: string = '选择图片'
  @State img: string = ''
  private imgArray: string[] = [];

  private openGally() {
    mediaLibrary.getMediaLibrary().startMediaSelect({
      type: 'image',
      count: 5
    }).then((value) => {
      this.imgArray = value;
    })
      .catch((err) => {
        console.log("An error occurred when selecting media resources.");
      });
  }

  build() {
    Row() {
      Column() {
        Text(this.message)
          .fontSize(50)
          .fontWeight(FontWeight.Bold).onClick(() => {
          this.openGally()
        })
        //显示全部
        ForEach(this.imgArray, (item: any) => { // 循环数组创建每一个Item
          Image(item).height(50).objectFit(ImageFit.Cover)
            .margin(10).borderRadius(20)
        })
      }
      .width('100%')
    }
    .height('100%').backgroundColor(Color.Orange)
  }
}

预览图片

Text("预览图片").fontSize(50).onClick(()=>{
  let index = 1;
  mediaLibrary.getMediaLibrary().startImagePreview(this.imgArray, index).then(() => {
    console.log("Succeeded in previewing the images.");
  }).catch((err) => {
    console.log("An error occurred when previewing the images.");
  });

更多关于HarmonyOS鸿蒙Next中如何选择本地图片显示在界面上的实战教程也可以访问 https://www.itying.com/category-93-b0.html

3 回复

大佬,想问一下如果想向后端发送一个动态类,属性中带有图片的,该怎么传好呢?后端又该怎么存储?前端又该如何请求这样的数据类型?

更多关于HarmonyOS鸿蒙Next中如何选择本地图片显示在界面上的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


在HarmonyOS鸿蒙Next中,选择本地图片并显示在界面上可以通过以下步骤实现:

  1. 使用Image组件:在布局文件中添加Image组件,用于显示图片。
  2. 设置图片资源:通过ohos:image_src属性指定图片资源路径,如$media:image_name
  3. 动态加载图片:在代码中使用Image.setImageResource()方法动态设置图片资源。
  4. 权限申请:如果需要访问外部存储,需在config.json中申请ohos.permission.READ_MEDIA权限。

示例代码:

<Image
    ohos:id="$+id:imageView"
    ohos:width="match_content"
    ohos:height="match_content"
    ohos:image_src="$media:my_image"/>
Image imageView = (Image) findComponentById(ResourceTable.Id_imageView);
imageView.setImageResource(ResourceTable.Media_my_image);

确保图片资源已放置在resources/base/media/目录下。

回到顶部