目录

filePreview (File Preview)

This module provides convenient quick file preview capabilities for apps. Apps can use the system-level preview APIs provided by filePreview to quickly display the preview screen, so that users can preview various types of files. With the preview service, users can perform operations on various files, such as images, videos, audios, texts, and HTML files.

This module provides the capability of quick file preview, allowing your app to quickly open the preview window based on the file preview information.

The following file types support preview:

Type File Name Extension MIME Type
Text txt, cpp, c, h, java, xhtml, and xml text/plain, text/x-c++src, text/x-csrc, text/x-chdr, text/x-java, application/xhtml+xml, and text/xml
Web page html and htm text/html
Image jpg, png, gif, webp, bmp, and svg image/jpeg, image/png, image/gif, image/webp, image/bmp, and image/svg+xml
Audio m4a, aac, mp3, ogg, and wav audio/mp4a-latm, audio/aac, audio/mpeg, audio/ogg, and audio/x-wav
Video mp4, mkv, and ts video/mp4, video/x-matroska, and video/mp2ts
Folder None None
Document pdf

application/pdf

Since: 5.0.0(12)

Office document doc, docx, xls, xlsx, ppt, pptx, csv, and ofd

application/msword, application/vnd.openxmlformats-officedocument.wordprocessingml.document, application/vnd.ms-excel, application/vnd.openxmlformats-officedocument.spreadsheetml.sheet, application/vnd.ms-powerpoint, application/vnd.openxmlformats-officedocument.presentationml.presentation, text/csv, and general.ofd

Since: 5.0.0(12)

Since: 4.1.0(11)

Modules to Import

import { filePreview } from '@kit.PreviewKit';

PreviewInfo

File preview information, including the file title, URI, and file type (mimeType).

Model restriction: This API can be used only in the stage model.

Atomic service API: This API can be used in atomic services since API version 4.1.0(11).

System capability: SystemCapability.FileManagement.FilePreview.Core

Since: 4.1.0(11)

Name Type Read-Only Optional Description
title string No Yes File title.
uri string No No URI of the file.
mimeType string No No

Media resource type of the file, for example, text/plain.

Note:

If the media resource type cannot be determined, this field can be set to an empty string (''). The system will infer the type based on the URI suffix.

DisplayInfo

Attribute values of the floating window, including its size and position.

Model restriction: This API can be used only in the stage model.

Atomic service API: This API can be used in atomic services since API version 4.1.0(11).

System capability: SystemCapability.FileManagement.FilePreview.Core

Since: 4.1.0(11)

Name Type Read-Only Optional Description
x number No No Start X coordinate of the preview window, in pixels.
y number No No Start Y coordinate of the preview window, in pixels.
width number No Yes Width of the preview window, in pixels.
height number No Yes Height of the preview window, in pixels.

openPreview

openPreview(context: Context, file: PreviewInfo, info?: DisplayInfo): Promise<void>

Displays the preview window based on the file preview information and floating window attributes. Calling this API repeatedly within 1 second will not take effect. This API uses a promise to return the result.

This API requires that the passed URI can be used for authorization transfer.

Model restriction: This API can be used only in the stage model.

Atomic service API: This API can be used in atomic services since API version 4.1.0(11).

System capability: SystemCapability.FileManagement.FilePreview.Core

Since: 4.1.0(11)

Parameters:

Parameter Type Mandatory Description
context Context Yes

common.UIAbilityContext.

Note:

Currently, the context supports only UIAbilityContext.

file PreviewInfo Yes File preview information. The title parameter is optional. If title is left blank, URI will be parsed. If URI cannot be parsed, the file will be marked as an unknown file.
info DisplayInfo No Modal window display information. If this parameter is not set for a PC or 2-in-1 device, the default window size is used. This parameter is invalid for phones and tablets.

Returns:

Type Description
Promise<void> Promise that returns no value.

Error codes: For details about the error codes, please refer to Universal Error Codes.

Error Code Error Message
401 invalid input parameter.

Example:

import { BusinessError } from '@kit.BasicServicesKit';
import { filePreview } from '@kit.PreviewKit';

