下面列出了android.content.res.Resources#getAssets() 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private Drawable getRoundIcon(Context context,String packageName, int iconDpi) {
mPackageManager = context.getPackageManager();
try {
Resources resourcesForApplication = mPackageManager.getResourcesForApplication(packageName);
AssetManager assets = resourcesForApplication.getAssets();
XmlResourceParser parseXml = assets.openXmlResourceParser("AndroidManifest.xml");
int eventType;
while ((eventType = parseXml.nextToken()) != XmlPullParser.END_DOCUMENT)
if (eventType == XmlPullParser.START_TAG && parseXml.getName().equals("application"))
for (int i = 0; i < parseXml.getAttributeCount(); i++)
if (parseXml.getAttributeName(i).equals("roundIcon"))
return resourcesForApplication.getDrawableForDensity(Integer.parseInt(parseXml.getAttributeValue(i).substring(1)), iconDpi, context.getTheme());
parseXml.close();
}
catch (Exception ex) {
ex.printStackTrace();
}
return null;
}
private static Resources getManagedProvisioningPackageResources(final Context context) {
try {
final Resources self_resources = context.getResources();
final Context target_context = context.createPackageContext(getManagedProvisioningPackageName(context), 0);
final Resources target_resources = target_context.getResources();
return new Resources(target_resources.getAssets(), target_resources.getDisplayMetrics(), target_resources.getConfiguration()) {
@NonNull @Override public String[] getStringArray(final int id) throws NotFoundException {
final String entry_name = self_resources.getResourceEntryName(id);
final int target_res_id = target_resources.getIdentifier(entry_name, "array", target_context.getPackageName());
if (target_res_id == 0) return new String[0]; // Return empty array instead of throwing NotFoundException.
return target_resources.getStringArray(target_res_id);
}
};
} catch (final NameNotFoundException e) {
return context.getResources(); // Fall-back to self, with default resource values.
}
}
/** Read a properties file from /assets. Returns null if it does not exist. */
public static Properties getProperties(String name, Context context) {
Resources resources = context.getResources();
AssetManager assetManager = resources.getAssets();
// Read from the /assets directory
try {
InputStream inputStream = assetManager.open(name);
Properties properties = new Properties();
properties.load(inputStream);
return properties;
} catch (IOException e) {
Log.i("ChatSecure", "no chatsecure.properties available");
return null;
}
}
@Override
public AssetManager getAssets() {
Resources resources = Stark.get().getResources();
if (resources != null) {
return resources.getAssets();
}
return super.getAssets();
}
/**
* 获取皮肤包资源{@link Resources}.
*
* @param skinPkgPath sdcard中皮肤包路径.
* @return
*/
@Nullable
public Resources getSkinResources(String skinPkgPath) {
try {
PackageInfo packageInfo = mAppContext.getPackageManager().getPackageArchiveInfo(skinPkgPath, 0);
packageInfo.applicationInfo.sourceDir = skinPkgPath;
packageInfo.applicationInfo.publicSourceDir = skinPkgPath;
Resources res = mAppContext.getPackageManager().getResourcesForApplication(packageInfo.applicationInfo);
Resources superRes = mAppContext.getResources();
return new Resources(res.getAssets(), superRes.getDisplayMetrics(), superRes.getConfiguration());
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* 获取皮肤包资源{@link Resources}.
*
* @param skinPkgPath sdcard中皮肤包路径.
* @return
*/
@Nullable
public Resources getSkinResources(String skinPkgPath) {
try {
PackageInfo packageInfo = mAppContext.getPackageManager().getPackageArchiveInfo(skinPkgPath, 0);
packageInfo.applicationInfo.sourceDir = skinPkgPath;
packageInfo.applicationInfo.publicSourceDir = skinPkgPath;
Resources res = mAppContext.getPackageManager().getResourcesForApplication(packageInfo.applicationInfo);
Resources superRes = mAppContext.getResources();
return new Resources(res.getAssets(), superRes.getDisplayMetrics(), superRes.getConfiguration());
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* Constructor
*
* @param mContext the application context
* @param sl the {@link SupportedLanguage}
*/
public SaiyResources(@NonNull final Context mContext, @NonNull final SupportedLanguage sl) {
this.mContext = mContext;
final Resources resources = this.mContext.getResources();
this.assetManager = resources.getAssets();
this.metrics = resources.getDisplayMetrics();
this.configuration = new Configuration(resources.getConfiguration());
this.targetLocale = sl.getLocale();
}
private EdgeEffectResources(Resources res, int color)
{
super(res.getAssets(), res.getDisplayMetrics(), res.getConfiguration());
mColorMatrixFilter = new ColorMatrixColorFilter(new ColorMatrix(new float[ ] {
0, 0, 0, 0, Color.red(color),
0, 0, 0, 0, Color.green(color),
0, 0, 0, 0, Color.blue(color),
0, 0, 0, 1, 0,
}));
}
@Override
public Resources getResources() {
if (mWrappedResources == null) {
Resources r = super.getResources();
mWrappedResources = new WrappedResources(
r.getAssets(), r.getDisplayMetrics(), r.getConfiguration()) {};
}
return mWrappedResources;
}
public AccentResources(Context c, Resources resources, int color, int colorDark, int colorActionBar) {
super(resources.getAssets(), resources.getDisplayMetrics(), resources.getConfiguration());
mContext = c;
mExplicitColor = color;
mExplicitColorDark = colorDark;
mExplicitColorActionBar = colorActionBar;
}
/**
* Internal method to load config
*
* @param context
* The Android Activity context
*
*/
private void loadConfig(Context ctx) throws Exception {
SocialAuthConfig config = new SocialAuthConfig();
Resources resources = ctx.getResources();
AssetManager assetManager = resources.getAssets();
InputStream inputStream = null;
boolean fileExist = false;
// Check oauth_consumer.properties file exist
try {
inputStream = assetManager.open("oauth_consumer.properties");
fileExist = true;
} catch (Exception e) {
fileExist = false;
Log.d("SocialAuthAdapter", "oauth_consumer.properties not found");
}
if (fileExist) {
// Add keys from oauth_consumers file. loadConfig() method
// is removed
config.load(inputStream);
socialAuthManager.setSocialAuthConfig(config);
} else {
// Add user keys if outh_consumers file not exists
for (String key : authMap.keySet()) {
config.addProviderConfig(key, authMap.get(key));
}
socialAuthManager.setSocialAuthConfig(config);
}
}
public AccentResources(Context c, Resources resources) {
super(resources.getAssets(), resources.getDisplayMetrics(), resources.getConfiguration());
mContext = c;
mExplicitColor = 0;
mExplicitColorDark = 0;
mExplicitColorActionBar = 0;
}
public AccentResources(Context c, Resources resources, int color) {
super(resources.getAssets(), resources.getDisplayMetrics(), resources.getConfiguration());
mContext = c;
mExplicitColor = color;
mExplicitColorDark = 0;
mExplicitColorActionBar = 0;
}
/**
* 创建插件的Resource {@link ResourcesProxy},通过此Resource对象
* 插件可以访问主工程和插件的资源
*/
private void createPluginResource() {
PluginDebugLog.runtimeLog(TAG, "createPluginResource for " + mPluginPackageName);
PackageManager pm = mHostContext.getPackageManager();
AssetManager am = null;
try {
Class<?>[] paramTypes = new Class[]{String.class};
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
// Android 5.0以下系统方法创建的AssetManager不支持扩展资源表,始终new出来
am = AssetManager.class.newInstance();
ReflectionUtils.on(am).call("addAssetPath", sMethods, paramTypes, mPluginPath);
} else {
// Android 5.0以上使用PackageManager的公开方法创建, 避免反射
Resources resources = pm.getResourcesForApplication(mPluginPackageInfo.getApplicationInfo());
am = resources.getAssets();
}
boolean shouldAddHostRes = !mPluginPackageInfo.isIndividualMode() && mPluginPackageInfo.isResourceNeedMerge();
if (shouldAddHostRes) {
// 添加宿主的资源到插件的AssetManager
ReflectionUtils.on(am).call("addAssetPath", sMethods, paramTypes,
mHostContext.getApplicationInfo().sourceDir);
PluginDebugLog.runtimeLog(TAG, "--- Resource merging into plugin @ " + mPluginPackageInfo.getPackageName());
}
// 添加系统Webview资源, Android L+
if (mPluginPackageInfo.isNeedAddWebviewResource()) {
addWebviewAssetPath(am);
}
mPluginAssetManager = am;
} catch (Exception e) {
ErrorUtil.throwErrorIfNeed(e);
String errMsg = "create plugin resources failed: " + e.getMessage();
PluginManager.deliver(mHostContext, false, mPluginPackageName, ErrorType.ERROR_PLUGIN_INIT_RESOURCES, errMsg);
}
Configuration config = new Configuration();
config.setTo(mHostResource.getConfiguration());
if (mPluginPackageInfo.isIndividualMode()) {
// 独立插件包,不依赖宿主的Resource
mPluginResource = new Resources(mPluginAssetManager, mHostResource.getDisplayMetrics(),
config);
} else {
mPluginResource = new ResourcesProxy(mPluginAssetManager, mHostResource.getDisplayMetrics(),
config, mHostResource, mPluginPackageName);
}
mPluginTheme = mPluginResource.newTheme();
mPluginTheme.setTo(mHostContext.getTheme());
mResourceTool = new ResourcesToolForPlugin(mHostContext);
}
private AssetManager createAssetManager(Context ctx) throws Throwable {
PackageManager pm = ctx.getPackageManager();
Resources res = pm.getResourcesForApplication(packageInfo.applicationInfo);
AssetManager am = res.getAssets();
return am;
}
@SuppressWarnings("deprecated")
ResourcesOverride(Resources parent, int overrideId, String overrideValue) {
super(parent.getAssets(), parent.getDisplayMetrics(), parent.getConfiguration());
mOverrideId = overrideId;
mOverrideValue = overrideValue;
}
public LeanplumResources(Resources base) {
super(base.getAssets(), base.getDisplayMetrics(), base.getConfiguration());
}
public SupportResources(Resources resources) {
super(resources.getAssets(), resources.getDisplayMetrics(), resources.getConfiguration());
}
public ContextWrapperEdgeEffect(Context context, int color) {
super(context);
mColor = color;
Resources resources = context.getResources();
mResourcesEdgeEffect = new ResourcesEdgeEffect(resources.getAssets(), resources.getDisplayMetrics(), resources.getConfiguration());
}
public ContextWrapperEdgeEffect(Context context, int color) {
super(context);
mColor = color;
Resources resources = context.getResources();
mResourcesEdgeEffect = new ResourcesEdgeEffect(resources.getAssets(), resources.getDisplayMetrics(), resources.getConfiguration());
}