我整天都在努力解决这个问题而且没有成功。我基本上试图从另一个apk获取图像资源。
因此,如果com.example.app在res文件夹中有一个名为image1.png的图像,我希望com.example2.app能够访问该资源并将其放在imageview中。
我知道您必须使用PackageManager.getResourcesForApplication,但我仍未获得实际资源。
任何帮助都会很棒!
答案 0 :(得分:15)
想出来......
final String packName = "com.example2.app";
String mDrawableName = "app_icon";
try {
PackageManager manager = getPackageManager();
Resources mApk1Resources = manager.getResourcesForApplication(packName);
int mDrawableResID = mApk1Resources.getIdentifier(mDrawableName, "drawable",packName);
Drawable myDrawable = mApk1Resources.getDrawable( mDrawableResID );
if( myDrawable != null )
TEST.setBackgroundDrawable(myDrawable );
}
catch (NameNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
点击此处查看其他问题的更多说明! Share raw resource between apk's
答案 1 :(得分:4)
试试这个:
final String packName = "com.example.app ";
Resources resources;
try {
PackageManager manager = getPackageManager();
resources = manager.getResourcesForApplication(packName);
int resID = resources.getIdentifier("image1", "drawable", packName);
Log.d(TAG, "resID = " + resID);
Drawable image = getResources().getDrawable(resID);
Log.d(TAG, "resID = " + resID);
}
catch (NameNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}