HarmonyOS鸿蒙Next中DocumentViewPicker拉起文档选择器只显示最近30天
HarmonyOS鸿蒙Next中DocumentViewPicker拉起文档选择器只显示最近30天 DocumentViewPicker 拉起文档选择器只显示最近30天,想直接看手机上所有文件应该怎么写,或者按照excel,word,ppt 等分类应该怎么弄;
let documentSelectOptions = new picker.DocumentSelectOptions();
if (fileSuffixFilters.length > 0) {
documentSelectOptions.fileSuffixFilters = fileSuffixFilters;
}
documentSelectOptions.maxSelectNumber = maxSelectNumber;
documentSelectOptions.selectMode = picker.DocumentSelectMode.FILE;
const documentPicker = new picker.DocumentViewPicker(this.context);
const result = await documentPicker.select(documentSelectOptions);
更多关于HarmonyOS鸿蒙Next中DocumentViewPicker拉起文档选择器只显示最近30天的实战教程也可以访问 https://www.itying.com/category-93-b0.html
- DocumentViewPicker对接的选择资源来自于FilePicker,负责文件类型的资源管理,文件类型不区分后缀,比如浏览器下载的图片、文档等,都属于文件类型。
- 文件选择参数DocumentSelectOptions可以配置文档选择选项,其中fileSuffixFilters支持配置选择文件的后缀类型,defaultFilePathUri支持配置指定选择的文件或者目录路径。常见的defaultFilePathUri配置路径说明如下:
| defaultFilePathUri | 说明 |
|---|---|
| 默认为空 | 效果为拉起最近打开页。 |
| file://docs/storage/Users/currentUser | 效果为拉起我的手机页。 |
| file://docs/storage/Users/currentUser/Documents | 效果为拉起Documents文件夹。 |
| file://docs/storage/Users/currentUser/Download | 效果为拉起Download文件夹。 |
借助DocumentViewPicker的能力,可以实现获取指定后缀的文件。参考代码如下:
import picker from '@ohos.file.picker';
import { BusinessError } from '@kit.BasicServicesKit';
@Entry
@Component
struct Index {
DocSel() {
let docSel = new picker.DocumentSelectOptions();
let docPic = new picker.DocumentViewPicker();
let uris: Array<string> = [];
// 可选,根据需要选择配置docSel.defaultFilePathUri ='file://docs/storage/Users/currentUser';
docSel.selectMode = picker.DocumentSelectMode.FILE;
docSel.fileSuffixFilters = [
'Word|.doc,.docx',
'Excel|.xls,.xlsx',
'PPT|.ppt,.pptx,.pdf'
];
docPic.select(docSel).then((documentSelectResult: Array<string>) => {
// 文件选择成功后,返回被选中文档的URI结果集。
uris = documentSelectResult;
console.info('documentViewPicker.select to file succeed and uris are:', uris);
// 后续业务逻辑
}).catch((err: BusinessError) => {
console.error(`DocumentViewPicker.select failed with err: code is ${err.code}, message is ${err.message}`);
});
}
build() {
Column() {
Row() {
Button('选择指定后缀文件')
.fontSize(20)
.fontWeight(FontWeight.Bold)
.onClick(() => {
this.DocSel();
})
}
.height('20%')
}
.height('100%')
.width('100%')
}
}
注意限制条件:
本示例支持API Version 20 Release及以上版本。
本示例支持HarmonyOS 6.0.0 Release SDK及以上版本。
本示例需要使用DevEco Studio 6.0.0 Release及以上版本进行编译运行。
更多关于HarmonyOS鸿蒙Next中DocumentViewPicker拉起文档选择器只显示最近30天的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html
DocumentViewPicker 只显示“最近30天”,应该不是接口把文件限制成 30 天了,而是因为没有传 defaultFilePathUri,系统默认先打开“最近访问”页。 如果想直接看手机上的某个目录文件,需要给选择器传起始目录 URI。 如果想按 Word、Excel、PPT 分类,不是用 mergeMode,而是用 fileSuffixFilters 按后缀过滤。 另外,mergeMode 只能做文档、图片、音频、视频这种大类聚合,不能细分到 word、excel、ppt
示例代码:
import { common } from '@kit.AbilityKit'
import { picker } from '@kit.CoreFileKit'
@Entry
@Component
struct FilePickerPage {
@State resultText: string = '暂无选择结果'
// 这里替换成你实际要打开的目录 URI。
// 不传时,Picker 默认会先进入“最近访问”页。
private startDirUri: string = '这里替换成真实目录URI'
private async pickAllFiles() {
try {
const context = this.getUIContext().getHostContext() as common.UIAbilityContext
const documentPicker = new picker.DocumentViewPicker(context)
const result = await documentPicker.select({
maxSelectNumber: 10,
selectMode: picker.DocumentSelectMode.FILE,
defaultFilePathUri: this.startDirUri
})
this.resultText = JSON.stringify(result)
} catch (err) {
this.resultText = '选择失败'
}
}
private async pickOfficeFiles() {
try {
const context = this.getUIContext().getHostContext() as common.UIAbilityContext
const documentPicker = new picker.DocumentViewPicker(context)
const result = await documentPicker.select({
maxSelectNumber: 10,
selectMode: picker.DocumentSelectMode.FILE,
defaultFilePathUri: this.startDirUri,
fileSuffixFilters: [
'Word|.doc,.docx',
'Excel|.xls,.xlsx',
'PPT|.ppt,.pptx'
]
})
this.resultText = JSON.stringify(result)
} catch (err) {
this.resultText = '选择失败'
}
}
private async pickDocumentCategory() {
try {
const context = this.getUIContext().getHostContext() as common.UIAbilityContext
const documentPicker = new picker.DocumentViewPicker(context)
const result = await documentPicker.select({
mergeMode: picker.MergeTypeMode.DOCUMENT
})
this.resultText = JSON.stringify(result)
} catch (err) {
this.resultText = '选择失败'
}
}
build() {
Column({ space: 12 }) {
Button('查看某目录下所有文件')
.width('90%')
.onClick(() => {
this.pickAllFiles()
})
Button('按 Word / Excel / PPT 筛选')
.width('90%')
.onClick(() => {
this.pickOfficeFiles()
})
Button('打开系统文档大类视图')
.width('90%')
.onClick(() => {
this.pickDocumentCategory()
})
Text(this.resultText)
.width('90%')
.fontSize(14)
}
.width('100%')
.padding(16)
}
}
DocumentViewPicker 拉起的是系统文件选择器,它的展示方式(比如默认只显示“最近30天”)是系统文件管理器的 UI 策略。 API17 起可尝试“所有文件(.)|.*”这种通配过滤。select 返回的是 URI,默认只有临时读权限;如果后续还要长期读取,记得做持久化授权。若你的目标是指定目录浏览,可结合 defaultFilePathUri/授权能力,但最终界面仍以系统 Picker 支持为准
在API12 想要显示全部文件或者分类显示excel/word/ppt/图片,这些应该怎么实现
DocumentViewPicker 拉起的是系统 FilePicker,能展示哪些入口/最近文件主要由系统文件管理器决定,应用侧不能强制把 UI 切成“所有文件”视图。你能控制的主要是筛选条件:不设置 fileSuffixFilters 就是不按后缀过滤;如果要按类型分组,可配置类似 Excel|.xls,.xlsx、Word|.doc,.docx、PPT|.ppt,.pptx、PDF|.pdf 这样的后缀过滤。
若只想显示全部文件,API17 起可尝试“所有文件(.)|.*”这种通配过滤。select 返回的是 URI,默认只有临时读权限;如果后续还要长期读取,记得做持久化授权。若你的目标是指定目录浏览,可结合 defaultFilePathUri/授权能力,但最终界面仍以系统 Picker 支持为准。
DocumentViewPicker 默认按修改时间排序并限定显示最近30天的文件,这是系统文件管理器的内置过滤策略。若需调整日期范围,需在创建 picker 时通过 filter 属性或 DatePicker 相关参数自定义过滤条件。该行为与 Java 或 C 语言无关。
HarmonyOS Next 的 DocumentViewPicker 顶部带有“最近”和“浏览”两个标签页,默认显示“最近”只列出近 30 天的文件;点击“浏览”即可查看设备上全部文档,无需额外代码。若要按类型筛选(如 Excel、Word、PPT),只需在 DocumentSelectOptions 中设置 fileSuffixFilters 为对应后缀数组,例如 ['xls','xlsx','doc','docx','ppt','pptx'],选择器便会仅展示这些格式的文件。