let uiContext = this.getUIContext().getHostContext() as Context;
// Set the window display information.
let displayInfo: filePreview.DisplayInfo = {
  x: 100,
  y: 100,
  width: 800,
  height: 800
};
// Set the file preview information.
let fileInfo: filePreview.PreviewInfo = {
  title: '1.txt',
  uri: 'file://docs/storage/Users/currentUser/Documents/1.txt',
  mimeType: 'text/plain'
};
filePreview.openPreview(uiContext, fileInfo, displayInfo).then(() => {
  console.info('Succeeded in opening preview');
}).catch((err: BusinessError) => {
  console.error(`Failed to open preview, err.code = ${err.code}, err.message = ${err.message}`);
});

openPreview

openPreview(context: Context, file: PreviewInfo, info: DisplayInfo, callback: AsyncCallback<void>): void

Displays the preview window based on the file preview information and floating window attributes. Calling this API repeatedly within 1 second will not take effect. This API uses an asynchronous callback to return the result.

This API requires that the passed URI can be used for authorization transfer.

Model restriction: This API can be used only in the stage model.

Atomic service API: This API can be used in atomic services since API version 4.1.0(11).

System capability: SystemCapability.FileManagement.FilePreview.Core

Since: 4.1.0(11)

Parameters:

Parameter Type Mandatory Description
context Context Yes

common.UIAbilityContext.

Note:

Currently, the context supports only UIAbilityContext.

file PreviewInfo Yes File preview information. The title parameter is optional. If title is left blank, URI will be parsed. If URI cannot be parsed, the file will be marked as an unknown file.
info DisplayInfo Yes Modal window display information. This parameter is invalid for phones and tablets.
callback AsyncCallback<void> Yes Callback function. If the preview window is displayed successfully, err is undefined or err.code is 0. Otherwise, err is an error object.

Error codes: For details about the error codes, please refer to Universal Error Codes.

Error Code Error Message
401 invalid input parameter.

Example:

import { filePreview } from '@kit.PreviewKit';

let uiContext = this.getUIContext().getHostContext() as Context;
// Set the window display information.
let displayInfo: filePreview.DisplayInfo = {
  x: 100,
  y: 100,
  width: 800,
  height: 800
};
// Set the file preview information.
let fileInfo: filePreview.PreviewInfo = {
  title: '1.txt',
  uri: 'file://docs/storage/Users/currentUser/Documents/1.txt',
  mimeType: 'text/plain'
};
filePreview.openPreview(uiContext, fileInfo, displayInfo, (err) => {
  if (err && err.code) {
    console.error(`Failed to open preview, err.code = ${err.code}, err.message = ${err.message}`);
    return;
  }
  console.info('Succeeded in opening preview');
});

openPreview

openPreview(context: Context, files: Array<PreviewInfo>, index?: number): Promise<void>

Displays the preview window based on the preview information of multiple files and selected file information indexes. Calling this API repeatedly within 1 second will not take effect. This API uses a promise to return the result.

This API requires that the passed URI can be used for authorization transfer.

Model restriction: This API can be used only in the stage model.

Atomic service API: This API can be used in atomic services since API version 5.0.0(12).

System capability: SystemCapability.FileManagement.FilePreview.Core

Device behavior differences: This API returns error code 801 when being called on PCs/2-in-1 devices, but works properly on other devices.

Since: 5.0.0(12)

Parameters:

Parameter Type Mandatory Description
context Context Yes

common.UIAbilityContext.

Note:

Currently, the context supports only UIAbilityContext.

files Array<PreviewInfo> Yes File preview information list.
index number No File preview information indexes displayed when the preview window is displayed. If this parameter is left blank, the default value 0 is used. The value is greater than or equal to 0 and less than the length of files.

Returns:

Type Description
Promise<void> Promise that returns no value.

Error codes: For details about the error codes, please refer to Universal Error Codes.

Error Code Error Message
401 invalid input parameter.
801 Capability not supported.

Example:

import { BusinessError } from '@kit.BasicServicesKit';
import { filePreview } from '@kit.PreviewKit';

let uiContext = this.getUIContext().getHostContext() as Context;
// Create the preview information of a single file.
let fileInfo: filePreview.PreviewInfo = {
  title: '1.txt',
  uri: 'file://docs/storage/Users/currentUser/Documents/1.txt',
  mimeType: 'text/plain'
};
// Create an array for viewing multiple files.
let files: Array<filePreview.PreviewInfo> = new Array();
files.push(fileInfo);
filePreview.openPreview(uiContext, files, 0).then(() => {
  console.info('Succeeded in opening preview');
}).catch((err: BusinessError) => {
  console.error(`Failed to open preview, err.code = ${err.code}, err.message = ${err.message}`);
});

