#鸿蒙通关秘籍#在鸿蒙系统中如何处理媒体文件URI?

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

媒体文件URI包括图片、视频、和音频文件。在鸿蒙系统中,可以通过photoAccessHelper模块来处理这些URI。

bash import { picker } from '@kit.CoreFileKit'; import { photoAccessHelper } from '@kit.MediaLibraryKit'; import { BusinessError } from '@kit.BasicServicesKit'; import { dataSharePredicates } from '@kit.ArkData';

let uris: Array<string> = []; const context = getContext(this);

async function photoPickerGetUri() { try { let PhotoSelectOptions = new picker.PhotoSelectOptions(); PhotoSelectOptions.MIMEType = picker.PhotoViewMIMETypes.IMAGE_TYPE; PhotoSelectOptions.maxSelectNumber = 1; let photoPicker = new picker.PhotoViewPicker(); photoPicker.select(PhotoSelectOptions).then((PhotoSelectResult: picker.PhotoSelectResult) => { uris = PhotoSelectResult.photoUris; }).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 function uriGetAssets() { try { let phAccessHelper = photoAccessHelper.getPhotoAccessHelper(context); let predicates = new dataSharePredicates.DataSharePredicates(); predicates.equalTo('uri', uris[0]); let fetchOption = { fetchColumns: [photoAccessHelper.PhotoKeys.WIDTH, photoAccessHelper.PhotoKeys.HEIGHT, photoAccessHelper.PhotoKeys.TITLE, photoAccessHelper.PhotoKeys.DURATION], predicates: predicates }; let fetchResult = await phAccessHelper.getAssets(fetchOption); const asset = await fetchResult.getFirstObject(); console.info('Asset information:', asset); } catch (error) { console.error('uriGetAssets failed with err: ' + JSON.stringify(error)); } }

分享
微博
QQ
微信
回复
1天前
相关问题
在读取媒体文件open: permission denied
2861浏览 • 1回复 待解决