Android 加载其他Apk中的资源文件

来源:互联网 发布:怎么发淘宝店铺链接 编辑:程序博客网 时间:2024/04/29 20:24
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图片
0 0
原创粉丝点击