canPreview

canPreview(context: Context, uri: string): Promise<boolean>

Checks whether a file or folder can be previewed based on the URI. If the URI of a file that supports preview is passed, true will be returned. If the URI of a file that cannot be previewed is passed, false will be returned. This API uses a promise to return the result.

The current API checks only whether the file exists and whether the file format is supported. When viewing the file using openPreview, ensure that the authorization of the file can be transferred.

Model restriction: This API can be used only in the stage model.

Atomic service API: This API can be used in atomic services since API version 4.1.0(11).

System capability: SystemCapability.FileManagement.FilePreview.Core

Since: 4.1.0(11)

Parameters:

Parameter Type Mandatory Description
context Context Yes

common.UIAbilityContext.

Note:

Currently, the context supports only UIAbilityContext.

uri string Yes URI of the file.

Returns:

Type Description
Promise<boolean> Promise used to return the result. If the URI of a file that supports preview is passed, true will be returned. If the URI of a file that cannot be previewed is passed, false will be returned.

Error codes: For details about the error codes, please refer to Universal Error Codes.

Error Code Error Message
401 invalid input parameter.

Example:

import { BusinessError } from '@kit.BasicServicesKit';
import { filePreview } from '@kit.PreviewKit';

 // The file supports preview and exists.
let uri = 'file://docs/storage/Users/currentUser/Documents/1.txt';
let uiContext = this.getUIContext().getHostContext() as Context;
filePreview.canPreview(uiContext, uri).then((result) => { // true will be returned.
  console.info(`Succeeded in obtaining the result of whether it can be previewed. result = ${result}`);
}).catch((err: BusinessError) => {
  console.error(`Failed to obtain the result of whether it can be previewed, err.code = ${err.code}, err.message = ${err.message}`);
})
import { BusinessError } from '@kit.BasicServicesKit';
import { filePreview } from '@kit.PreviewKit';

// The file does not support preview or the file does not exist.
let uri = 'file://docs/storage/Users/currentUser/Documents/1.txt';
let uiContext = this.getUIContext().getHostContext() as Context;
filePreview.canPreview(uiContext, uri).then((result) => { // false will be returned.
  console.info(`Succeeded in obtaining the result of whether it can be previewed. result = ${result}`);
}).catch((err: BusinessError) => {
  console.error(`Failed to obtain the result of whether it can be previewed, err.code = ${err.code}, err.message = ${err.message}`);
});

canPreview

canPreview(context: Context, uri: string, callback: AsyncCallback<boolean>): void

Checks whether a file or folder can be previewed based on the URI. If the URI of a file that supports preview is passed, true will be returned. If the URI of a file that cannot be previewed is passed, false will be returned. This API uses an asynchronous callback to return the result.

The current API checks only whether the file exists and whether the file format is supported. When viewing the file using openPreview, ensure that the authorization of the file can be transferred.

Model restriction: This API can be used only in the stage model.

Atomic service API: This API can be used in atomic services since API version 4.1.0(11).

System capability: SystemCapability.FileManagement.FilePreview.Core

Since: 4.1.0(11)

Parameters:

Parameter Type Mandatory Description
context Context Yes

common.UIAbilityContext.

Note:

Currently, the context supports only UIAbilityContext.

uri string Yes URI of the file.
callback AsyncCallback<boolean> Yes Callback function. If the URI of a file that supports preview is passed, true will be returned. If the URI of a file that cannot be previewed is passed, false will be returned.

Error codes: For details about the error codes, please refer to Universal Error Codes.

Error Code Error Message
401 invalid input parameter.

Example:

import { filePreview } from '@kit.PreviewKit';

// The file supports preview and exists.
let uri = 'file://docs/storage/Users/currentUser/Documents/1.txt';
let uiContext = this.getUIContext().getHostContext() as Context;
filePreview.canPreview(uiContext, uri, (err, result) => {
  if (err && err.code) {
    console.error(`Failed to obtain the result of whether it can be previewed, err.code = ${err.code}, err.message = ${err.message}`);
    return;
  }
  console.info(`Succeeded in obtaining the result of whether it can be previewed. result = ${result}`); // true will be returned.
});
import { filePreview } from '@kit.PreviewKit';

