跳过内容

开始使用

终端窗口
bun add @capgo/capacitor-file-picker
bunx cap sync

导入

导入
import { CapgoFilePicker } from '@capgo/capacitor-file-picker';

API概述

API概述

pickFiles

选择文件

从设备中选择一个或多个文件。

import { CapgoFilePicker } from '@capgo/capacitor-file-picker';
const result = await CapgoFilePicker.pickFiles({
types: ['application/pdf', 'image/*'],
limit: 5,
readData: false
});
console.log('Picked files:', result.files);

pickImages

选择图片

从相册中选择一个或多个图片(仅Android/iOS)。

import { CapgoFilePicker } from '@capgo/capacitor-file-picker';
const result = await CapgoFilePicker.pickImages({
limit: 10,
readData: false
});
console.log('Picked images:', result.files);

pickVideos

选择视频

从图库中选择一个或多个视频。 仅限 Android/iOS。

import { CapgoFilePicker } from '@capgo/capacitor-file-picker';
const result = await CapgoFilePicker.pickVideos({
limit: 3,
skipTranscoding: true
});
console.log('Picked videos:', result.files);

从图库中选择一个或多个图片或视频。 仅限 Android/iOS。

import { CapgoFilePicker } from '@capgo/capacitor-file-picker';
const result = await CapgoFilePicker.pickMedia({
limit: 5,
readData: true
});
console.log('Picked media:', result.files);

从设备中选择一个目录。 仅限 Android/iOS。

import { CapgoFilePicker } from '@capgo/capacitor-file-picker';
const result = await CapgoFilePicker.pickDirectory();
console.log('Selected directory:', result.path);

将 HEIC 图像转换为 JPEG 格式。 仅限 iOS。

import { CapgoFilePicker } from '@capgo/capacitor-file-picker';
const result = await CapgoFilePicker.convertHeicToJpeg({
path: '/path/to/image.heic',
quality: 0.9
});
console.log('Converted file:', result.path);

复制文件到新位置。

import { CapgoFilePicker } from '@capgo/capacitor-file-picker';
await CapgoFilePicker.copyFile({
from: '/source/file.pdf',
to: '/destination/file.pdf',
overwrite: true
});

checkPermissions

检查权限

仅限 Android,检查读取文件的权限。

import { CapgoFilePicker } from '@capgo/capacitor-file-picker';
const status = await CapgoFilePicker.checkPermissions();
console.log('Read permission:', status.readExternalStorage);

requestPermissions

请求读取文件的权限

仅限 Android,请求读取文件的权限。

import { CapgoFilePicker } from '@capgo/capacitor-file-picker';
const status = await CapgoFilePicker.requestPermissions();
if (status.readExternalStorage === 'granted') {
console.log('Permission granted');
}

类型参考

类型参考

PickFilesOptions

选择文件选项

选择文件的选项

export interface PickFilesOptions {
/**
* List of accepted MIME types or file extensions.
* On iOS, only MIME types are supported.
* Examples: ['image/*'], ['application/pdf'], ['.pdf', '.doc']
*/
types?: string[];
/**
* Maximum number of files to pick.
* Set to 0 for unlimited (platform default).
* @default 0
*/
limit?: number;
/**
* Whether to read the file data as base64.
* Note: Reading large files may cause memory issues.
* @default false
*/
readData?: boolean;
}

文件选择结果。

export interface PickFilesResult {
/** Array of picked files */
files: PickedFile[];
}

选择媒体(图片/视频)的选项。

export interface PickMediaOptions {
/**
* Maximum number of files to pick.
* Set to 0 for unlimited (platform default).
* @default 0
*/
limit?: number;
/**
* Whether to read the file data as base64.
* Note: Reading large files may cause memory issues.
* @default false
*/
readData?: boolean;
/**
* iOS only: Skip transcoding of videos.
* @default false
*/
skipTranscoding?: boolean;
/**
* iOS 15+ only: Show ordered selection badges.
* @default false
*/
ordered?: boolean;
}

选择目录的结果。

export interface PickDirectoryResult {
/** The path to the selected directory */
path: string;
}

将 HEIC 转换为 JPEG 的选项。

export interface ConvertHeicToJpegOptions {
/** The path to the HEIC file to convert */
path: string;
/**
* The compression quality for JPEG (0.0 - 1.0).
* @default 0.9
*/
quality?: number;
}

ConvertHeicToJpegResult

标题:HEIC转JPEG结果

HEIC转JPEG转换结果。

export interface ConvertHeicToJpegResult {
/** The path to the converted JPEG file */
path: string;
}

复制文件的选项。

export interface CopyFileOptions {
/** Source file path */
from: string;
/** Destination file path */
to: string;
/**
* Whether to overwrite if destination exists.
* @default false
*/
overwrite?: boolean;
}

文件访问权限状态。

export interface PermissionStatus {
/** Whether permission to read media files is granted */
readExternalStorage: PermissionState;
/** Whether permission to access media location is granted */
accessMediaLocation?: PermissionState;
}

选择器dismiss事件的回调函数。

export type PickerDismissedListener = (event: null) => void;

表示已选择的文件。

export interface PickedFile {
/** The name of the file */
name: string;
/** The path to the file */
path?: string;
/** The MIME type of the file */
mimeType: string;
/** The size of the file in bytes */
size: number;
/**
* The base64 encoded data of the file.
* Only present if readData was true.
*/
data?: string;
/**
* The Blob instance of the file.
* Web only.
*/
blob?: Blob;
/** Width in pixels (images/videos only) */
width?: number;
/** Height in pixels (images/videos only) */
height?: number;
/** Duration in seconds (videos only) */
duration?: number;
/** Last modified timestamp in milliseconds */
modifiedAt?: number;
}

权限状态值。

export type PermissionState = 'prompt' | 'prompt-with-rationale' | 'granted' | 'denied';

本页面是根据插件的 src/definitions.ts当公共 API 在上游发生变化时,请重新同步。