请设置下DocumentPickerMode模式为下载模式。保存在文件管理器中需要用户授权,如果不希望拉起文件管理可以将DocumentPickerMode配置为下载模式。参考https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/js-apis-file-picker-V5\#documentsaveoptions保存参考https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/js-apis-file-picker-V5\#save-3请参考下面,不拉起文管保存文件import { photoAccessHelper } from '@kit.MediaLibraryKit'; import { fileIo, fileUri as fileuri } from '@kit.CoreFileKit'; import fs from '@ohos.file.fs'; import { BusinessError } from '@ohos.base'; @Entry @Component struct testFilePickerPage { build() { Row() { Column() { Image($r('app.media.startIcon')) // 要保存的图片 .height(280) .width(280) .padding(40) Button('保存图片到指定目录') .fontSize(12) .onClick(async () => { let uri = 'file://docs/storage/Users/currentUser' + '/outputImg123'; // 图片存储路径 let fileUri: fileuri.FileUri = new fileuri.FileUri(uri); let path = fileUri.path; fs.mkdir(path); getContext().resourceManager.getMediaContent($r('app.media.startIcon').id, 120, (error: BusinessError, value: Uint8Array) => { if (error != null) { console.error(`callback getMediaContent failed, error code: ${error.code}, message: ${error.message}.`); } else { let mediaBuffer = value.buffer; path = path + '/11111.png'; let file = fs.openSync(path, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE); let writeLen = fs.writeSync(file.fd, mediaBuffer); console.info("write data to file succeed and size is:" + writeLen); fs.closeSync(file); console.info('DocumentViewPicker.save successfully, DocumentSaveResult uri: '); } }); }).margin({ left: 15 }) Text().width(20) SaveButton() .onClick(async (event: ClickEvent, result: SaveButtonOnClickResult) => { if (result === SaveButtonOnClickResult.SUCCESS) { try { const context = getContext(this); let helper = photoAccessHelper.getPhotoAccessHelper(context); // onClick触发后5秒内通过createAsset接口创建图片文件,5秒后createAsset权限收回。 let uri = await helper.createAsset(photoAccessHelper.PhotoType.IMAGE, 'jpg'); // 使用uri打开文件,可以持续写入内容,写入过程不受时间限制 let file = await fileIo.open(uri, fileIo.OpenMode.READ_WRITE | fileIo.OpenMode.CREATE); try { context.resourceManager.getMediaContent($r('app.media.startIcon').id, 0) .then(async value => { let media = value.buffer; // 写到媒体库文件中 await fileIo.write(file.fd, media); await fileIo.close(file.fd); AlertDialog.show({ message: '已保存至相册!' }); }); } catch (err) { console.error('error is ' + JSON.stringify(err)) } } catch (error) { console.error('error is ' + JSON.stringify(error)); } } else { AlertDialog.show({ message: '设置权限失败' }) } }) } .width('100%') } .height('100%') } }
请设置下DocumentPickerMode模式为下载模式。
保存在文件管理器中需要用户授权,如果不希望拉起文件管理可以将DocumentPickerMode配置为下载模式。参考
https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/js-apis-file-picker-V5\#documentsaveoptions
保存参考
https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/js-apis-file-picker-V5\#save-3
请参考下面,不拉起文管保存文件