// The file does not support preview or the file does not exist.
let uri = 'file://docs/storage/Users/currentUser/Documents/9.txt';
let uiContext = this.getUIContext().getHostContext() as Context;
filePreview.canPreview(uiContext, uri, (err, result) => {
  if (err && err.code) {
    console.error(`Failed to obtain the result of whether it can be previewed, err.code = ${err.code}, err.message = ${err.message}`);
    return;
  }
  console.info(`Succeeded in obtaining the result of whether it can be previewed. result = ${result}`); // false will be returned.
});

hasDisplayed

hasDisplayed(context: Context): Promise<boolean>

Checks whether a preview window already exists. The preview window is implemented as a singleton. If the preview window has been opened and is not closed, true will be returned. If the preview window has not been opened or has been closed after being opened, false will be returned. This API uses a promise to return the result. It can take effect only after a preview window has been created. If hasDisplayed is called before the preview window has been created, false will be returned.

Model restriction: This API can be used only in the stage model.

Atomic service API: This API can be used in atomic services since API version 4.1.0(11).

System capability: SystemCapability.FileManagement.FilePreview.Core

Since: 4.1.0(11)

Parameters:

Parameter Type Mandatory Description
context Context Yes

common.UIAbilityContext.

Note:

Currently, the context supports only UIAbilityContext.

Returns:

Type Description
Promise<boolean> Promise used to return the result. The preview window is implemented as a singleton. If the preview window has been opened and is not closed, true will be returned. If the preview window has not been opened or has been closed after being opened, false will be returned.

Error codes: For details about the error codes, please refer to Universal Error Codes.

Error Code Error Message
401 invalid input parameter.

Example:

import { BusinessError } from '@kit.BasicServicesKit';
import { filePreview } from '@kit.PreviewKit';

// The preview window already exists.
let uiContext = this.getUIContext().getHostContext() as Context;
filePreview.hasDisplayed(uiContext).then((result) => { // true will be returned.
  console.info(`Succeeded in obtaining the result of whether the preview has displayed. result = ${result}`);
}).catch((err: BusinessError) => {
  console.error(`Failed to obtain the result of whether the preview has displayed, err.code = ${err.code}, err.message = ${err.message}`);
});
import { BusinessError } from '@kit.BasicServicesKit';
import { filePreview } from '@kit.PreviewKit';

 // The preview window does not exist.
let uiContext = this.getUIContext().getHostContext() as Context;
filePreview.hasDisplayed(uiContext).then((result) => { // false will be returned.
  console.info(`Succeeded in obtaining the result of whether the preview has displayed. result = ${result}`);
}).catch((err: BusinessError) => {
  console.error(`Failed to obtain the result of whether the preview has displayed, err.code = ${err.code}, err.message = ${err.message}`);
});

hasDisplayed

hasDisplayed(context: Context, callback: AsyncCallback<boolean>): void

Checks whether a preview window already exists. The preview window is implemented as a singleton. If the preview window has been opened and is not closed, true will be returned. If the preview window has not been opened or has been closed after being opened, false will be returned. This API uses an asynchronous callback to return the result. It can take effect only after a preview window has been created. If hasDisplayed is called before the preview window has been created, false will be returned.

Model restriction: This API can be used only in the stage model.

Atomic service API: This API can be used in atomic services since API version 4.1.0(11).

System capability: SystemCapability.FileManagement.FilePreview.Core

Since: 4.1.0(11)

Parameters:

Parameter Type Mandatory Description
context Context Yes

common.UIAbilityContext.

Note:

Currently, the context supports only UIAbilityContext.

callback AsyncCallback<boolean> Yes Callback function. The preview window is implemented as a singleton. If the preview window has been opened and is not closed, true will be returned. If the preview window has not been opened or has been closed after being opened, false will be returned.

Error codes: For details about the error codes, please refer to Universal Error Codes.

Error Code Error Message
401 invalid input parameter.

Example:

import { filePreview } from '@kit.PreviewKit';

// The preview window already exists.
let uiContext = this.getUIContext().getHostContext() as Context;
filePreview.hasDisplayed(uiContext, (err, result) => {
  if (err && err.code) {
    console.error(`Failed to obtain the result of whether the preview has displayed, err.code = ${err.code}, err.message = ${err.message}`);
    return;
  }
  console.info(`Succeeded in obtaining the result of whether the preview has displayed. result = ${result}`); // true will be returned.
});
import { filePreview } from '@kit.PreviewKit';

