HarmonyOS 如何获取到图片的大小,图片大小是多少k,图片名称

HarmonyOS
3天前
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
zbw_apple

可以使用photoAccessHelper模块获取,另外接口需要申请READ/WRITE_IMAGEVIDEO权限,文档地址:https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/js-apis-photoaccesshelper-V5

参考demo如下

import dataSharePredicates from '@ohos.data.dataSharePredicates';
import photoAccessHelper from '@ohos.file.photoAccessHelper';
import fs from '@ohos.file.fs';
import { BusinessError } from '@ohos.base';
import abilityAccessCtrl, { Permissions } from '@ohos.abilityAccessCtrl'

function getPhoto(context:Context){
  let atManager = abilityAccessCtrl.createAtManager();
  // requestPermissionsFromUser会判断权限的授权状态来决定是否唤起弹窗
  const permissions: Array<Permissions> = ['ohos.permission.READ_MEDIA',"ohos.permission.WRITE_MEDIA"];


  atManager.requestPermissionsFromUser(context, permissions).then(async (data) => { //需要用户允许授权图库权限
    let grantStatus: Array<number> = data.authResults;
    let length: number = grantStatus.length;
    for (let i = 0; i < length; i++) {
      if (grantStatus[i] === 0) {  //用户同意权限之后进行的操作


        let phAccessHelper = photoAccessHelper.getPhotoAccessHelper(context);  //context为getContext获取
        console.info('albumGetAssetsDemoPromise');
        let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates(); //筛选条件
        let predicates1: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates();//筛选条件
        let albumName: photoAccessHelper.AlbumKeys = photoAccessHelper.AlbumKeys.ALBUM_NAME;
        let title: photoAccessHelper.PhotoKeys = photoAccessHelper.PhotoKeys.TITLE;
        // predicates.equalTo(albumName, 'test2');
        let albumFetchOptions: photoAccessHelper.FetchOptions = {
          fetchColumns: [],
          predicates: predicates
        };
        let fetchOption: photoAccessHelper.FetchOptions = {
          fetchColumns: ['title'],
          predicates: predicates1
        };
        const albumList: photoAccessHelper.FetchResult<photoAccessHelper.Album> = await phAccessHelper.
        getAlbums(photoAccessHelper.AlbumType.USER , photoAccessHelper.AlbumSubtype.ANY, albumFetchOptions); //获取满足条件的相册列表,如果没加筛选条件就是所有相册


        while(await albumList.isAfterLast()!=null){
          console.info('photoAsset displayName: 数量',albumList.getCount());
          let album: photoAccessHelper.Album = await albumList.getNextObject(); //获取下一个
          console.info('photoAsset displayName: ', album.albumName); //名字
          console.info('photoAsset displayName: ', album.albumSubtype); //名字
          console.info('photoAsset displayName:------------------------------------------------------- ')
          album.getAssets(fetchOption).then(async (albumFetchResult) => {
            while (!albumFetchResult.isAfterLast()) { //循环打印相册中的图片
              let photoAsset: photoAccessHelper.PhotoAsset = await albumFetchResult.getNextObject();
              let photoTitle = photoAsset.get(title);
              console.info(`xxxwidth is ${JSON.stringify(photoTitle)}`);
              // console.info('photoAsset displayName: ', photoAsset.uri); //图片视频资源uri

              // if (photoAsset === undefined) {
              //   console.error('asset not exist');
              //   return;
              // }
              //  phAccessHelper.createDeleteRequest([photoAsset.uri]);
              // console.info('createDeleteRequest successfully');

            }
            console.info('photoAsset displayName:------------------------------------------------------- ')
          }).catch((err: BusinessError) => {
            console.error('album getPhotoAssets failed with error: ' + err);
          });
        }

      } else {
        // 用户拒绝授权,提示用户必须授权才能访问当前页面的功能,并引导用户到系统设置中打开相应的权限
        return;
      }
    }
    // 授权成功
  }).catch((err:BusinessError) => {
    console.error(`requestPermissionsFromUser failed, code is ${err.code}, message is ${err.message}`);
  })
}
@Entry
@Component
struct Index {
  @State message: string = 'Hello World';

  build() {
    Row() {
      Column() {
        Text(this.message)
          .fontSize(50)
          .fontWeight(FontWeight.Bold)
        Button("获取")
          .onClick(()=>{
            let context=getContext()
            getPhoto(context)
          })
      }
      .width('100%')
    }
    .height('100%')
  }
}
分享
微博
QQ
微信
回复
3天前
相关问题
HarmonyOS 线程栈默认大小是多少
197浏览 • 1回复 待解决
HarmonyOS图片压缩不到指定大小
636浏览 • 1回复 待解决
鸿蒙能获取图片指定大小缩略图吗
9612浏览 • 1回复 待解决
图片压缩尺寸和大小问题
411浏览 • 1回复 待解决
图片压缩指定宽高和限制大小
984浏览 • 1回复 待解决
如何图片PixelMap压缩到指定大小
1809浏览 • 1回复 待解决