diff --git a/code/DocsSample/ResourceManagement/RawFile/README.md b/code/DocsSample/ResourceManagement/RawFile/README.md index 9cdb8e6e5d317625c5c520863300e3af6cddad40..5386077e2eb0ec91f585ad9aeebec656f33c4f47 100644 --- a/code/DocsSample/ResourceManagement/RawFile/README.md +++ b/code/DocsSample/ResourceManagement/RawFile/README.md @@ -2,13 +2,13 @@ ### 介绍 -本工程主要实现了[Rawfile开发指导](https://docs.openharmony.cn/pages/v5.0/zh-cn/application-dev/napi/rawfile-guidelines.md)中功能,开发者可以通过本项目了解如何使用Native Rawfile接口操作Rawfile目录和文件。功能包括文件列表遍历、文件打开、搜索、读取和关闭Rawfile。 +本工程主要实现了[Rawfile开发指导](https://gitee.com/openharmony/docs/blob/OpenHarmony-5.1.0-Release/zh-cn/application-dev/napi/rawfile-guidelines.md)中功能,开发者可以通过本项目了解如何使用Native Rawfile接口操作Rawfile目录和文件。功能包括文件列表遍历、文件打开、搜索、读取和关闭Rawfile。 ### 效果预览 -| 显示页面 | 点击后日志打印效果 | -| ------------------------------------------------------------ | ----------------------------------------------- | -| ![](screenshots/RawFile_Index1.png) | ![](screenshots/log.png) | +| 显示页面 | 点击Hello World后的页面 | 点击后日志打印效果 | +| ------------------------------------------------------------|-------------------------------------| ----------------------------------------------- | +| ![](screenshots/RawFile_Index1.png) | ![](screenshots/RawFile_Index2.png) | ![](screenshots/log.png) | ### 使用说明 @@ -41,8 +41,8 @@ RawFile/entry/src/ ### 具体实现 -- 想要使用Native Rawfile接口操作Rawfile目录和文件。功能包括文件列表遍历、文件打开、搜索、读取和关闭Rawfile,需要在CPP目录下的index.d.ts文件中声明应用侧函数getFileList()、getRawFileContent()、getRawFileDescriptor()。 -- 在hallo.cpp中实现getFileList()、getRawFileContent()、getRawFileDescriptor()的具体代码,实际操作逻辑。 +- 想要使用Native Rawfile接口操作Rawfile目录和文件。功能包括文件列表遍历、文件打开、搜索、读取和关闭Rawfile,需要在CPP目录下的index.d.ts文件中声明应用侧函数getFileList()、getRawFileContent()、getRawFileDescriptor()、isRawDir()。 +- 在hello.cpp中实现getFileList()、getRawFileContent()、getRawFileDescriptor()、isRawDir()的具体代码,实际操作逻辑。 - 在ets目录下的index.ets文件就可以通过import testNapi from "libentry.so";导入相关库,进行对上述函数的调用。 ### 相关权限 @@ -55,9 +55,9 @@ RawFile/entry/src/ ### 约束与限制 -1. 本示例仅支持标准系统上运行,支持设备:RK3568; -2. 本示例为Stage模型,支持API14版本SDK,版本号:5.0.2.58; -3. 本示例需要使用DevEco Studio Beta1(5.0.5.200)及以上版本才可编译运行; +1. 本示例仅支持标准系统上运行,支持设备:RK3568。 +2. 本示例为Stage模型,支持API 18版本SDK,版本号:5.1.0.108。 +3. 本示例需要使用DevEco Studio 5.0.4 Release(5.0.11.100)及以上版本才可编译运行。 ### 下载 diff --git a/code/DocsSample/ResourceManagement/RawFile/build-profile.json5 b/code/DocsSample/ResourceManagement/RawFile/build-profile.json5 index a8f4fc1fad6fc640d9448acb0a9b19a16787e030..c3a9aede5e2beca5db24bffad02245e7daaaff92 100644 --- a/code/DocsSample/ResourceManagement/RawFile/build-profile.json5 +++ b/code/DocsSample/ResourceManagement/RawFile/build-profile.json5 @@ -12,7 +12,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - + { "app": { "signingConfigs": [], @@ -21,8 +21,8 @@ "name": "default", "signingConfig": "default", "compatibleSdkVersion": 14, - "compileSdkVersion": 14, - "targetSdkVersion": 14, + "compileSdkVersion": 18, + "targetSdkVersion": 18, "runtimeOS": "OpenHarmony", "buildOption": { "externalNativeOptions": { diff --git a/code/DocsSample/ResourceManagement/RawFile/entry/src/main/cpp/hello.cpp b/code/DocsSample/ResourceManagement/RawFile/entry/src/main/cpp/hello.cpp index 1d58203ddddd2e86a246378862216efc561f0874..4c737f6d815440dfa496aa6f88e1871b7efbc708 100644 --- a/code/DocsSample/ResourceManagement/RawFile/entry/src/main/cpp/hello.cpp +++ b/code/DocsSample/ResourceManagement/RawFile/entry/src/main/cpp/hello.cpp @@ -13,26 +13,29 @@ * limitations under the License. */ +// [Start includes] #include #include #include #include -#include #include #include "napi/native_api.h" #include "rawfile/raw_file_manager.h" #include "rawfile/raw_file.h" #include "rawfile/raw_dir.h" #include "hilog/log.h" +// [End includes] +// [Start constants] const int GLOBAL_RESMGR = 0xFF00; const char *TAG = "[Sample_rawfile]"; +// [End constants] +// [Start example_get_file_list] // 示例一:获取rawfile文件列表 GetFileList static napi_value GetFileList(napi_env env, napi_callback_info info) { - OH_LOG_Print(LOG_APP, LOG_ERROR, GLOBAL_RESMGR, TAG, "NDKTest Begin"); - size_t requireArgc = 3; + OH_LOG_Print(LOG_APP, LOG_INFO, GLOBAL_RESMGR, TAG, "NDKTest GetFileList Begin"); size_t argc = 2; napi_value argv[2] = { nullptr }; // 获取参数信息 @@ -74,7 +77,9 @@ static napi_value GetFileList(napi_env env, napi_callback_info info) OH_ResourceManager_ReleaseNativeResourceManager(mNativeResMgr); return fileList; } +// [End example_get_file_list] +// [Start example_get_rawfile_content] // 示例二:获取rawfile文件内容 GetRawFileContent napi_value CreateJsArrayValue(napi_env env, std::unique_ptr &data, long length) { @@ -102,8 +107,7 @@ napi_value CreateJsArrayValue(napi_env env, std::unique_ptr &data, lo static napi_value GetRawFileContent(napi_env env, napi_callback_info info) { - OH_LOG_Print(LOG_APP, LOG_ERROR, GLOBAL_RESMGR, TAG, "GetFileContent Begin"); - size_t requireArgc = 3; + OH_LOG_Print(LOG_APP, LOG_INFO, GLOBAL_RESMGR, TAG, "GetFileContent Begin"); size_t argc = 2; napi_value argv[2] = { nullptr }; // 获取参数信息 @@ -119,7 +123,7 @@ static napi_value GetRawFileContent(napi_env env, napi_callback_info info) // 获取rawfile指针对象 RawFile *rawFile = OH_ResourceManager_OpenRawFile(mNativeResMgr, filename.c_str()); if (rawFile != nullptr) { - OH_LOG_Print(LOG_APP, LOG_ERROR, GLOBAL_RESMGR, TAG, "OH_ResourceManager_OpenRawFile success"); + OH_LOG_Print(LOG_APP, LOG_INFO, GLOBAL_RESMGR, TAG, "OH_ResourceManager_OpenRawFile success"); } // 获取rawfile大小并申请内存 long len = OH_ResourceManager_GetRawFileSize(rawFile); @@ -134,7 +138,9 @@ static napi_value GetRawFileContent(napi_env env, napi_callback_info info) // 转为js对象 return CreateJsArrayValue(env, data, len); } +// [End example_get_rawfile_content] +// [Start example_get_rawfile_descriptor] // 示例三:获取rawfile文件描述符 GetRawFileDescriptor // 定义一个函数,将RawFileDescriptor转为js对象 napi_value createJsFileDescriptor(napi_env env, RawFileDescriptor& descriptor) @@ -183,16 +189,13 @@ napi_value createJsFileDescriptor(napi_env env, RawFileDescriptor& descriptor) static napi_value GetRawFileDescriptor(napi_env env, napi_callback_info info) { - OH_LOG_Print(LOG_APP, LOG_ERROR, GLOBAL_RESMGR, TAG, "NDKTest GetRawFileDescriptor Begin"); - size_t requireArgc = 3; + OH_LOG_Print(LOG_APP, LOG_INFO, GLOBAL_RESMGR, TAG, "NDKTest GetRawFileDescriptor Begin"); size_t argc = 2; napi_value argv[2] = { nullptr }; // 获取参数信息 napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr); - - napi_valuetype valueType; - napi_typeof(env, argv[0], &valueType); - // 获取native的resourceManager对象 + + // argv[0]即为函数第一个参数Js资源对象,OH_ResourceManager_InitNativeResourceManager转为Native对象 NativeResourceManager *mNativeResMgr = OH_ResourceManager_InitNativeResourceManager(env, argv[0]); size_t strSize; char strBuf[256]; @@ -201,7 +204,7 @@ static napi_value GetRawFileDescriptor(napi_env env, napi_callback_info info) // 获取rawfile指针对象 RawFile *rawFile = OH_ResourceManager_OpenRawFile(mNativeResMgr, filename.c_str()); if (rawFile != nullptr) { - OH_LOG_Print(LOG_APP, LOG_ERROR, GLOBAL_RESMGR, TAG, "OH_ResourceManager_OpenRawFile success"); + OH_LOG_Print(LOG_APP, LOG_INFO, GLOBAL_RESMGR, TAG, "OH_ResourceManager_OpenRawFile success"); } // 获取rawfile的描述符RawFileDescriptor {fd, offset, length} RawFileDescriptor descriptor; @@ -212,7 +215,10 @@ static napi_value GetRawFileDescriptor(napi_env env, napi_callback_info info) // 转为js对象 return createJsFileDescriptor(env, descriptor); } +// [End example_get_rawfile_descriptor] +// [Start example_is_raw_dir] +// 示例四:判断路径是否是rawfile下的目录 IsRawDir napi_value CreateJsBool(napi_env env, bool &bValue) { napi_value jsValue = nullptr; @@ -224,21 +230,19 @@ napi_value CreateJsBool(napi_env env, bool &bValue) static napi_value IsRawDir(napi_env env, napi_callback_info info) { - OH_LOG_Print(LOG_APP, LOG_ERROR, GLOBAL_RESMGR, TAG, "NDKTest IsRawDir Begin"); - size_t requireArgc = 3; + OH_LOG_Print(LOG_APP, LOG_INFO, GLOBAL_RESMGR, TAG, "NDKTest IsRawDir Begin"); size_t argc = 2; napi_value argv[2] = { nullptr }; // 获取参数信息 napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr); - napi_valuetype valueType; - napi_typeof(env, argv[0], &valueType); - // 获取native的resourceManager对象 + // argv[0]即为函数第一个参数Js资源对象,OH_ResourceManager_InitNativeResourceManager转为Native对象 NativeResourceManager *mNativeResMgr = OH_ResourceManager_InitNativeResourceManager(env, argv[0]); - napi_valuetype valueType1; - napi_typeof(env, argv[1], &valueType); - if (valueType1 == napi_undefined || valueType1 == napi_null) { + napi_valuetype fileNameType; + napi_typeof(env, argv[1], &fileNameType); + if (fileNameType == napi_undefined || fileNameType == napi_null) { + OH_LOG_Print(LOG_APP, LOG_ERROR, GLOBAL_RESMGR, TAG, "NDKTest file name is null"); bool temp = false; return CreateJsBool(env, temp); } @@ -246,12 +250,14 @@ static napi_value IsRawDir(napi_env env, napi_callback_info info) char strBuf[256]; napi_get_value_string_utf8(env, argv[1], strBuf, sizeof(strBuf), &strSize); std::string filename(strBuf, strSize); - // 获取rawfile指针对象 - bool result = OH_ResourceManager_OpenRawFile(mNativeResMgr, filename.c_str()); + // 判断是否是rawfile下的目录 + bool result = OH_ResourceManager_IsRawDir(mNativeResMgr, filename.c_str()); OH_ResourceManager_ReleaseNativeResourceManager(mNativeResMgr); return CreateJsBool(env, result); } +// [End example_is_raw_dir] +// [Start module_registration] EXTERN_C_START static napi_value Init(napi_env env, napi_value exports) { @@ -266,6 +272,7 @@ static napi_value Init(napi_env env, napi_value exports) return exports; } EXTERN_C_END +// [End module_registration] static napi_module demoModule = { .nm_version = 1, @@ -281,4 +288,3 @@ extern "C" __attribute__((constructor)) void RegisterEntryModule(void) { napi_module_register(&demoModule); } - diff --git a/code/DocsSample/ResourceManagement/RawFile/entry/src/main/cpp/types/libentry/Index.d.ts b/code/DocsSample/ResourceManagement/RawFile/entry/src/main/cpp/types/libentry/Index.d.ts index cb9941fafa1ad4906db3449eb22bda50d415a516..ef75cb6c92bf68900057d76754f4787d25b1d0a7 100644 --- a/code/DocsSample/ResourceManagement/RawFile/entry/src/main/cpp/types/libentry/Index.d.ts +++ b/code/DocsSample/ResourceManagement/RawFile/entry/src/main/cpp/types/libentry/Index.d.ts @@ -17,4 +17,4 @@ import resourceManager from '@ohos.resourceManager'; export const getFileList: (resmgr: resourceManager.ResourceManager, path: string) => Array; export const getRawFileContent: (resmgr: resourceManager.ResourceManager, path: string) => Uint8Array; export const getRawFileDescriptor: (resmgr: resourceManager.ResourceManager, path: string) => resourceManager.RawFileDescriptor; -export const isRawDir: (resmgr: resourceManager.ResourceManager, path: string) => Boolean; \ No newline at end of file +export const isRawDir: (resmgr: resourceManager.ResourceManager, path: string) => boolean; \ No newline at end of file diff --git a/code/DocsSample/ResourceManagement/RawFile/entry/src/main/ets/pages/Index.ets b/code/DocsSample/ResourceManagement/RawFile/entry/src/main/ets/pages/Index.ets index 7488f398e15d8bde8ad42ea9f7e2c7fc0846d8a5..58cec65633fb8f0e7c0550c315ba9872e6a2c31b 100644 --- a/code/DocsSample/ResourceManagement/RawFile/entry/src/main/ets/pages/Index.ets +++ b/code/DocsSample/ResourceManagement/RawFile/entry/src/main/ets/pages/Index.ets @@ -12,32 +12,21 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - -import hilog from '@ohos.hilog'; -import testNapi from 'libentry.so'; // 导入so - -class RawFileDescriptor { - fd: number; - offset: number; - length: number; - - constructor(fd: number, offset: number, length: number) { - this.fd = fd; - this.offset = offset; - this.length = length; - } -} +// [Start native_rawfile_guide_sample] +import { util } from '@kit.ArkTS'; +import { resourceManager } from '@kit.LocalizationKit'; +import testNapi from 'libentry.so'; // 导入so @Entry @Component struct Index { @State message: string = 'Hello World'; - private resmgr = getContext().resourceManager; // 获取本应用包的资源对象 - @State rawfilelistMsg: string = 'FileList = '; + private resMgr = this.getUIContext().getHostContext()?.resourceManager; // 获取本应用包的资源对象 + @State rawfileListMsg: string = 'FileList = '; @State retMsg: string = 'isRawDir = '; - @State rawfileContetMsg: string = 'RawFileContent = '; + @State rawfileContentMsg: string = 'RawFileContent = '; @State rawfileDescriptorMsg: string = 'RawFileDescriptor.length = '; - + build() { Row() { Column() { @@ -46,32 +35,38 @@ struct Index { .fontSize(30) .fontWeight(FontWeight.Bold) .onClick(async () => { - hilog.isLoggable(0x0000, 'testTag', hilog.LogLevel.INFO); - console.log('rawfilelist'); - let rawfilelist = testNapi.getFileList(this.resmgr, ''); // 传入资源对象,以及访问的rawfile文件夹名称 - console.log('rawfilelist:' + rawfilelist); - this.rawfilelistMsg = 'FileList = ' + rawfilelist; + // 传入资源管理对象,以及访问的rawfile文件夹名称 + let rawFileList: Array = testNapi.getFileList(this.resMgr, ''); + this.rawfileListMsg = 'FileList = ' + rawFileList; + console.log(this.rawfileListMsg); - let ret = testNapi.isRawDir(this.resmgr, 'rawfile1.txt'); - console.log('isRawDir:' + ret); + let ret: boolean = testNapi.isRawDir(this.resMgr, 'subrawfile'); this.retMsg = 'isRawDir = ' + ret; + console.log(this.retMsg); - let rawfileContet = testNapi.getRawFileContent(this.resmgr, 'rawfile1.txt'); - console.log('rawfileContet:' + rawfileContet); - this.rawfileContetMsg = 'RawFileContent = ' + rawfileContet; + // 传入资源管理对象,以及访问的rawfile文件夹名称 + let rawfileArray: Uint8Array = testNapi.getRawFileContent(this.resMgr, 'rawfile1.txt'); + // 将Uint8Array转为字符串 + let textDecoder: util.TextDecoder = new util.TextDecoder(); + let rawfileContent: string = textDecoder.decodeToString(rawfileArray); + this.rawfileContentMsg = 'RawFileContent = ' + rawfileContent; + console.log(this.rawfileContentMsg); - let rawfileDescriptor = (testNapi.getRawFileDescriptor(this.resmgr, 'rawfile1.txt') as RawFileDescriptor); - console.log('RawFileDescriptor:' + rawfileDescriptor.fd, rawfileDescriptor.offset, - rawfileDescriptor.length); + // 传入资源管理对象,以及访问的rawfile文件名称 + let rawfileDescriptor: resourceManager.RawFileDescriptor = + testNapi.getRawFileDescriptor(this.resMgr, 'rawfile1.txt'); this.rawfileDescriptorMsg = 'RawFileDescriptor.length = ' + rawfileDescriptor.length; + console.log(this.rawfileDescriptorMsg); }) - Text(this.rawfilelistMsg).id('get_file_list').fontSize(30); + Text(this.rawfileListMsg).id('get_file_list').fontSize(30); Text(this.retMsg).id('is_raw_dir').fontSize(30); - Text(this.rawfileContetMsg).id('get_raw_file_content').fontSize(30); + Text(this.rawfileContentMsg).id('get_raw_file_content').fontSize(30); Text(this.rawfileDescriptorMsg).id('get_raw_file_descriptor').fontSize(30); } .width('100%') } .height('100%') } -} \ No newline at end of file +} + +// [End native_rawfile_guide_sample] \ No newline at end of file diff --git a/code/DocsSample/ResourceManagement/RawFile/entry/src/ohosTest/ets/test/Ability.test.ets b/code/DocsSample/ResourceManagement/RawFile/entry/src/ohosTest/ets/test/Ability.test.ets index 34f0ff6a886904137a5273bab059f59c70fdec8b..0b271712f59d7d61fc909ac1eea18ca9a57672bf 100644 --- a/code/DocsSample/ResourceManagement/RawFile/entry/src/ohosTest/ets/test/Ability.test.ets +++ b/code/DocsSample/ResourceManagement/RawFile/entry/src/ohosTest/ets/test/Ability.test.ets @@ -15,7 +15,7 @@ import { hilog } from '@kit.PerformanceAnalysisKit'; import { describe, expect, it } from '@ohos/hypium'; -import { UIAbility } from '@kit.AbilityKit'; +import { UIAbility, Want } from '@kit.AbilityKit'; import { abilityDelegatorRegistry, Driver, ON } from '@kit.TestKit'; const TAG = '[Sample_rawfile]'; @@ -29,7 +29,7 @@ function sleep(time: number) { export default function abilityTest() { describe('RawFileTest', () => { -it('StartAbility_001', 0, async (done: Function) => { + it('StartAbility_001', 0, async (done: Function) => { console.info("uitest: StartAbility_001 begin"); const want: Want = { bundleName: BUNDLE, @@ -42,7 +42,7 @@ it('StartAbility_001', 0, async (done: Function) => { done(); console.info("uitest: StartAbility_001 end"); }); - + it(BUNDLE + 'clickTest_001', 0, async (done: Function) => { hilog.info(DOMAIN, TAG, BUNDLE + 'clickTest begin'); let driver = Driver.create(); @@ -79,7 +79,7 @@ it('StartAbility_001', 0, async (done: Function) => { let driver = Driver.create(); await driver.delayMs(1000); let fileContentText = await driver.findComponent(ON.id('get_raw_file_content')); - expect(await fileContentText.getText() === 'RawFileContent = 115,100,103,100,102,103').assertTrue(); + expect(await fileContentText.getText() === 'RawFileContent = sdgdfg').assertTrue(); hilog.info(DOMAIN, TAG, BUNDLE + 'GetRawFileContentTest end'); done(); }) diff --git a/code/DocsSample/ResourceManagement/RawFile/screenshots/RawFile_Index2.png b/code/DocsSample/ResourceManagement/RawFile/screenshots/RawFile_Index2.png index 0256928aa0ae69a6b7fde3038baadfd2aec1afee..af4629bdf9a5502f7b65efdfa94b278882bee580 100644 Binary files a/code/DocsSample/ResourceManagement/RawFile/screenshots/RawFile_Index2.png and b/code/DocsSample/ResourceManagement/RawFile/screenshots/RawFile_Index2.png differ diff --git a/code/DocsSample/ResourceManagement/RawFile/screenshots/log.png b/code/DocsSample/ResourceManagement/RawFile/screenshots/log.png index 5f130f90a927dc0c50dbf44aa07aa64c74b3d36e..bb6ae558718334d8c2e07c2eab3c41dd770ca6fa 100644 Binary files a/code/DocsSample/ResourceManagement/RawFile/screenshots/log.png and b/code/DocsSample/ResourceManagement/RawFile/screenshots/log.png differ