// The preview window does not exist.
let uiContext = this.getUIContext().getHostContext() as Context;
filePreview.hasDisplayed(uiContext, (err, result) => {
  if (err && err.code) {
    console.error(`Failed to obtain the result of whether the preview has displayed, err.code = ${err.code}, err.message = ${err.message}`);
    return;
  }
  console.info(`Succeeded in obtaining the result of whether the preview has displayed. result = ${result}`); // false will be returned.
});

closePreview

closePreview(context: Context): Promise<void>

Closes the preview window and takes effect only when a preview window exists. This API uses a promise to return the result. It can take effect only after a preview window has been created. If closePreview is called before the preview window has been created, the API call will fail.

Model restriction: This API can be used only in the stage model.

Atomic service API: This API can be used in atomic services since API version 4.1.0(11).

System capability: SystemCapability.FileManagement.FilePreview.Core

Since: 4.1.0(11)

Parameters:

Parameter Type Mandatory Description
context Context Yes

common.UIAbilityContext.

Note:

Currently, the context supports only UIAbilityContext.

Returns:

Type Description
Promise<void> Promise that returns no value.

Error codes: For details about the error codes, please refer to Universal Error Codes.

Error Code Error Message
401 invalid input parameter.

Example:

import { BusinessError } from '@kit.BasicServicesKit';
import { filePreview } from '@kit.PreviewKit';

let uiContext = this.getUIContext().getHostContext() as Context;
filePreview.closePreview(uiContext).then(() => { // Take effect only when a preview window exists.
  console.info('Succeeded in closing preview');
}).catch((err: BusinessError) => {
  console.error(`Failed to close preview, err.code = ${err.code}, err.message = ${err.message}`);
});

closePreview

closePreview(context: Context, callback: AsyncCallback<void>): void

Closes the preview window and takes effect only when a preview window exists. This API uses an asynchronous callback to return the result. It can take effect only after a preview window has been created. If closePreview is called before the preview window has been created, the API call will fail.

Model restriction: This API can be used only in the stage model.

Atomic service API: This API can be used in atomic services since API version 4.1.0(11).

System capability: SystemCapability.FileManagement.FilePreview.Core

Since: 4.1.0(11)

Parameters:

Parameter Type Mandatory Description
context Context Yes

common.UIAbilityContext.

Note:

Currently, the context supports only UIAbilityContext.

callback AsyncCallback<void> Yes Callback function. If the preview window is closed successfully, err is undefined or err.code is 0. Otherwise, err is an error object.

Error codes: For details about the error codes, please refer to Universal Error Codes.

Error Code Error Message
401 invalid input parameter.

Example:

import { filePreview } from '@kit.PreviewKit';

let uiContext = this.getUIContext().getHostContext() as Context;
filePreview.closePreview(uiContext, (err) => { // Take effect only when a preview window exists.
  if (err && err.code) {
    console.error(`Failed to close preview, err.code = ${err.code}, err.message = ${err.message}`);
    return;
  }
  console.info('Succeeded in closing preview');
});

loadData

loadData(context: Context, file: PreviewInfo): Promise<void>

Loads information about the file to be previewed. This API can take effect only when a preview window exists. If a file that can be previewed is passed, the preview page will be displayed. If a file that cannot be previewed is passed, a message will be displayed, indicating that the preview page cannot be opened. Calling this API repeatedly within 100 milliseconds will not take effect. This API uses a promise to return the result. It can take effect only after a preview window has been created. If loadData is called before the preview window has been created, the API call will fail.

Model restriction: This API can be used only in the stage model.

Atomic service API: This API can be used in atomic services since API version 4.1.0(11).

System capability: SystemCapability.FileManagement.FilePreview.Core

Since: 4.1.0(11)

Parameters:

Parameter Type Mandatory Description
context Context Yes

common.UIAbilityContext.

Note:

Currently, the context supports only UIAbilityContext.

file PreviewInfo Yes File preview information. The title parameter is optional. If title is left blank, URI will be parsed. If URI cannot be parsed, the file will be marked as an unknown file.

Returns:

Type Description
Promise<void> Promise that returns no value.

Error codes: For details about the error codes, please refer to Universal Error Codes.

Error Code Error Message
401 invalid input parameter.

Example:

import { BusinessError } from '@kit.BasicServicesKit';
import { filePreview } from '@kit.PreviewKit';

