文档开发者中心
我的
|

ArkUI (方舟UI框架)

UI

OpenHarmony

HarmonyOS 5

HarmonyOS 文件遍历筛选(API12+)

要在整个手机里面查询文件(如pdf,xlsx,docx,video),然后显示出来方便用户选择上传。有没有直接获取所有文件然后分类筛选的api?

点赞
收藏
回复
1
分享
举报
浏览98 发布于2025-08-14 12:01未知归属地
全部评论
最多点赞
最新发布
最早发布
|
技术支持

DocumentViewPicker与PhotoViewPicker只能从对应文件夹选取文件,不能一次打开所有类型文件,可以使用tab组件+picker实现类似效果,参考demo:

收起
自动换行
深色代码主题
复制
@Entry
@Component
struct Index {
  @State currentIndex: number = 0
  private controller: TabsController = new TabsController()

  @Builder tabBuilder(index: number, name: string) {
    Column() {
      Text(name)
        .fontColor(this.currentIndex === index ? '#007DFF' : '#182431')
        .fontSize(16)
        .fontWeight(this.currentIndex === index ? 500 : 400)
        .lineHeight(22)
        .margin({ top: 17, bottom: 7 })
      Divider()
        .strokeWidth(2)
        .color('#007DFF')
        .opacity(this.currentIndex === index ? 1 : 0)
    }.width('100%')
  }

  build() {
    Column() {
      Tabs({ barPosition: BarPosition.Start, index: this.currentIndex, controller: this.controller }) {
        TabContent() {
          Column(){
            Text("你需要从手机中选择文件").fontSize(20).fontWeight(FontWeight.Bold)
            Button('DocumentViewPicker').width('80%').height(40).margin(20)
              .onClick(() => {DocumentViewPicker()})
          }.width('100%').height('100%')
        }.tabBar(this.tabBuilder(0, '手机文件'))

        TabContent() {
          Column(){
            Button('DocumentViewPicker').width('80%').height(40).margin(20)
              .onClick(() => {PhotoViewPicker()})
          }.width('100%').height('100%')
        }.tabBar(this.tabBuilder(1, '手机相册'))

        TabContent() {
          Column().width('100%').height('100%')
        }.tabBar(this.tabBuilder(2, '...'))

        TabContent() {
          Column().width('100%').height('100%')
        }.tabBar(this.tabBuilder(3, '...'))
      }
      .vertical(false)
      .barMode(BarMode.Fixed)
      .barWidth(360)
      .barHeight(56)
      .animationDuration(400)
      .onChange((index: number) => {
        this.currentIndex = index
      })
      .width(360)
      .height(296)
      .margin({ top: 52 })
      .backgroundColor('#F1F3F5')
    }.width('100%')
  }
}

async function DocumentViewPicker() {
  let DocumentSelectOptions = new picker.DocumentSelectOptions();
  let documentPicker = new picker.DocumentViewPicker();
  documentPicker.select(DocumentSelectOptions)
}

async function PhotoViewPicker() {
  let PhotoSelectOptions = new picker.PhotoSelectOptions();
  PhotoSelectOptions.MIMEType = picker.PhotoViewMIMETypes.IMAGE_TYPE;
  PhotoSelectOptions.maxSelectNumber = 5;
  let photoPicker = new picker.PhotoViewPicker();
  photoPicker.select(PhotoSelectOptions)
}
展开全部
1楼回复于2025-08-14 12:01 未知归属地
写回答
新增插入模板功能
一键使用模板,快速填写内容,轻松发帖~
知道了
  • 为了保障您的信息安全,请勿上传您的敏感个人信息(如您的密码等信息)和您的敏感资产信息(如关键源代码、签名私钥、调试安装包、业务日志等信息),且您需自行承担由此产生的信息泄露等安全风险。
  • 如您发布的内容为转载内容,请注明内容来源。

我要提问题

了解社区公约,与您携手共创和谐专业的开发者社区。