HarmonyOS 鸿蒙Next OpenHarmony-SIG/fluttertpc_video_thumbnail 获取本地视频的 cover,返回异常
HarmonyOS 鸿蒙Next OpenHarmony-SIG/fluttertpc_video_thumbnail 获取本地视频的 cover,返回异常 三方库版本:https://gitee.com/openharmony-sig/fluttertpc_video_thumbnail.git 最新提交
相册选择视频或者相机拍摄视频,获取到视频文件的 path,使用 File 操作复制文件到DocumentDirectory目录,之后使用fluttertpc_video_thumbnail这个三方库读取这个文件的封面,但是什么都没返回,查看DevEco 的 log 也没有任何输出
int nowTime = DateTime.now().millisecondsSinceEpoch;
File videoFile = File(path);
String fileExtension = p.extension(path);
debugPrint("fileExtension:$fileExtension");
String targetVideoPath = "${Constant.ohosCacheDirectory}/${nowTime}_hotspot_video_temp_original$fileExtension";
String targetVideoPathThumbnail = "${Constant.ohosDocumentDirectory}/${nowTime}_hotspot_video_thumbnail_temp_original.png";
// 复制图片到到持久化文件夹下
videoFile.copy(targetVideoPath);
debugPrint("添加的热点视频复制到持久化目录: targetImagePath:$targetVideoPath");
int timestampMillis = DateTime.now().millisecondsSinceEpoch;
final String? videoCoverPath = await VideoThumbnail.thumbnailFile(
video: targetVideoPath,
thumbnailPath: targetVideoPathThumbnail,
imageFormat: ImageFormat.PNG,
maxHeight: 100,
maxWidth: 100,
);
//说明,执行这个VideoThumbnail.thumbnailFile异步方法之后,下面代码就不跑了,没有任何输出
if (videoCoverPath == null) {
if (mounted) {
CommonUtils.showHUDDialog(ShowHUDDialogType.error, "获取视频封面失败", context);
}
return;
}
debugPrint("general videoCoverPath is:$videoCoverPath");
更多关于HarmonyOS 鸿蒙Next OpenHarmony-SIG/fluttertpc_video_thumbnail 获取本地视频的 cover,返回异常的实战教程也可以访问 https://www.itying.com/category-92-b0.html
import camera from '@ohos.multimedia.camera';
import common from '@ohos.app.ability.common';
import { BusinessError } from '@ohos.base';
import fileuri from '@ohos.file.fileuri';
import fs from '@ohos.file.fs';
import photoAccessHelper from '@ohos.file.photoAccessHelper';
import { picker } from '@kit.CoreFileKit';
import { dataSharePredicates } from '@kit.ArkData';
let context = getContext(this) as common.Context;
class CameraPosition {
cameraPosition: camera.CameraPosition
saveUri: string
constructor(cameraPosition: camera.CameraPosition, saveUri: string) {
this.cameraPosition = cameraPosition
this.saveUri = saveUri
}
}
let pathDir = getContext().filesDir;
//沙箱路径地址
console.log('保存路径为' + pathDir)
let filePath = pathDir + '/test.mp4'
fs.createRandomAccessFileSync(filePath, fs.OpenMode.CREATE);
let uri = fileuri.getUriFromPath(filePath);
async function picture() {
let PhotoSelectOptions = new photoAccessHelper.PhotoSelectOptions();
PhotoSelectOptions.MIMEType = photoAccessHelper.PhotoViewMIMETypes.IMAGE_TYPE;
PhotoSelectOptions.maxSelectNumber = 1;
let photoPicker = new photoAccessHelper.PhotoViewPicker();
photoPicker.select().then((PhotoSelectResult: photoAccessHelper.PhotoSelectResult) => {
let photouri: Array<string> = PhotoSelectResult.photoUris
let file = fs.openSync(photouri[0], fs.OpenMode.READ_ONLY)
let file2 = fs.openSync(pathDir+'/test.mp4', fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE)
fs.copyFileSync(file.fd, file2.fd)
fs.closeSync(file);
fs.closeSync(file2);
})
}
@Entry
@Component
export struct Index {
@State message: string = 'Hello World';
private appContext: common.Context = getContext(this);
saveFile(){
try {
let documentSaveOptions = new picker.DocumentSaveOptions();
documentSaveOptions.newFileNames = ['test.mp4'];
let documentPicker = new picker.DocumentViewPicker();
documentPicker.save(documentSaveOptions).then((documentSaveResult: Array<string>) => {
let uri = documentSaveResult[0];
let sanFile = fs.openSync(filePath, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE)
let pubFile = fs.openSync(uri, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE)
fs.copyFileSync(sanFile.fd, pubFile.fd)
fs.close(sanFile)
fs.close(pubFile)
console.info('DocumentViewPicker.save successfully, documentSaveResult uri: ' + JSON.stringify(documentSaveResult));
}).catch((err: BusinessError) => {
console.error('DocumentViewPicker.save failed with err: ' + JSON.stringify(err));
});
} catch (error) {
let err: BusinessError = error as BusinessError;
console.error('DocumentViewPicker failed with err: ' + JSON.stringify(err));
}
}
build() {
Column() {
Button('选择并保存').onClick(() => {
picture()
})
Button('保存documents')
.fontSize(50)
.fontWeight(FontWeight.Bold)
.onClick(()=>{
this.saveFile();
})
}
}
}
更多关于HarmonyOS 鸿蒙Next OpenHarmony-SIG/fluttertpc_video_thumbnail 获取本地视频的 cover,返回异常的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
针对帖子标题中提到的问题“HarmonyOS 鸿蒙Next OpenHarmony-SIG/fluttertpc_video_thumbnail 获取本地视频的 cover,返回异常”,以下是根据要求提供的专业回答:
在HarmonyOS鸿蒙系统中,若使用OpenHarmony的SIG(Special Interest Group)组件fluttertpc_video_thumbnail时遇到获取本地视频封面返回异常的情况,可能的原因及解决方案包括:
-
权限问题:确保应用已正确申请并获取了访问本地存储的权限。检查AndroidManifest.xml或config.json中的权限声明。
-
路径问题:验证视频文件的路径是否正确,确保文件存在且应用有权限访问该路径。
-
组件兼容性:检查fluttertpc_video_thumbnail组件是否与当前使用的HarmonyOS版本兼容。可查阅相关文档或更新组件至最新版本。
-
异常处理:在代码中添加异常捕获逻辑,打印或记录异常信息,以便更准确地定位问题。
-
资源限制:检查系统资源是否充足,如内存、存储空间等,避免因资源不足导致处理失败。
如果问题依旧没法解决请联系官网客服,官网地址是:https://www.itying.com/category-93-b0.html,