由於Android app 安裝到手機上的位置是開發人員無法得知的(或許有辦法,不過目前我不知道),所以取得專案的檔案就必須透過其內部的機制達成

我們通常是透過 android.content.Context 取得(Context要另外寫一篇文章了),別擔心,我們常寫的Activity就是繼承Context,所以要取得Context是非常容易的事。

大致上要取得檔案都會跟Context 中的getResources()有關。getResources()會回傳Resources物件,利用這個物件可以得到很多我們需要access的資源。

1. 取得圖檔 : 

BitmapFactory.Options option = new BitmapFactory.Options();
Bitmap a1 = BitmapFactory.decodeResource(getResources(), R.drawable.a1, option);

a1就是取得的Bitmap檔,關於Bitmap的操作會在另一篇文章中說明。

2.取得XML檔:

XmlResourceParser parser = this.getResources().getXml(R.xml.aaa);

可以利用parser取得這個xml的資料,parser又是另一個故事了。

 

其他的部份再去Resources撈一撈應該會有很多的發現歐。

 

3. 利用 AssetManager 取得檔案

android.content.ContextWrapper 提供的 getAssets() 函數回傳 android.content.res.AssetManager

利用這個AssetManager 可以對asset資料夾中的檔案進行存取

自己試一試就知道了

 

 

arrow
arrow
    全站熱搜

    西西 發表在 痞客邦 留言(0) 人氣()