HarmonyOS鸿蒙Next中文件管理器选择视频读取缩略图
HarmonyOS鸿蒙Next中文件管理器选择视频读取缩略图
// 从文件管理器选择视频
getFileFromFileManager(){
try {
let DocumentSelectOptions = new picker.DocumentSelectOptions();
let documentPicker = new picker.DocumentViewPicker();
documentPicker.select(DocumentSelectOptions).then((DocumentSelectResult) => {
if (DocumentSelectResult !== null && DocumentSelectResult !== undefined) {
DocumentSelectResult.forEach(async (value) => {
let videoPic = await VideoUtils.getThumbnailByUrl(value);
console.info(`getFileFromFileManager: `,JSON.stringify(videoPic));
})
}
})
} catch (error) {
let err: BusinessError = error as BusinessError;
console.error('getFileFromFileManager failed with err: ' + JSON.stringify(err));
}
}
//获取缩略图,返回PixelMap格式
static async getThumbnailByUrl(videoUrl: string): Promise<PixelMap|null> {
try {
videoUrl = VideoUtils.getFileInfo(videoUrl)
// Obtain video resources.
let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates();
predicates.equalTo('uri', videoUrl);
let videoFetchResult: photoAccessHelper.FetchResult<photoAccessHelper.PhotoAsset> =
await VideoUtils.phAccessHelper.getAssets({
fetchColumns: ['width', 'height', 'orientation','title','duration'],
predicates: predicates
});
let photoAsset: photoAccessHelper.PhotoAsset = await videoFetchResult.getFirstObject();
// Configure thumbnail parameters.
let thumbnailSize: Size = { width: 0, height: 0 };
if (photoAsset.get(photoAccessHelper.PhotoKeys.ORIENTATION) === 90 ||
photoAsset.get(photoAccessHelper.PhotoKeys.ORIENTATION) === 270) {
thumbnailSize.width = photoAsset.get(photoAccessHelper.PhotoKeys.HEIGHT) as number;
thumbnailSize.height = photoAsset.get(photoAccessHelper.PhotoKeys.WIDTH) as number;
} else {
thumbnailSize.width = photoAsset.get(photoAccessHelper.PhotoKeys.WIDTH) as number;
thumbnailSize.height = photoAsset.get(photoAccessHelper.PhotoKeys.HEIGHT) as number;
}
console.info('getThumbnailByUrl','to here')
let thumbpic= await photoAsset.getThumbnail(thumbnailSize)//缩略图
return thumbpic;
} catch (error) {
hilog.error(0x0000, TAG, `getThumbnailByUrl failed, error: ${JSON.stringify(error)}`);
return null;
}
}
然后报错 FetchResultNapi:{GetNapiResFromAsset:492} Failed to get file asset napi object getThumbnailByUrl failed, error: {“code”:“14000011”} 查找一圈发现都是图库选择视频可以获取到缩略图,文件管理器获取缩略图的没有说到,看到有需要申请权限’ohos.permission.READ_IMAGEVIDEO’的,请问有没有遇到这样的问题
更多关于HarmonyOS鸿蒙Next中文件管理器选择视频读取缩略图的实战教程也可以访问 https://www.itying.com/category-93-b0.html
更多关于HarmonyOS鸿蒙Next中文件管理器选择视频读取缩略图的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html
在HarmonyOS鸿蒙Next中,文件管理器通过系统提供的媒体库API读取视频文件的缩略图。开发者可以使用MediaLibrary
模块中的getThumbnail
方法获取视频缩略图。具体流程包括查询视频文件、调用getThumbnail
方法生成缩略图,并在UI中展示。此过程不涉及Java或C语言,完全基于鸿蒙的API实现。
在HarmonyOS Next中,从文件管理器选择视频获取缩略图确实需要注意几个关键点:
-
权限问题:必须确保应用已申请ohos.permission.READ_IMAGEVIDEO权限,并在manifest.json中声明。同时需要动态请求用户授权。
-
URI转换问题:文件管理器返回的URI格式可能与图库不同,需要先转换为PhotoAsset能识别的格式。建议使用@ohos.file.fs的getFileInfo()方法处理URI。
-
代码修正建议:
- 在getThumbnailByUrl方法中,确保videoUrl是有效的媒体库URI
- 检查photoAccessHelper是否已正确初始化
- 添加对photoAsset为空的判断处理
- 替代方案:如果仍无法解决,可以考虑使用@ohos.multimedia.mediaLibrary的getThumbnail方法,或通过MediaPlayer获取视频帧作为缩略图。
错误码14000011通常表示权限不足或资源访问失败,建议先重点检查权限配置和URI有效性。