HarmonyOS 鸿蒙Next中showAssetsCreationDialog异常
HarmonyOS 鸿蒙Next中showAssetsCreationDialog异常 在调用showAssetsCreationDialog时,没有报错,但是不弹保存的框,有配置icon和appaname字段,
执行到 await photoHelper.showAssetsCreationDialog后 不会catch 也不继续往下执行
private async saveVideoToAlbum(localPath: string, context: common.UIAbilityContext): Promise<void> {
const srcFileUri = fileUri.getUriFromPath(localPath);
const fileExt = localPath.includes('.')
? localPath.split('.').pop()?.toLowerCase() ?? 'mp4'
: 'mp4';
const fileName = localPath.split('/').pop() || `video_${Date.now()}`;
const title = fileName.replace(/\.[^.]+$/, '');
const creationConfigs: photoAccessHelper.PhotoCreationConfig[] = [{
title: title,
fileNameExtension: fileExt,
photoType: photoAccessHelper.PhotoType.VIDEO
}];
try{
const photoHelper = photoAccessHelper.getPhotoAccessHelper(context);
const desFileUris = await photoHelper.showAssetsCreationDialog(
[srcFileUri],
creationConfigs
);
const destFile = fs.openSync(desFileUris[0], fs.OpenMode.WRITE_ONLY);
fs.copyFileSync(localPath, destFile.fd);
fs.closeSync(destFile.fd);
photoHelper.release();
KLog.i(TAG, TAG, `save video to album success, uri: ${desFileUris[0]}`);
}catch(error){
KLog.e(TAG, TAG, `save video to album error: ${JSON.stringify(error)}`);
}
}
更多关于HarmonyOS 鸿蒙Next中showAssetsCreationDialog异常的实战教程也可以访问 https://www.itying.com/category-93-b0.html
开发者您好,本地用您的代码未能复现您的问题,以下是本地尝试复现的代码,结果是有弹框的,您可以参考一下:
import { common } from "@kit.AbilityKit";
import { fileUri } from "@kit.CoreFileKit";
import { photoAccessHelper } from "@kit.MediaLibraryKit";
import { fileIo as fs } from '@kit.CoreFileKit'
@Entry
@Component
struct Demo {
@State message: string = 'Hello World';
uiContext = this.getUIContext().getHostContext() as common.UIAbilityContext
private async saveVideoToAlbum(localPath: string, context: common.UIAbilityContext): Promise<void> {
const srcFileUri = fileUri.getUriFromPath(localPath);
const fileExt = localPath.includes('.')
? localPath.split('.').pop()?.toLowerCase() ?? 'mp4'
: 'mp4';
const fileName = localPath.split('/').pop() || `video_${Date.now()}`;
const title = fileName.replace(/\.[^.]+$/, '');
const creationConfigs: photoAccessHelper.PhotoCreationConfig[] = [{
title: title,
fileNameExtension: fileExt,
photoType: photoAccessHelper.PhotoType.VIDEO
}];
try {
const photoHelper = photoAccessHelper.getPhotoAccessHelper(context);
const desFileUris = await photoHelper.showAssetsCreationDialog(
[srcFileUri],
creationConfigs
);
const destFile = fs.openSync(desFileUris[0], fs.OpenMode.WRITE_ONLY);
fs.copyFileSync(localPath, destFile.fd);
fs.closeSync(destFile.fd);
photoHelper.release();
KLog.i(TAG, TAG, `save video to album success, uri: ${desFileUris[0]}`);
} catch (error) {
KLog.e(TAG, TAG, `save video to album error: ${JSON.stringify(error)}`);
}
}
build() {
Column() {
Text(this.message)
.id('DemoHelloWorld')
.fontSize($r('app.float.page_text_font_size'))
.fontWeight(FontWeight.Bold)
.onClick(() => {
let array = this.uiContext.resourceManager.getRawFileContentSync('demo.mp4');
let dirpath = (this.uiContext).tempDir + '/demo.mp4';
let dirFile = fs.openSync(dirpath, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE);
fs.writeSync(dirFile.fd, array.buffer);
fs.closeSync(dirFile);
this.saveVideoToAlbum(dirpath, this.uiContext);
})
Button("直接保存").onClick((event: ClickEvent) => {
let dirpath = (this.uiContext).tempDir + '/demo.mp4';
this.saveVideoToAlbum(dirpath, this.uiContext);
this.message = '直接保存';
})
}
.height('100%')
.width('100%')
}
}
若还是不能解决问题,麻烦您提供一下可复现的完整demo,IDE版本,设备版本和相关日志信息,感谢您的支持和理解。
更多关于HarmonyOS 鸿蒙Next中showAssetsCreationDialog异常的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html
帮顶
好的 已经解决了 在拉起弹窗的config参数的title title内不能包含特殊字符 应该是有的视频名有问题
showAssetsCreationDialog异常通常由以下原因导致:未授予ohos.permission.WRITE_MEDIA或ohos.permission.READ_MEDIA权限;传入的URI格式错误或指向不可写路径;Context类型不匹配(需为AbilityContext或ApplicationContext);或设备存储空间不足。请检查权限声明、URI有效性以及Context参数。
遇到 showAssetsCreationDialog 不弹框、不报错且卡在 await 的情况,最常见的原因是 未在主线程中调用该 API。showAssetsCreationDialog 依赖 UI 线程弹出系统保存界面,如果当前上下文是非 UI 工作线程,API 会一直挂起,既不 resolve 也不 reject。
请检查调用 saveVideoToAlbum 的所在任务是否由 TaskPool 或 Worker 触发。必须将调用切回主线程(例如在 @Entry 组件内通过事件触发),确保 context 来自 UIAbilityContext 且在主线程中传递。
另一种可能:srcFileUri 对应的沙箱文件不存在或路径无效,导致系统无法读取预览,从而不弹出对话框。请先确认 fs.accessSync(localPath) 返回成功后再调用。

