HarmonyOS 需要一个保存用户文件的demo工程

需要知道具体文件保存的流程,文件保存documentViewPicker.save调用后看到documentSaveOptions中没有保存的源数据参数,麻烦提供个demo示例。

HarmonyOS
3天前
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
superinsect

示例代码如下:

import { BusinessError } from '@ohos.base';
import picker from '@ohos.file.picker';

async function example10() {
  try {
    let documentSaveOptions = new picker.DocumentSaveOptions();
    documentSaveOptions.newFileNames = ['DocumentViewPicker01.txt'];
    let documentPicker = new picker.DocumentViewPicker();
    documentPicker.save(documentSaveOptions).then((documentSaveResult: Array<string>) => {
      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));
  }
}

async function example07() {
  try {
    let documentSelectOptions = new picker.DocumentSelectOptions();
    let documentPicker = new picker.DocumentViewPicker();
    documentPicker.select(documentSelectOptions).then((documentSelectResult: Array<string>) => {
      console.info('DocumentViewPicker.select successfully, documentSelectResult uri: ' + JSON.stringify(documentSelectResult));
    }).catch((err: BusinessError) => {
      console.error('DocumentViewPicker.select failed with err: ' + JSON.stringify(err));
    });
  } catch (error) {
    let err: BusinessError = error as BusinessError;
    console.error('DocumentViewPicker failed with err: ' + JSON.stringify(err));
  }
}

@Entry
@Component
struct DocumentViewPickerPage {
  @State message: string = 'Hello World';

  build() {
    Column() {
      Text(this.message)
        .id('HelloWorld')
        .fontSize(50)
        .fontWeight(FontWeight.Bold)
        .alignRules({
          center: { anchor: '__container__', align: VerticalAlign.Center },
          middle: { anchor: '__container__', align: HorizontalAlign.Center }
        })

      Button() {
        Text('Save')
      }
      .type(ButtonType.Capsule)
      .height(40)
      .width(200)
      .margin({top: 20})
      .onClick(() => {
        example10()
      })

      Button() {
        Text('Select')
      }
      .type(ButtonType.Capsule)
      .height(40)
      .width(200)
      .margin({top: 20})
      .onClick(() => {
        example07()
      })
    }
    .height('100%')
    .width('100%')
    .justifyContent(FlexAlign.Center)
  }
}

详细情况和其他示例请参考文档:https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/js-apis-file-picker-V5#documentviewpicker

分享
微博
QQ
微信
回复
3天前
相关问题
需要一个NFC读取demo
697浏览 • 1回复 待解决
HarmonyOS 需要一个筛选页面的demo
389浏览 • 1回复 待解决
HarmonyOS 一个工程推荐有几个UIAbility
95浏览 • 1回复 待解决
HarmonyOS 需要一个上下翻页控件
52浏览 • 1回复 待解决
HarmonyOS 需要定制一个登录模版
34浏览 • 1回复 待解决
HarmonyOS 需要一个图片预览组件
318浏览 • 1回复 待解决