1、参照文档,在操作文件时,读取不到内容或出现程序闪退?
参照文档,进行文件写入和读取时,出现读取不到或闪退
export function createFile() {// 获取应用文件路径let context = getContext(this) as common.UIAbilityContext;let filesDir = context.filesDir;// 新建并打开文件let file = fs.openSync(filesDir + '/test.txt', fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE);// 写入一段内容至文件let content = "static
存储类指示编译器在程序的生命周期内保持局部变量的存在,而不需要在每次它进入和离开作用域时进行创建和销毁,因此使用static修饰局部变量可以在函数调用之间保持局部变量的值register 用于定义存储在寄存器中而不是RAM中的局部变量,不过需要注意,定义 ‘register’ 并不意味着变量将被存储在寄存器中,它意味着变量可能存储在寄存器中,这取决于硬件和实现的限制。";
let writeLen = fs.writeSync(file.fd, content);
console.info("The length of str is: " + writeLen);
// 从文件读取一段内容
let buf = new ArrayBuffer(4096);
let readLen = fs.readSync(file.fd, buf, { offset: 0 });
let s = String.fromCharCode.apply(null, new Uint8Array(buf.slice(0, readLen)));
let d = decodeURIComponent(escape(s));
console.info("the content of file: " + d);
// 关闭文件
fs.closeSync(file);
}
上面这段代码是直接拷贝文档的,就修改了下写入内容,在执行到 let d = decodeURIComponent(escape(s)); 程序直接闪退,每次都是。然后自己将文件操作方法写成了工具类
export function getFilesDir(): string {// 获取应用文件路径let context = getContext(this) as common.UIAbilityContext;return context.filesDir;
}
//<路径前缀>/<加密等级>/base/files/
//表示 APP 内部存储的缓存目录,可读写,但是随时可能被清楚,不保证持久性,一般用作下载临时目录或者缓存目录。
export function getCacheDir(): string {let context = getContext(this) as common.UIAbilityContext;return context.cacheDir;
}
//将内容写入内存中
export function writeFile(fileName: string, content: string) {// 新建并打开文件let file = fs.openSync(getFilesDir() + "/" + fileName, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE);// 写入一段内容至文件let writeLen = fs.writeSync(file.fd, content);console.info("The length of str is: " + writeLen);// 关闭文件fs.closeSync(file);
}
//从文件中读取内容
export function readFile(fileName: string) {// 打开文件let srcFile = fs.openSync(getFilesDir() + "/" + fileName, fs.OpenMode.READ_WRITE);// 读取源文件内容并写入至目的文件let bufSize = 4096;let readSize = 0;let buf = new ArrayBuffer(bufSize);let readLen = fs.readSync(srcFile.fd, buf, { offset: readSize });while (readLen > 0) {readSize += readLen;readLen = fs.readSync(srcFile.fd, buf, { offset: readSize });}let s = String.fromCodePoint.apply(null, new Uint8Array(buf));console.log("readFile=============s==" + s);let d = decodeURIComponent(escape(s));console.log("readFile=============d==" + d);// 关闭文件fs.closeSync(srcFile);
}
//以流的形式读取文件
export async function readIOFile(fileName: string) {// 打开文件流let inputStream = fs.createStreamSync(getFilesDir() + '/' + fileName, 'r+');// 以流的形式读取源文件内容并写入目的文件let bufSize = 4096;let readSize = 0;let buf = new ArrayBuffer(bufSize);let readLen = await inputStream.read(buf, { offset: readSize });readSize += readLen;while (readLen > 0) {readLen = await inputStream.read(buf, { offset: readSize });readSize += readLen;}let s = String.fromCodePoint.apply(null, new Uint8Array(buf));console.log("readFile=============s==" + s);let d = decodeURIComponent(escape(s));console.log("readFile=============d==" + d);// 关闭文件流inputStream.closeSync();
}
在执行问写入,再执行读取的时候,调用readFile方法,在执行到该方法let s = String.fromCodePoint.apply(null, new Uint8Array(buf)); s得到的一直是"",没有内容
文件读写可参考:https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/app-file-access-management-V5
2、HarmonyOS 获取和设置屏幕亮度方法?
是否有获取和设置屏幕亮度接口可用, @ohos.brightness (屏幕亮度)提示找不到该模块。
- 获取屏幕亮度:https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/js-apis-settings-V5
- 设置屏幕亮度:https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/js-apis-window-V5
退出页面时需要把屏幕设为之前的亮度,可理解为手机默认亮度,将数值设置为-1即为默认亮度。
3、调用系统扫一扫时未弹出授权提示框,是否规格如此?
默认界面扫码能力提供了默认的扫码界面,需要对系统相机权限进行预授权,调用接口时,无需再次申请相机权限但有隐私提示。参考链接:https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/scan-scanbarcode-V5
4、编码器的bufferQueue报错?
04-19 18:15:58.074 1611 5120 E C01401/Bufferqueue: [nodict]<514>CheckBufferConfig: width or height is greater than 0, now is w 0 h 0
04-19 18:15:58.074 1611 5120 E C01401/Bufferqueue: [nodict](HEncoderSurface) AllocBuffer: Failure [1669], Reason: Alloc failed, then <400 invalid arguments>
04-19 18:15:58.074 1611 5120 E C01401/Bufferqueue: [nodict](HEncoderSurface) RequestBuffer: Fail to alloc or map Buffer[0 0] ret: 40001000, id: 6919192313868
04-19 18:15:58.074 19365 19554 E C01401/Bufferqueue: [nodict]<115>NativeWindowRequestBuffer: API failed, please check RequestBuffer function ret:40001000, Queue Id:6919192313868
04-19 18:15:58.074 19365 19554 E A0FF00/Xhey:C:EglRenderContext: EglRenderContext::SwapBuffers: Failed to SwapBuffers on EGLSurface 1, error is EGL_SUCCESS.
代码按照:https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/video-encoding-V5描述的走Surface模式,通过opengl 走buffer模式,通过OH_VideoEncoder_GetSurface获得nativeWindow, 通过eglCreateWindowSurface创建surface ,之后和camera的纹理进行swap, 但是bufferQueue报错。
5、无网络时跳转wlan设置页面,一直报错暂无支持此类文件的应用?
设置页面目前已经不支持隐式跳转了
bundleName:'com.example.xxx.settings' a
bilityName:'com.example.xxx.settings.MainAbility',
uri:'application_info_entry', //application_settings
建议把uri改成wifi_entry