HarmonyOS 鸿蒙Next系统PhotoViewPicker如何获取到图片、视频信息?
HarmonyOS 鸿蒙Next系统PhotoViewPicker如何获取到图片、视频信息?
参考demo,已经获取到uri的话,可以通过uri获取PhotoAsset(getAssets,fetchOptions中predicates指定uri,fetchColumns指定需要信息+getFirstObject),然后用get方法来获取属性,时长duration和大小size都在PhotoKeys里面,封面可以用photoAsset.getThumbnail。
import photoAccessHelper from '[@ohos](/user/ohos).file.photoAccessHelper';
import { BusinessError } from '[@ohos](/user/ohos).base';
import dataSharePredicates from '[@ohos](/user/ohos).data.dataSharePredicates';
import { picker } from '[@kit](/user/kit).CoreFileKit';
import { image } from '[@kit](/user/kit).ImageKit';
[@Entry](/user/Entry)
[@Component](/user/Component)
struct Index { // 定义一个uri数组,用于接收PhotoViewPicker选择图片返回的uri
[@State](/user/State) uris: Array<string> = [];
context = getContext(this); // 调用PhotoViewPicker.select选择图片
[@State](/user/State) pixelMap: image.PixelMap | undefined = undefined
async photoPickerGetUri() {
try {
let PhotoSelectOptions = new picker.PhotoSelectOptions();
PhotoSelectOptions.MIMEType = picker.PhotoViewMIMETypes.IMAGE_TYPE;
PhotoSelectOptions.maxSelectNumber = 1;
let photoViewPicker = new picker.PhotoViewPicker();
photoViewPicker.select().then(async (PhotoSelectResult: picker.PhotoSelectResult) => {
console.info('PhotoViewPicker.select successfully, PhotoSelectResult uri: ' +
JSON.stringify(PhotoSelectResult));
this.uris = PhotoSelectResult.photoUris;
this.uriGetAssets()
}).catch((err: BusinessError) => {
console.error('PhotoViewPicker.select failed with err: ' + JSON.stringify(err));
});
} catch (error) {
let err: BusinessError = error as BusinessError;
console.error('PhotoViewPicker failed with err: ' + JSON.stringify(err));
}
}
async uriGetAssets() {
try {
let phAccessHelper = photoAccessHelper.getPhotoAccessHelper(this.context);
let predicates: dataSharePredicates.DataSharePredicates =
new dataSharePredicates.DataSharePredicates(); // 配置查询条件,使用PhotoViewPicker选择图片返回的uri进行查询
predicates.equalTo('uri', this.uris[0]);
let fetchOption: photoAccessHelper.FetchOptions = {
fetchColumns: [photoAccessHelper.PhotoKeys.WIDTH, photoAccessHelper.PhotoKeys.HEIGHT,
photoAccessHelper.PhotoKeys.TITLE, photoAccessHelper.PhotoKeys.SIZE, photoAccessHelper.PhotoKeys.DURATION],
predicates: predicates
};
let fetchResult: photoAccessHelper.FetchResult<photoAccessHelper.PhotoAsset> =
await phAccessHelper.getAssets(fetchOption); // 得到uri对应的PhotoAsset对象,读取文件的部分信息
const asset: photoAccessHelper.PhotoAsset = await fetchResult.getFirstObject();
console.info('asset displayName: ', asset.displayName);
console.info('asset uri: ', asset.uri);
console.info('asset photoType: ', asset.photoType);
console.info('asset width: ', asset.get(photoAccessHelper.PhotoKeys.WIDTH));
console.info('asset height: ', asset.get(photoAccessHelper.PhotoKeys.HEIGHT));
console.info('asset SIZE: ' + asset.get(photoAccessHelper.PhotoKeys.SIZE));
console.info('asset DURATION: ' +
asset.get(photoAccessHelper.PhotoKeys.DURATION)); // 获取缩略图
asset.getThumbnail((err, pixelMap) => {
if (err == undefined) {
this.pixelMap =pixelMap
console.info('getThumbnail successful ' + JSON.stringify(pixelMap));
} else {
console.error('getThumbnail fail', err);
}
});
} catch (error) {
console.error('uriGetAssets failed with err: ' + JSON.stringify(error));
}
}
build() {
Column(){
Image(this.pixelMap)
.width(200)
.height(200)
Button('获取文件大小').width(100).height(100).onClick(() => {
this.photoPickerGetUri()
})
}
}
}
更多关于HarmonyOS 鸿蒙Next系统PhotoViewPicker如何获取到图片、视频信息?的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html
在HarmonyOS鸿蒙Next系统中,PhotoViewPicker组件用于选择图片和视频。要获取到选中的图片和视频信息,可以通过组件的回调接口来实现。
具体来说,你可以为PhotoViewPicker组件设置选择结果监听器(例如,通过setPhotoViewPickerListener
方法,假设此方法存在,实际API可能有所不同)。在选择完成后,监听器的回调方法会被触发,其中会包含选择结果的列表。
这个列表通常包含了所选文件的路径或URI信息。你可以遍历这个列表,根据路径或URI访问文件系统,以获取每张图片或视频的具体信息,如文件大小、分辨率、时长等。
示例代码(伪代码):
photoViewPicker.setPhotoViewPickerListener(new PhotoViewPickerListener() {
@Override
public void onSelected(List<String> selectedFiles) {
for (String file : selectedFiles) {
// file 是选中文件的路径或URI
// 通过文件路径访问文件系统,获取图片或视频信息
}
}
});
注意:实际开发中,API名称和调用方式可能有所不同,请查阅最新的HarmonyOS开发文档以获取准确信息。
如果问题依旧没法解决请联系官网客服,官网地址是:https://www.itying.com/category-93-b0.html