```
import utdDesc from '@ohos.data.uniformTypeDescriptor';
import fs from '@ohos.file.fs';
import common from '@ohos.app.ability.common';
import hilog from '@ohos.hilog';
let context = getContext(this) as common.UIAbilityContext;
let baseDir = context.filesDir;
let filesPath = baseDir + '/utdType';
class FileInfo {
filename: string;
utdType: string;
iconFile: string;
getListFile(): string[] {
let files = fs.listFileSync(filesPath);
for (let i = 0; i < files.length; i++) {
hilog.info(0, `The name of file: ${files}`);
}
return files;
}
UpdateSelectedFiles(): void {
this.fileNames = this.getListFile();
for (let i = this.selectedFilesInfos.length - 1; i >= 0; i--) {
this.selectedFilesInfos.pop();
}
this.selectedFilesInfos = [];
hilog.info(0, "selectText: " + this.text)
for (let i = 0; i < this.fileNames.length; i++) {
let fileName = this.fileNames;
hilog.info(0, `fileNames file: ${fileName}`);
let index = fileName.lastIndexOf('.');
if (index < 0) {
continue;
} else {
let fileExtension = fileName.substring(index);
hilog.info(0, `fileNames file extension: ${fileExtension}`);
let utd = utdDesc.getUniformDataTypeByFilenameExtension(fileExtension);
try {
if (utd != null) {
let typeObj: utdDesc.TypeDescriptor = utdDesc.getTypeDescriptor(utd);
let utdType: string = "";
if (typeObj.belongsTo("general.image")) {
utdType = "general.image"
} else if (typeObj.belongsTo("general.audio")) {
utdType = "general.audio"
} else if (typeObj.belongsTo("general.video")) {
utdType = "general.video"
} else {
continue;
}
hilog.info(0, `typeObj.belongsTo: ${fileName}, utdType: ${utdType}`);
let fileInfo = new FileInfo(fileName, utdType, typeObj.iconFile);
this.selectedFilesInfos.push(fileInfo);
}
}
catch (e) {
let error: BusinessError = e as BusinessError;
hilog.error(0, `belongsTo throws an exception. code is ${error.code}, message is ${error.message} `);
}
}
}
}