选择器(Picker)是一个封装PhotoViewPicker、DocumentViewPicker、AudioViewPicker等API模块,具有选择与保存的能力。应用可以自行选择使用哪种API实现文件选择和文件保存的功能。该类接口,需要应用在界面UIAbility中调用,否则无法拉起photoPicker应用或FilePicker应用。
导入模块
import { picker } from '@kit.CoreFileKit';
- 1
DocumentViewPicker
文件选择器对象,用来支撑选择和保存各种格式文档。在使用前,需要先创建DocumentViewPicker实例。
系统能力:SystemCapability.FileManagement.UserFileService
constructor12+
constructor(context: Context)
元服务API: 从API version 12开始,该接口支持在元服务中使用。
系统能力:SystemCapability.FileManagement.UserFileService
创建DocumentViewPicker对象,推荐使用该构造函数,获取context参考[getContext]
示例:
import { common } from '@kit.AbilityKit';
import { picker } from '@kit.CoreFileKit';
@Entry
@Component
struct Index {
@State message: string = 'hello World';
build() {
Row() {
Column() {
Text(this.message)
.fontSize(50)
.fontWeight(FontWeight.Bold)
.onClick(()=>{
let context = getContext(this) as common.Context; // 请确保getContext(this)返回结果为UIAbilityContext
let documentPicker = new picker.DocumentViewPicker(context);
})
}
.width('100%')
}
.height('100%')
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
constructor12+
constructor()
元服务API: 从API version 12开始,该接口支持在元服务中使用。
系统能力:SystemCapability.FileManagement.UserFileService
创建DocumentViewPicker对象,不推荐使用该构造函数,会出现概率性失败问题
示例:
let documentPicker = new picker.DocumentViewPicker(); // 不推荐使用无参构造,会出现概率性拉起失败问题
- 1
select
select(option?: DocumentSelectOptions): Promise<Array>
通过选择模式拉起documentPicker界面,用户可以选择一个或多个文件。接口采用Promise异步返回形式,传入可选参数DocumentSelectOptions对象,返回选择文件的uri数组。
注意:此接口返回的uri数组的具体使用方式参见用户文件uri介绍中的[文档类uri的使用方式]。
元服务API: 从API version 12开始,该接口支持在元服务中使用。
系统能力:SystemCapability.FileManagement.UserFileService
参数:
参数名 | 类型 | 必填 | 说明 |
---|---|---|---|
option | [DocumentSelectOptions] | 否 | documentPicker选择选项,若无此参数,则默认拉起documentPicker主界面 |
返回值:
类型 | 说明 |
---|---|
Promise |
Promise对象。返回documentPicker选择后的结果集 |
示例:
import { BusinessError } from '@kit.BasicServicesKit';
import { common } from '@kit.AbilityKit';
import { picker } from '@kit.CoreFileKit';
async function example07(context: common.Context) { // 需确保 context 由 UIAbilityContext 转换而来
try {
let documentSelectOptions = new picker.DocumentSelectOptions();
let documentPicker = new picker.DocumentViewPicker(context);
documentPicker.select(documentSelectOptions).then((documentSelectResult: Array) => {
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));
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
select
select(option: DocumentSelectOptions, callback: AsyncCallback
通过选择模式拉起documentPicker界面,用户可以选择一个或多个文件。接口采用callback异步返回形式,传入参数DocumentSelectOptions对象,返回选择文件的uri数组。
注意:此接口返回的uri数组的具体使用方式参见用户文件uri介绍中的[文档类uri的使用方式]。
元服务API: 从API version 12开始,该接口支持在元服务中使用。
系统能力:SystemCapability.FileManagement.UserFileService
参数:
参数名 | 类型 | 必填 | 说明 |
---|---|---|---|
option | [DocumentSelectOptions] | 是 | documentPicker选择选项 |
callback | AsyncCallback |
是 | callback 返回documentPicker选择后的结果集 |
示例:
import { BusinessError } from '@kit.BasicServicesKit';
import { common } from '@kit.AbilityKit';
import { picker } from '@kit.CoreFileKit';
async function example08(context: common.Context) { // 需确保 context 由 UIAbilityContext 转换而来
try {
let documentSelectOptions = new picker.DocumentSelectOptions();
let documentPicker = new picker.DocumentViewPicker(context);
documentPicker.select(documentSelectOptions, (err: BusinessError, documentSelectResult: Array) => {
if (err) {
console.error('DocumentViewPicker.select failed with err: ' + JSON.stringify(err));
return;
}
console.info('DocumentViewPicker.select successfully, documentSelectResult uri: ' + JSON.stringify(documentSelectResult));
});
} catch (error) {
let err: BusinessError = error as BusinessError;
console.error('DocumentViewPicker failed with err: ' + JSON.stringify(err));
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
select
select(callback: AsyncCallback
通过选择模式拉起documentPicker界面,用户可以选择一个或多个文件。接口采用callback异步返回形式,返回选择文件的uri数组。
注意:此接口返回的uri数组的具体使用方式参见用户文件uri介绍中的[文档类uri的使用方式]。
元服务API: 从API version 12开始,该接口支持在元服务中使用。
系统能力:SystemCapability.FileManagement.UserFileService
参数:
参数名 | 类型 | 必填 | 说明 |
---|---|---|---|
callback | AsyncCallback |
是 | callback 返回documentPicker选择后的结果集 |
示例:
import { BusinessError } from '@kit.BasicServicesKit';
import { common } from '@kit.AbilityKit';
import { picker } from '@kit.CoreFileKit';
async function example09(context: common.Context) { // 需确保 context 由 UIAbilityContext 转换而来
try {
let documentPicker = new picker.DocumentViewPicker(context);
documentPicker.select((err: BusinessError, documentSelectResult: Array) => {
if (err) {
console.error('DocumentViewPicker.select failed with err: ' + JSON.stringify(err));
return;
}
console.info('DocumentViewPicker.select successfully, documentSelectResult uri: ' + JSON.stringify(documentSelectResult));
});
} catch (error) {
let err: BusinessError = error as BusinessError;
console.error('DocumentViewPicker failed with err: ' + JSON.stringify(err));
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
save
save(option?: DocumentSaveOptions): Promise
通过保存模式拉起documentPicker界面,用户可以保存一个或多个文件。接口采用Promise异步返回形式,传入可选参数DocumentSaveOptions对象,返回保存文件的uri数组。
注意:此接口返回的uri数组的具体使用方式参见用户文件uri介绍中的[文档类uri的使用方式]。
元服务API: 从API version 12开始,该接口支持在元服务中使用。
系统能力:SystemCapability.FileManagement.UserFileService
参数:
参数名 | 类型 | 必填 | 说明 |
---|---|---|---|
option | [DocumentSaveOptions] | 否 | documentPicker保存选项,若无此参数,则拉起documentPicker界面后需用户自行输入保存的文件名 |
返回值:
类型 | 说明 |
---|---|
Promise |
Promise对象。返回documentPicker保存后的结果集 |
示例:
import { BusinessError } from '@kit.BasicServicesKit';
import { common } from '@kit.AbilityKit';
import { picker } from '@kit.CoreFileKit';
async function example10(context: common.Context) { // 需确保 context 由 UIAbilityContext 转换而来
try {
let documentSaveOptions = new picker.DocumentSaveOptions();
documentSaveOptions.newFileNames = ['DocumentViewPicker01.txt'];
let documentPicker = new picker.DocumentViewPicker(context);
docu
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
评论记录:
回复评论: