在android中資源檔案的存放和讀取

2021-10-10 02:36:33 字數 1331 閱讀 7148

在main檔案下建立乙個asset資料夾,將所需的檔案存放其中。

利用context.getassets()來獲取乙個assetmanager物件,該物件會與asset資料夾相關聯,通過open(filepath),可以獲取對應路徑的檔案的inputstream.

原始碼展示

context.getassets()

/**

* *note:implementations of this method should return

* an assetmanager instance that is consistent with the resources instance

* returned by . for example, they should share the

* same object.

* * @see #getresources()

*/public

abstract assetmanager getassets()

;

inputstream stream = manager.open(filepath);

/**

* open an asset using access_streaming mode. this provides access to

* files placed in to the "assets" directory.

* 使用access_streaming模式開啟資產。

這提供了訪問與應用程式**在一起的檔案作為資產-即,文

件放置在「資產」目錄中。

* * @param filename the name of the asset to open. this name can be hierarchical.

* * @see #open(string, int)

* @see #list

*/public

@nonnull inputstream open

(@nonnull string filename)

throws ioexception

將資源檔案的內容讀入到物件中

public

static string getjsonfromassets (context context, string filepath)

}catch

(ioexception e)

return stringbuilder.

tostring()

;}

JAR中資源檔案讀取

通過uri讀取檔案內容時。url url chinesematrix.class.getresource abc.dat file file new file url.touri 正常編譯時讀取沒問題。當程式打包jar讀取時系統提示 uri is not hierarchical 錯誤。無法讀取到檔...

關於Qt Creater中資源檔案和檔案路徑的記錄

想顯示一幅,建立了乙個資源檔案用來儲存路徑,使用qimage的load 方法載入 通過 png 引用 時,一直不能正常顯示,也看了好些帖子,有這麼幾種說法 a.在 在 qrc 即 qrc png 經測試,無效 b.看你的qt安裝目錄下是否有對應格式的dll檔案,我沒看到jpg png的dll檔案,以...

Qt中資源檔案的使用及意義

乙個應用程式中可能需要載入各種資源 如圖示 文字翻譯等 對於這些資源的管理,qt有專門的資源管理系統,它是乙個跨平台的資源機制,用於將程式執行時需要的資源以二進位制的形式儲存到可執行檔案內部。因此如果將用到的資源,比如圖示檔案等,放在資源檔案中,它將編譯到可執行檔案內部,這樣就無需擔心這些檔案的丟失...