鸿蒙-使用系统文件预览不同类型的本地文件

SDK 版本:HarmonyOS NEXT Developer Beta2 SDK (5.0.0.31)
DevEco-Studio 版本:DevEco Studio NEXT Developer Beta2 (5.0.3.502)
工程机版本:ALN-AL00 NEXT.0.0.31

使用系统文件预览不同类型的本地文件

MediaUtils.ets

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
/*
* Copyright (c) 2024. Dench.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import { fileUri } from "@kit.CoreFileKit";
import { BusinessError } from "@kit.BasicServicesKit";
import { filePreview } from "@kit.PreviewKit";
import { uniformTypeDescriptor } from "@kit.ArkData";

/**
* filePreview(文件预览)
*/
export function openSystemPreview(context: Context, path: string) {
// let uiContext = getContext(this);
// let displayInfo: filePreview.DisplayInfo = {
// x: 100,
// y: 100,
// width: 800,
// height: 800
// };
const uri = fileUri.getUriFromPath(path);
const mimeType = getFileMimeType(path);
console.info("uri:" + uri);
console.info("mimeType:" + mimeType);
let fileInfo: filePreview.PreviewInfo = {
uri: uri,
mimeType: mimeType,
};
filePreview
.openPreview(context, fileInfo)
.then(() => {
console.info("Succeeded in opening preview");
})
.catch((err: BusinessError) => {
console.error(
`Failed to open preview, err.code = ${err.code}, err.message = ${err.message}`
);
});
}

/**
* 根据文件后缀查询对应文件的 mimeType
*/
export function getFileMimeType(path: string): string {
if (path.indexOf(".") != -1) {
try {
const ext = "." + path.split(".").pop();
console.info("ext:" + ext);
// 2.可根据 “.mp3” 文件后缀查询对应UTD数据类型,并查询对应UTD数据类型的具体属性
let typeId1 =
uniformTypeDescriptor.getUniformDataTypeByFilenameExtension(ext);
console.info("typeId1:" + typeId1);
let typeObj1 = uniformTypeDescriptor.getTypeDescriptor(typeId1);
// console.info('typeId:' + typeObj1.typeId);
console.info("belongingToTypes:" + typeObj1.belongingToTypes);
// console.info('description:' + typeObj1.description);
// console.info('referenceURL:' + typeObj1.referenceURL);
// console.info('filenameExtensions:' + typeObj1.filenameExtensions);
console.info("mimeTypes:" + typeObj1.mimeTypes);
return typeObj1.mimeTypes[0];
} catch (e) {}
}
return "text/plain";
}

Reference

https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/preview-arkts-V5#section1081123302517

https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/uniform-data-type-descriptors-V5

作者

Dench

发布于

2024-08-08

更新于

2024-08-08

许可协议

CC BY-NC-SA 4.0

Your browser is out-of-date!

Update your browser to view this website correctly.&npsb;Update my browser now

×