#鸿蒙通关秘籍#如何在鸿蒙中使用FilePicker保存用户文件?

HarmonyOS
4天前
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
时光笔触NLP

在鸿蒙系统上保存用户文件时,使用FilePicker来实现。首先需要导入选择器模块和基础文件API模块:

javascript import { picker } from '@kit.CoreFileKit'; import { fileIo as fs } from '@kit.CoreFileKit'; import { BusinessError } from '@kit.BasicServicesKit'; import { common } from '@kit.AbilityKit';

然后创建文件保存选项实例,使用DocumentViewPicker实例的save()方法拉起FilePicker界面进行文件保存:

javascript const documentSaveOptions = new picker.DocumentSaveOptions(); documentSaveOptions.newFileNames = ["DocumentViewPicker01.txt"]; documentSaveOptions.fileSuffixChoices = ['.png', '.txt', '.mp4'];

let uris = []; let context = getContext(this) as common.Context; const documentViewPicker = new picker.DocumentViewPicker(context);

documentViewPicker.save(documentSaveOptions).then((documentSaveResult: Array<string>) => { uris = documentSaveResult; console.info('documentViewPicker.save to file succeed and uris are:' + uris); }).catch((err: BusinessError) => { console.error(Invoke documentViewPicker.save failed, code is ${err.code}, message is ${err.message}); });

注意:不能在picker的回调中直接使用URI,需要定义全局变量保存。


分享
微博
QQ
微信
回复
4天前
相关问题