let uiContext = this.getUIContext().getHostContext() as Context;
let fileInfo: filePreview.PreviewInfo = {
  title: '1.txt',
  uri: 'file://docs/storage/Users/currentUser/Documents/1.txt',
  mimeType: 'text/plain'
};
filePreview.loadData(uiContext, fileInfo).then(() => { // Take effect only when a preview window exists.
  console.info('Succeeded in loading data.');
}).catch((err: BusinessError) => {
  console.error(`Failed to load data, err.code = ${err.code}, err.message = ${err.message}`);
});

loadData

loadData(context: Context, file: PreviewInfo, callback: AsyncCallback<void>): void

Loads information about the file to be previewed. This API can take effect only when a preview window exists. If a file that can be previewed is passed, the preview page will be displayed. If a file that cannot be previewed is passed, a message will be displayed, indicating that the preview page cannot be opened. Calling this API repeatedly within 100 milliseconds will not take effect. This API uses an asynchronous callback to return the result. It can take effect only after a preview window has been created. If loadData is called before the preview window has been created, the API call will fail.

Model restriction: This API can be used only in the stage model.

Atomic service API: This API can be used in atomic services since API version 4.1.0(11).

System capability: SystemCapability.FileManagement.FilePreview.Core

Since: 4.1.0(11)

Parameters:

Parameter Type Mandatory Description
context Context Yes

common.UIAbilityContext.

Note:

Currently, the context supports only UIAbilityContext.

file PreviewInfo Yes File preview information. The title parameter is optional. If title is left blank, URI will be parsed. If URI cannot be parsed, the file will be marked as an unknown file.
callback AsyncCallback<void> Yes Callback function. If the preview file is loaded successfully, err is undefined or err.code is 0. Otherwise, err is an error object.

Error codes: For details about the error codes, please refer to Universal Error Codes.

Error Code Error Message
401 invalid input parameter.

Example:

import { filePreview } from '@kit.PreviewKit';

let uiContext = this.getUIContext().getHostContext() as Context;
let fileInfo: filePreview.PreviewInfo = {
  title: '1.txt',
  uri: 'file://docs/storage/Users/currentUser/Documents/1.txt',
  mimeType: 'text/plain'
};
filePreview.loadData(uiContext, fileInfo, (err) => { // Take effect only when a preview window exists.
  if (err && err.code) {
    console.error(`Failed to load data, err.code = ${err.code}, err.message = ${err.message}`);
    return;
  }
  console.info('Succeeded in loading data.');
});

loadData

loadData(context: Context, files: Array<PreviewInfo>, index?: number): Promise<void>

Loads information about the file to be previewed. This API can take effect only when a preview window exists. The preview window is displayed based on the preview information of multiple files and selected file information indexes. This API uses a promise to return the result.

It can take effect only after a preview window has been created. If loadData is called before the preview window has been created, the API call will fail. This API does not respond on PCs or 2-in-1 devices. Calling this API repeatedly within 100 milliseconds will not take effect.

Model restriction: This API can be used only in the stage model.

Atomic service API: This API can be used in atomic services since API version 5.0.0(12).

System capability: SystemCapability.FileManagement.FilePreview.Core

Device behavior differences: This API returns error code 801 when being called on PCs/2-in-1 devices, but works properly on other devices.

Since: 5.0.0(12)

Parameters:

Parameter Type Mandatory Description
context Context Yes

common.UIAbilityContext.

Note:

Currently, the context supports only UIAbilityContext.

files Array<PreviewInfo> Yes File preview information list.
index number No File preview information indexes displayed when the preview window is displayed. If this parameter is left blank, the default value 0 is used. The value is greater than or equal to 0 and less than the length of files.

Returns:

Type Description
Promise<void> Promise that returns no value.

Error codes: For details about the error codes, please refer to Universal Error Codes.

Error Code Error Message
401 invalid input parameter.
801 Capability not supported.

Example:

import { BusinessError } from '@kit.BasicServicesKit';
import { filePreview } from '@kit.PreviewKit';

let uiContext = this.getUIContext().getHostContext() as Context;
let fileInfo: filePreview.PreviewInfo = {
  title: '1.txt',
  uri: 'file://docs/storage/Users/currentUser/Documents/1.txt',
  mimeType: 'text/plain'
};
let files: Array<filePreview.PreviewInfo> = new Array();
files.push(fileInfo);
filePreview.loadData(uiContext, files, 0).then(() => { // Take effect only when a preview window exists.
  console.info('Succeeded in loading data.');
}).catch((err: BusinessError) => {
  console.error(`Failed to load data, err.code = ${err.code}, err.message = ${err.message}`);
});