HarmonyOS 关于使用getRawFileContentSync提示Rawfile path is invalid的问题
关于使用getRawFileContentSync提示Rawfile path is invalid的问题,从官方文档中无法定位出是路径给错了还是使用的方式不对。
错误日志:
07-19 11:37:22.088 13435-13435 C01e00/ResourceManagerJs E Failed to get rawfile by city_code.json
07-19 11:37:22.088 13435-13435 A0ff00/[QZD] E CityCodeHelper, getRawFileContentSync failed, error code: 9001005, message: Rawfile path is invalid.
- 1.
- 2.
具体代码如下:
import { JSON, util } from '@kit.ArkTS'
import { CityCodeBean } from '../bean/CityCodeBean';
import { CityCodeFileDataSource } from '../datasource/CityCodeFileDataSource';
import { common } from '@kit.AbilityKit'
import { BusinessError } from '@ohos.base';
import { log } from 'lib_common/src/main/ets/utils/Logger';
const TAG = 'CityCodeHelper'
class CityCodeHelper {
constructor() {
CityCodeFileDataSource.init(getContext(this) as common.ApplicationContext)
}
/*
* 此方法提供的CityCodeBean 类型绝对安全,无需再次转换
* */
getCityCodes():Array<CityCodeBean> {
const cityCodes = CityCodeFileDataSource.getCityCodes()
if (cityCodes == null) {
return this.getLocalCityCodeTransformer()
}else {
let safeCityCodes:Array<CityCodeBean> = []
cityCodes.forEach((item)=>{
safeCityCodes.push(this.transform(item))
})
return safeCityCodes
}
}
/*
* 保存网络上同步得到的城市码表数据
* @param cityCodes: 原始数据表弱类型对象,但是必须符合`CityCodeBean`的数据结构
* @param version:获取到的版本号
* */
saveCityCodes(cityCodes:Array<CityCodeBean>, version:number) {
CityCodeFileDataSource.saveCityCodes(cityCodes, version)
}
private getLocalCityCodeTransformer():Array<CityCodeBean> {
try {
let rawFile: Uint8Array = getContext(this).resourceManager.getRawFileContentSync('city_code.json');
let localCityCodeStr: string = util.TextDecoder.create("utf-8", { ignoreBOM: true }).decodeWithStream(rawFile);
let localCityCodes:Array<CityCodeBean> = []
const _cityCodes:Array<CityCodeBean> = JSON.parse(localCityCodeStr) as Array<CityCodeBean>
_cityCodes.forEach((item)=>{
localCityCodes.push(this.transform(item))
})
return localCityCodes
} catch (error) {
let code = (error as BusinessError).code;
let message = (error as BusinessError).message;
log.error(TAG, `getRawFileContentSync failed, error code: ${code}, message: ${message}.`)
return []
}
}
private transform(data:Object):CityCodeBean {
let original:CityCodeBean = data as CityCodeBean
let subs:Array<CityCodeBean> | null = []
if (Array.isArray(original.sub)) {
original.sub?.forEach((item)=>{
subs?.push(this.transform(item))
})
}else {
subs = null
}
return new CityCodeBean(
original.id,
original.name,
original.shortName,
original.type,
original.parentId,
original.levelType,
original.pinyin,
original.shortPinyin,
subs
)
}
}
export const cityCodeInstance = new CityCodeHelper()
- 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.
- 76.
- 77.
- 78.
- 79.
- 80.
- 81.
- 82.
- 83.
HarmonyOS
赞
收藏 0
回答 1
相关问题
通过getRawFileContentSync读取rawfile时一直报错:Rawfile path is invalid
2200浏览 • 1回复 待解决
HarmonyOS getRawFileContentSync读取rawfile下的文件返回空
891浏览 • 1回复 待解决
DevEco编译报错:Invalid JavaScript file path
4706浏览 • 0回复 待解决
HarmonyOS LoginWithHuaweiIDButton报错1001502003,提示Invalid parameter
1311浏览 • 1回复 待解决
HarmonyOS 通过$rawfile(path)获取rowfile下的资源,如何识别这个path实际文件存在与否
791浏览 • 1回复 待解决
HarmonyOS getRawFileContentSync 疑问
1064浏览 • 1回复 待解决
编译构建异常提示hvigor ERROR: Invalid version
4508浏览 • 1回复 待解决
关于WebView提示没有用户手势的问题
11317浏览 • 1回复 待解决
HarmonyOS 关于使用@BuilderParam的问题
1485浏览 • 1回复 待解决
HarmonyOS关于API使用的问题
1403浏览 • 1回复 待解决
使用RawFile
656浏览 • 1回复 待解决
关于import router from '@system.router'提示问题?
6075浏览 • 1回复 待解决
关于library提示模块名的问题ERROR有懂的吗?
2747浏览 • 1回复 待解决
HarmonyOS 关于Video无法播放rawfile视频资源
1019浏览 • 1回复 待解决
HarmonyOS关于异步Promise的使用问题
1380浏览 • 1回复 待解决
关于 SO 文件的使用问题
1101浏览 • 1回复 待解决
HarmonyOS window.setUIContent 参数path问题
639浏览 • 1回复 待解决
关于 Extend 使用问题
1063浏览 • 1回复 待解决
HarmonyOS关于使用fs.copyfile的问题
1156浏览 • 1回复 待解决
关于HarmonyOS webview中iFrame的使用问题
1179浏览 • 1回复 待解决
HarmonyOS 关于Iconfont如何使用问题?
970浏览 • 1回复 待解决
关于xml中include的使用问题
8068浏览 • 2回复 待解决
HarmonyOS app打包命令--signature-path和--certificate-path怎么使用?
800浏览 • 1回复 待解决
使用Native Rawfile接口操作Rawfile目录和文件
2539浏览 • 1回复 待解决
尝试下如下办法:getContext(this)整个context默认为entry的context,需要指定hsp的context,示例如下:
在context后加上createModuleContext(‘library’) ,library为module.json5中的name修改后:
参考文档:https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/application-dev-guide-V5?catalogVersion=V5