【HarmonyOS NEXT】photoAsset读取photoAccessHelper.PhotoKeys.SIZE失败

【关键字】

phAccessHelper / 相册管理模块 / getAssets / photoAsset / 14000014

【问题描述】

用phAccessHelper.getAssets读取媒体文件后,再用photoAsset读取媒体信息时失败,报错如下:size is err = 14000014; err message: member not exist,但是我看API参考文档介绍里是可以的。

代码如下:

async readMedia(){
let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates();
// 排序
predicates.orderByDesc(photoAccessHelper.PhotoKeys.DATE_MODIFIED)
let fetchOptions: photoAccessHelper.FetchOptions = {
fetchColumns: [],
predicates: predicates,
};

try {
let fetchResult: photoAccessHelper.FetchResult<photoAccessHelper.PhotoAsset> = await phAccessHelper.getAssets(fetchOptions);
// let photoAsset: photoAccessHelper.PhotoAsset = await fetchResult.getFirstObject();
fetchResult.getAllObjects().then((photoAssets: photoAccessHelper.PhotoAsset[])=>{
photoAssets.forEach((photoAsset: photoAccessHelper.PhotoAsset)=>{
console.info('getAssets photoAsset.uri : ' + photoAsset.uri);
console.info('getAssets displayName = ', photoAsset.displayName);
this.getAssets(photoAsset, photoAccessHelper.PhotoKeys.TITLE)
this.getAssets(photoAsset, photoAccessHelper.PhotoKeys.PHOTO_TYPE)
this.getAssets(photoAsset, photoAccessHelper.PhotoKeys.DISPLAY_NAME)
this.getAssets(photoAsset, photoAccessHelper.PhotoKeys.SIZE)
this.getAssets(photoAsset, photoAccessHelper.PhotoKeys.DURATION)
this.getAssets(photoAsset, photoAccessHelper.PhotoKeys.DATE_ADDED)
this.getAssets(photoAsset, photoAccessHelper.PhotoKeys.DATE_MODIFIED)
this.getAssets(photoAsset, photoAccessHelper.PhotoKeys.DATE_TAKEN)
this.getAssets(photoAsset, photoAccessHelper.PhotoKeys.WIDTH)
this.getAssets(photoAsset, photoAccessHelper.PhotoKeys.HEIGHT)

// 图片
if (photoAsset.photoType === photoAccessHelper.PhotoType.IMAGE){
this.data.push({
itemType: MediaItemType.IMAGE,
urlType: MediaType.LOCAL,
url: photoAsset.uri
})
}else if(photoAsset.photoType === photoAccessHelper.PhotoType.VIDEO){
// 视频
photoAsset.getThumbnail((err, pixelMap) => {
if (err === undefined) {
console.info('getThumbnail successful ' + pixelMap);
this.data.push({
itemType: MediaItemType.VIDEO,
urlType: MediaType.LOCAL,
url: photoAsset.uri,
thumbnailPixMap: pixelMap
})
} else {
console.error(`getThumbnail fail with error: ${err.code}, ${err.message}`);
}
});
}
this.testFetchMetadataFromDataSrc(photoAsset.uri)
})
})

fetchResult.close();
} catch (err) {
console.error('getAssets failed with err: ' + err);
}
}

API介绍如下:

/**
* Returns the value of the specified member.
*
* @param { string } member - Photo asset member. for example : get(PhotoKeys.SIZE)
* @returns { MemberType } Returns the value of the specified photo asset member
* @throws { BusinessError } 401 - if parameter is invalid
* @throws { BusinessError } 13900020 - Invalid argument
* @throws { BusinessError } 14000014 - Member is not a valid PhotoKey
* @syscap SystemCapability.FileManagement.PhotoAccessHelper.Core
* @since 10
*/get(member: string): MemberType;

【解决方案】

原因是getAssets时没有在fetchColumn里面指定key。

参考:

fetchColumns: [photoAccessHelper.PhotoKeys.WIDTH, photoAccessHelper.PhotoKeys.HEIGHT, photoAccessHelper.PhotoKeys.TITLE],

API文档地址:

https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/js-apis-photoaccesshelper-V5#get

  • 3
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值