(android插件)獲取jar包中的資源總結

需求說明:
       項目開發過程中,需要將公用資源打包成jar包,後面的app直接將jar包導入項目,實現公用資源共享。

資源打包
     對項目資源打包,包含瞭class,assets,res

 

 

1 通過AssetManager類讀取jar包中的資源文件
 AssetManager類提供瞭讀取文件,讀取xml文件的接口

註:限制條件是 讀取的文件必須放到Assets文件夾中

 

 

下面是讀取資源的示例
 
    public static Drawable getAssertDrawable(Context context,String fileName){
        try {
            InputStream inStream=context.getAssets().open(fileName);
            return new BitmapDrawable(BitmapFactory.decodeStream(inStream));
        } catch (IOException e) {
            Log.e(LOG_TAG, "Assert中"+fileName+"不存在");
        }
        return null;
    }
 
 
使用ClassLoader類讀取資源文件
classLoader類提供瞭讀取資源路徑,和資源(流形式)的接口。
classLoader類和AssetManager類的不同點是classLoader類也可以讀取res文件夾中的資源
 

 

 

下面是ClassLoader讀取資源的小例子

  BufferedImage image=ImageIO.read(ClassLoader.getSystemResourceAsStream(“/res/drawable/icon.png”));
 

 

摘自 徐文兵的IT博客

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *