• Home
Name Date Size #Lines LOC

..--

AppScope/06-May-2025-3532

entry/06-May-2025-877719

hvigor/06-May-2025-3836

screenshots/06-May-2025-

.gitignoreD06-May-2025133 1212

README_zh.mdD06-May-20253.9 KiB10577

build-profile.json5D06-May-20251.4 KiB6362

code-linter.json5D06-May-2025958 3534

hvigorfile.tsD06-May-2025839 225

oh-package.json5D06-May-2025809 2624

ohosTest.mdD06-May-2025563 117

README_zh.md

1# 应用文件访问(C++)
2
3## 介绍
4
5 本实例主要实现了使用C++的方式去访问应用沙箱目录下的文件,使用接口FileManagement_ErrCode OH_FileIO_GetFileLocation(char *uri, int uriLength, FileIO_FileLocation *location),接口的详细说明请参考:FileIO。主要实现的功能是:使用文件uri作为入参,调用C语言函数去访问应用文件,并确定文件的存储位置。该工程中展示的代码详细描述可查如下链接。
6
7- [应用文件访问(C/C++)](https://gitee.com/openharmony/docs/blob/OpenHarmony-5.0.1-Release/zh-cn/application-dev/file-management/native-fileio-guidelines.md)
8
9## 效果预览
10
11| 应用启动界面                                      | 文件访问结果                                    |
12| ------------------------------------------------- | ----------------------------------------------- |
13| <img src="./screenshots/start.jpg" width = "400"> | <img src="./screenshots/end.jpg" width = "400"> |
14
15使用说明:
16
171. 本应用主要使用napi的方式,使用C语言函数对应用的沙箱文件进行访问,可以在创建文件后向napi定义的函数传递文件的uri参数,通过uri对文件进行访问,并确定文件的存储文件是本地还是云端。
182. 在启动应用之后,按顺序点击界面上的两个按钮,先创建文件,在对文件进行访问,通过日的反馈知道访问的结果。
19
20## 工程目录
21
22```
23NDKAppFileSample
24├──entry/src/main
25|	├──cpp
26|	|	├──types
27|	|	|	├──libentry
28|	|	|	└──CMakeLists.txt        	// CMake脚本文件
29|	|	|	└──napi_init.cpp			// napi函数封装类
30|	├──ets
31|	|	├──commom
32|	|	|	├──Logger.ts				// 封装日志类
33|	|	├──entryability
34|	|	|	└──EntryAbility.ets         // 程序入口类
35|	|	├──entrybackupability
36|	|	|	└──EntryBackupAbility.ets
37|	|	├──fileFs
38|	|	|	└──fileFs.ets               // 文件创建函数封装
39|	|	└──pages                        // 页面文件
40|	|		└──Index.ets 				// 主界面
41|	├──resources						// 资源文件目录
42```
43
44## 具体实现
45
461. 在CMake脚本中链接动态库,即在CMakeLists.txt中添加以下lib。
47
48   ```
49   target_link_libraries(sample PUBLIC libohfileio.so)
50   ```
51
522. 添加头文件
53
54   ```
55   #include <filemanagement/fileio/oh_fileio.h>
56   ```
57
583. 调用OH_FileIO_GetFileLocation接口获取文件存储位置。示例代码如下所示:
59
60   ```c
61       void GetFileLocationExample() {
62           char *uri = "file://com.example.demo/data/storage/el2/base/files/test.txt";
63           FileIO_FileLocation location;
64           FileManagement_ErrCode ret = OH_FileIO_GetFileLocation(uri, strlen(uri), &location);
65           if (ret == 0) {
66               if (location == FileIO_FileLocation::LOCAL) {
67                   printf("This file is on local.");
68               } else if (location == FileIO_FileLocation::CLOUD) {
69                   printf("This file is on cloud.");
70               } else if (location == FileIO_FileLocation::LOCAL_AND_CLOUD) {
71                   printf("This file is both on local and cloud.");
72               }
73           } else {
74               printf("GetFileLocation failed, error code is %d", ret);
75           }
76       }
77   ```
78
79## 相关权限
80
8182
83## 依赖
84
85不涉及
86
87## 约束与限制
88
891.本示例仅支持标准系统上运行,支持设备:RK3568。
90
912.本示例为Stage模型,支持API14版本SDK,版本号:5.0.2.58,镜像版本号:OpenHarmony 5.0.2.58。
92
933.本示例需要使用DevEco Studio 5. 1Release (Build Version: 5.0.5.306, built on December 6, 2024)及以上版本才可编译运行。
94
95## 下载
96
97```
98git init
99git config core.sparsecheckout true
100echo code/DocsSample/CoreFile/NDKAppFileSample > .git/info/sparse-checkout
101git remote add origin https://gitee.com/openharmony/applications_app_samples.git
102git pull origin master
103```
104
105