@capacitor/file-viewer
FileViewer API 提供了打开文件和预览媒体的功能。Web 平台不可用。
媒体预览功能目前仅在 iOS 上受支持。它使用内置播放器。
安装
npm install @capacitor/file-viewer@latest-7
npx cap sync
示例
import { FileViewer } from "@capacitor/file-viewer";
// 可以使用 @capacitor/filesystem 等插件来获取文件的完整路径
const openDocument = async () => {
await FileViewer.openDocumentFromLocalPath({
path: "path/to/file.pdf"
});
};
// iOS 专用功能
const previewMedia = async () => {
await FileViewer.previewMediaContentFromUrl({
path: "https://url_hosting_media/file.mp4"
});
}
API
现有错误代码列表,请参阅错误处理。
File Viewer API
仅在原生 Android 和 iOS 平台可用;Web/PWA 不可用。
openDocumentFromLocalPath(...)
openDocumentFromLocalPath(options: OpenFromLocalPathOptions) => Promise<void>
打开存储在本地文件系统中的文件
| 参数 | 类型 |
|---|---|
options | |
起始版本: 1.0.0
openDocumentFromResources(...)
openDocumentFromResources(options: OpenFromResourcesOptions) => Promise<void>
打开应用程序资源文件
| 参数 | 类型 |
|---|---|
options | |
起始版本: 1.0.0
openDocumentFromUrl(...)
openDocumentFromUrl(options: OpenFromUrlOptions) => Promise<void>
从远程 URL 打开文件
| 参数 | 类型 |
|---|---|
options | |
起始版本: 1.0.0
previewMediaContentFromLocalPath(...)
previewMediaContentFromLocalPath(options: PreviewMediaFromLocalPathOptions) => Promise<void>
预览存储在本地文件系统中的媒体文件(主要是视频)。
仅在 iOS 上 实现。Android 平台默认使用 openDocumentFromLocalPath。
| 参数 | 类型 |
|---|---|
options | |
起始版本: 1.0.0
previewMediaContentFromResources(...)
previewMediaContentFromResources(options: PreviewMediaFromResourcesOptions) => Promise<void>
预览应用程序资源中的媒体文件(主要是视频)。
仅在 iOS 上实现。Android 平台默认使用 openDocumentFromResources。
| 参数 | 类型 |
|---|---|
options | |
起始版本: 1.0.0
previewMediaContentFromUrl(...)
previewMediaContentFromUrl(options: PreviewMediaFromUrlOptions) => Promise<void>
预览远程 URL 中的媒体文件(主要是视频)。
仅在 iOS 上实现。Android 平台默认使用 openDocumentFromUrl。
| 参数 | 类型 |
|---|---|
options | |
起始版本: 1.0.0