OpenHarmony开发者论坛
标题:
使用ServiceExtensionAbility后fileAccess.createFileAccessHelper(context)方法出现异常
[打印本页]
作者:
tenon
时间:
4 天前
标题:
使用ServiceExtensionAbility后fileAccess.createFileAccessHelper(context)方法出现异常
[md]### 【问题描述】
1. 介绍问题现象和发生的背景
使用ServiceExtensionAbility来实现开机启动时, 使用fileAccess.createFileAccessHelper(context)去读取U盘中的内容, 出现报错代码14300003
2. 相关的代码(请勿使用截图)
```
private async loadAdvertFromExternalStorage(): Promise<void> {
try {
let isDone: boolean = false
let helper = fileAccess.createFileAccessHelper(AppUtil.getContext())
let rootIterator = await helper.getRoots()
if (!rootIterator) {
this.logger.info("getRoots interface returns an undefined object");
return
}
let rootinfos: Array<fileAccess.RootInfo> = [];
while (!isDone) {
let result = rootIterator.next()
isDone = result.done
if (!isDone && result.value.deviceType == 6) { // 外置存储
rootinfos.push(result.value);
this.logger.info(`发现外置存储: ${result.value.displayName}`);
}
}
let fileInfos: Record<string, Array<NativeMaterial>> = {}
for (let index = 0; index < rootinfos.length; index++) {
let rootInfo = rootinfos[index]
let fileIterator = rootInfo.listFile({
suffix: ['.mp4', '.png', '.jpg', '.jpeg'],
displayName: ['ad_*']
});
if (!fileIterator) {
this.logger.info("listFile interface returns an undefined object");
return
}
isDone = false
while (!isDone) {
let result = fileIterator.next()
isDone = result.done
if (!isDone) {
let type = result.value.fileName.endsWith('.mp4') ? MaterialType.native_video : MaterialType.native_image
let splitNames = result.value.fileName.split('_') // 格式 ad_8_xx.mp4 | ad_8_xx.png
if (splitNames.length >= 3) {
let duration = Number.parseInt(splitNames[1])
if (duration) {
let material: NativeMaterial = { uri: result.value.uri, relativePath: result.value.relativePath, type: type, duration: duration }
this.logger.info(`发现素材: ${JSON.stringify(material)}`)
materials.push(material)
} else {
this.logger.info(`素材: ${result.value.relativePath} -----时长获取失败`)
}
} else {
this.logger.info(`素材: ${result.value.relativePath} -----不满足条件`)
}
}
}
if (ArrayUtil.isNotEmpty(materials)) {
fileInfos[rootInfo.displayName] = materials
}
}
Object.entries(fileInfos).map(kv => {
let videos = kv[1].filter(e => e.type == MaterialType.native_video)
if (ArrayUtil.isNotEmpty(videos)) {
this.nativeAdverts.push(new NativeAdvertPlayPlan(MaterialType.native_video, kv[0], videos))
}
let images = kv[1].filter(e => e.type == MaterialType.native_image)
if (ArrayUtil.isNotEmpty(images)) {
this.nativeAdverts.push(new NativeAdvertPlayPlan(MaterialType.native_image, kv[0], images))
}
})
} catch (err) {
this.logger.error("listFile failed, " + JSON.stringify(err));
}
}
```
3. 运行结果、错误截图
![1735190700184.jpg](
https://forums-obs.openharmony.c ... dtp6pbdzko8dnd9.jpg
"1735190700184.jpg")
4. 我尝试过的解决方法和结果
没有使用ServiceExtensionAbility之前, 已经成功实现从U盘中读取内容, 但是添加ServiceExtensionAbility的功能逻辑后, 出现截图中的报错, 导致无法读取U盘的内容
5. 我想要达到的结果
在使用ServiceExtensionAbility的前提下, 通过fileAccess的方式可以正常的读取到U盘中的内容.
### 【运行环境】
硬件:
ROM版本:
DevEvoStudio版本:Build Version: 4.1.0.400, built on April 9, 2024
SDK版本:full-sdk 4.1.8.4
[/md]
欢迎光临 OpenHarmony开发者论坛 (https://forums.openharmony.cn/)
Powered by Discuz! X3.5