1. 程式人生 > >Android 載入其他Apk中的資原始檔

Android 載入其他Apk中的資原始檔

public Drawable loadApkResources(String apkPath) {
try {
//獲取apk的資源 最終都要通過AssetManager 獲取, getAssets() 獲取的AssetManager是獲取的本身Apk
//獲取其他Apk的資源需要例項化一個AssetManager,並把該AssetManager的載入路徑修改為被 載入的Apk的路徑
AssetManager assetMAnager = AssetManager.class.newInstance();
AssetManager.class.getDeclaredMethod("addAssetPath"
, String.class).invoke(assetMAnager, apkPath);
Resources resources = new Resources(assetMAnager, getResources().getDisplayMetrics(), getResources().getConfiguration());
Drawable drawable = resources.getDrawable(resources.getIdentifier("abc", "drawable", "com.mz.testapk"));
return drawable;
} catch
(Exception e) {
e.printStackTrace();
return null;
}
}
被載入的Apk的包名為com.mz.testapk ,drawable下有一張名為abc.png圖片