OpenHarmony北向應(yīng)用開發(fā)—將應(yīng)用資源目錄Rawfile中的文件推送到應(yīng)用沙箱
想了解更多關(guān)于開源的內(nèi)容,請(qǐng)?jiān)L問:
在應(yīng)用開發(fā)調(diào)試時(shí),可能需要向應(yīng)用沙箱下推送一些文件用于應(yīng)用訪問或者調(diào)試,本文介紹了如何放置在應(yīng)用資源目錄rawfile中的文件推送到應(yīng)用沙箱。并且在提供一個(gè)樣例Demo用于讀者老爺參考學(xué)習(xí)。
樣例demo下載鏈接:https://gitee.com/from-north-to-north/OpenHarmony_hap/tree/master/rawfile_to_sandbox。
筆者開發(fā)環(huán)境:(本文提供的樣例demo 一定得是以下IDE和SDK版本或者更高版本才能編譯運(yùn)行)。
- 開發(fā)板:潤和軟件DAYU200開發(fā)板
- OpenHarmony版本:OpenHarmony3.2 release
- IDE:DevEco Studio 3.1.0.400
- SDK:API9(3.2.11.9)
通過本文您將了解:
- 應(yīng)用資源resources目錄和應(yīng)用沙箱的概念。
- 將應(yīng)用資源目錄rawfile中的文件推送到應(yīng)用沙箱。
文章開始首先要熟悉兩個(gè)概念,OpenHarmony應(yīng)用開發(fā)中 應(yīng)用資源目錄中的rawfile目錄和應(yīng)用沙箱是什么?
1、應(yīng)用資源目錄中的rawfile目錄 是什么
OpenHarmony中應(yīng)用開發(fā)使用的各類資源文件會(huì)被放進(jìn)應(yīng)用資源目錄中,它在應(yīng)用源碼中長下面這個(gè)樣子。
應(yīng)用資源resources目錄包括三大類目錄,一類為base目錄,一類為限定詞目錄,還有一類就是rawfile目錄。
應(yīng)用資源目錄中的rawfile目錄特點(diǎn):
- 組織形式:支持創(chuàng)建多層子目錄,目錄名稱可以自定義,文件夾內(nèi)可以自由放置各類資源文件。rawfile目錄的文件不會(huì)根據(jù)設(shè)備狀態(tài)去匹配不同的資源。
- 編譯方式:目錄中的資源文件會(huì)被直接打包進(jìn)應(yīng)用,不經(jīng)過編譯,也不會(huì)被賦予資源文件ID。
- 引用方式:通過指定文件路徑和文件名來引用。
參考鏈接:
https://gitee.com/openharmony/docs/blob/master/zh-cn/application-dev/quick-start/resource-categories-and-access.md。
2、應(yīng)用沙箱 是什么
應(yīng)用沙箱是一種以安全防護(hù)為目的的隔離機(jī)制,避免數(shù)據(jù)受到惡意路徑穿越訪問。在這種沙箱的保護(hù)機(jī)制下,應(yīng)用可見的目錄范圍即為“應(yīng)用沙箱目錄”。
OpenHarmony提供應(yīng)用沙箱機(jī)制,增加目錄可見性數(shù)據(jù)訪問防線,減少了應(yīng)用數(shù)據(jù)和用戶隱私信息泄露,建立了更加嚴(yán)格安全的應(yīng)用沙盒隔離能力。
詳細(xì)可參考:https://gitee.com/openharmony/docs/blob/master/zh-cn/application-dev/file-management/app-sandbox-directory.md。
應(yīng)用沙箱實(shí)現(xiàn)源碼:
https://gitee.com/openharmony/startup_appspawn/blob/master/util/src/sandbox_utils.cpp。
https://gitee.com/openharmony/startup_appspawn。
3、向應(yīng)用沙箱推送文件
參考資料:https://gitee.com/openharmony/docs/blob/master/zh-cn/application-dev/file-management/send-file-to-app-sandbox.md。
開發(fā)者在應(yīng)用開發(fā)調(diào)試時(shí),可能需要向應(yīng)用沙箱下推送一些文件以期望在應(yīng)用內(nèi)訪問或測(cè)試,此時(shí)有兩種方式:
- 第一種:可以通過DevEco Studio向應(yīng)用安裝路徑中放入目標(biāo)文件,詳見應(yīng)用安裝資源訪問。
- 第二種:在具備設(shè)備環(huán)境時(shí),可以使用另一種更為靈活的方式,通過hdc工具來向設(shè)備中應(yīng)用沙箱路徑推送文件。即本文介紹的內(nèi)容。
本文介紹的就是第一種方式——通過DevEco Studio向應(yīng)用安裝路徑中放入目標(biāo)文件,也就是在應(yīng)用資源目錄rawfile目錄中放入文件,然后將其推送至沙箱路徑。
(1)樣例demo實(shí)現(xiàn)步驟:
新建應(yīng)用,創(chuàng)建資源文件,在rawfile下面新建文件。筆者在樣例中新建的的是input.txt。
獲取context上下文,src/main/ets/entryability/EntryAbility.ts
onWindowStageCreate(windowStage: window.WindowStage) {
// Main window is created, set main page for this ability
hilog.info(0x0000, 'testTag', '%{public}s', 'Ability
onWindowStageCreate');
globalThis.abilityContext = this.context
//用全局對(duì)象獲取context類的接口
globalThis.context = this.context
...
}
將應(yīng)用資源目錄rawfile中的文件推送到應(yīng)用沙箱,實(shí)際上是通過沙箱與公共路徑間文件的復(fù)制來完成的,使用到的API有g(shù)etRawFd ,還使用到了一些文件管理相關(guān)的api。
import fs from '@ohos.file.fs';
@Entry
@Component
struct Index {
@State message: string = 'rawfile_copy_to_sandbox'
//沙箱路徑
dir:string = globalThis.abilityContext.filesDir + "/";
//從rawfile中讀取input.txt文件,在log中顯示
private async readfile_from_rawfile() {
try {
let uint8Array = await
globalThis.context.resourceManager.getRawFileContent('rawfile/input.txt');
let str = String.fromCharCode.apply(null, new
Uint8Array(uint8Array.buffer));
console.info("[rawfile_copy_to_sandbox]
———————————————————————————————————————————————————————");
console.info("[rawfile_copy_to_sandbox] rawfile中的input.txt內(nèi)容為" + str);
} catch (error) {
console.info("[rawfile_copy_to_sandbox]
———————————————————————————————————————————————————————");
console.info("[rawfile_copy_to_sandbox] rawfile中的input.txt內(nèi)容讀取失敗" +
error);
}
}
//用來拷貝rawfile文件中的input.txt到應(yīng)用沙箱目錄下
private async copy_rawfile__to_sandbox() {
try {
let file = this.dir+"input.txt";
let sss = fs.createStreamSync(file, "w");//沒有會(huì)創(chuàng)建一個(gè)空的input.txt
sss.closeSync();
//獲取rawfile下input.txt
globalThis.context.resourceManager.getRawFileDescriptor('rawfile/input.txt',(error,
value) => {
if (error != null) { //getRawFileDescriptor運(yùn)行失敗
console.info("[rawfile_copy_to_sandbox]
———————————————————————————————————————————————————————");
console.log("[rawfile_copy_to_sandbox] getRawFileDescriptor api 運(yùn)行失敗:
${error.code}, message: ${error.message}.");
console.log("[rawfile_copy_to_sandbox] 未能成功將rawfile下的input.txt文件拷貝到應(yīng)用沙箱下
");
} else { //getRawFileDescriptor運(yùn)行成功
let fd = value.fd;
fs.copyFileSync(fd, file);
console.info("[rawfile_copy_to_sandbox]
———————————————————————————————————————————————————————");
console.log("[rawfile_copy_to_sandbox] getRawFileDescriptor api 運(yùn)行成功");
console.log("[rawfile_copy_to_sandbox] 成功將rawfile下的input.txt文件拷貝到應(yīng)用沙箱下");
}
});
} catch (error) {
console.info("[rawfile_copy_to_sandbox]
———————————————————————————————————————————————————————");
console.info("[rawfile_copy_to_sandbox] getRawFileDescriptor api 運(yùn)行失敗" +
error);
console.log("[rawfile_copy_to_sandbox]
未能成功將rawfile下的input.txt文件拷貝到應(yīng)用沙箱下");
}
}
build() {
Row() {
Column() {
Button(this.message)
.fontSize(25)
.margin({top:0})
.fontWeight(FontWeight.Normal)
.backgroundColor(Color.Green) //設(shè)置按鈕顏色
.onClick(() => {
console.info("[rawfile_copy_to_sandbox] 沙箱路徑是"+ this.dir);
//用來復(fù)制rawfile文件中的input.txt到沙箱目錄下
//調(diào)用的是私有的自定義的copy_rawfile__to_sandbox方法
this.copy_rawfile__to_sandbox();
this.readfile_from_rawfile();
})
}
.width('100%')
}
.height('100%')
}
}
(2)樣例實(shí)現(xiàn)效果
日志顯示:日志顯示rawfile目錄下的input.txt成功推送到/data/app/el2/100/base/com.sandbox.rawfile_to_sandbox/haps/entry/files/沙箱路徑下。
進(jìn)入設(shè)備shell終端。