下面列出了android.content.res.Resources#getResourceName() 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Gets the style attributes from the {@link Resources.Theme}. For debugging only.
*
* @param resources Resources to resolve attributes from.
* @param theme Theme to dump.
* @return a String array containing pairs of adjacent Theme attribute data: name followed by
* its value.
*
* @hide
*/
private static String[] getStyleAttributesDump(Resources resources, Resources.Theme theme) {
TypedValue outValue = new TypedValue();
String nullString = "null";
int i = 0;
int[] attributes = theme.getAllAttributes();
String[] data = new String[attributes.length * 2];
for (int attributeId : attributes) {
try {
data[i] = resources.getResourceName(attributeId);
data[i + 1] = theme.resolveAttribute(attributeId, outValue, true) ?
outValue.coerceToString().toString() : nullString;
i += 2;
// attempt to replace reference data with its name
if (outValue.type == TypedValue.TYPE_REFERENCE) {
data[i - 1] = resources.getResourceName(outValue.resourceId);
}
} catch (Resources.NotFoundException e) {
// ignore resources we can't resolve
}
}
return data;
}
/**
* Look up resource name for a given resource ID.
*
* @return a simple resource name (e.g. "text_1") when {@code withType} is false, or with the
* type (e.g. "string/text_1").
*
* @hide
*/
@VisibleForTesting
public static String lookUpResourceName(@NonNull Resources res, int resId, boolean withType,
@NonNull String packageName) {
if (resId == 0) {
return null;
}
try {
final String fullName = res.getResourceName(resId);
if (ANDROID_PACKAGE_NAME.equals(getResourcePackageName(fullName))) {
// If it's a framework resource, the value won't change, so just return the ID
// value as a string.
return String.valueOf(resId);
}
return withType ? getResourceTypeAndEntryName(fullName)
: getResourceEntryName(fullName);
} catch (NotFoundException e) {
Log.e(TAG, "Resource name for ID=" + resId + " not found in package " + packageName
+ ". Resource IDs may change when the application is upgraded, and the system"
+ " may not be able to find the correct resource.");
return null;
}
}
static BitmapDrawable decodeSampledBitmapFromRes(Resources resources, @RawRes int resId,
int reqWidth, int reqHeight,
ImageCache cache, boolean isOpenLruCache) {
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
decodeStream(resources.openRawResource(resId), null, options);
options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
// String resourceName = resources.getResourceName(resId);
// if (Utils.hasHoneycomb()) {
BitmapDrawable bitmapFromCache;
if (isOpenLruCache) {
String resourceName = resources.getResourceName(resId);
bitmapFromCache = cache.getBitmapFromCache(resourceName);
if (bitmapFromCache == null) {
// if (Utils.hasHoneycomb()) {
bitmapFromCache = readBitmapFromRes(resources, resId, cache, options);
cache.addBitmap(resourceName, bitmapFromCache);
}
} else {
bitmapFromCache = readBitmapFromRes(resources, resId, cache, options);
}
return bitmapFromCache;
}
public String nameFromId(Resources res, int id) {
String value = mIdToIdName.get(id);
if (value != null) {
return value;
}
try {
if (res == null) {
res = AnalysysUtil.getContext().getResources();
}
String resName = res.getResourceName(id);
if (TextUtils.isEmpty(resName)) {
value = null;
} else {
String[] arr = resName.split("/");
if (arr.length != 2) {
value = resName;
} else if (arr[0].equals(mPkgName + ":id")) {
value = arr[1];
} else {
value = resName.replaceAll("id/", "");
}
}
mIdToIdName.put(id, value);
return value;
} catch (Throwable ignore) {
ExceptionUtil.exceptionThrow(ignore);
}
return null;
}
/**
* Return whether any users are currently set to use the wallpaper
* with the given resource ID. That is, their wallpaper has been
* set through {@link #setResource(int)} with the same resource id.
*/
public boolean hasResourceWallpaper(@RawRes int resid) {
if (sGlobals.mService == null) {
Log.w(TAG, "WallpaperService not running");
throw new RuntimeException(new DeadSystemException());
}
try {
Resources resources = mContext.getResources();
String name = "res:" + resources.getResourceName(resid);
return sGlobals.mService.hasNamedWallpaper(name);
} catch (RemoteException e) {
throw e.rethrowFromSystemServer();
}
}
/**
* Inflate a NavGraph from the given XML resource id.
*
* @param graphResId
* @return
*/
@SuppressLint("ResourceType")
@NonNull
public NavGraph inflate(@NavigationRes int graphResId) {
Resources res = mContext.getResources();
XmlResourceParser parser = res.getXml(graphResId);
final AttributeSet attrs = Xml.asAttributeSet(parser);
try {
int type;
while ((type = parser.next()) != XmlPullParser.START_TAG
&& type != XmlPullParser.END_DOCUMENT) {
// Empty loop
}
if (type != XmlPullParser.START_TAG) {
throw new XmlPullParserException("No start tag found");
}
String rootElement = parser.getName();
NavDestination destination = inflate(res, parser, attrs);
if (!(destination instanceof NavGraph)) {
throw new IllegalArgumentException("Root element <" + rootElement + ">"
+ " did not inflate into a NavGraph");
}
return (NavGraph) destination;
} catch (Exception e) {
throw new RuntimeException("Exception inflating "
+ res.getResourceName(graphResId) + " line "
+ parser.getLineNumber(), e);
} finally {
parser.close();
}
}
private static String getResourceName(Resources resources, int resId) {
try {
return resources.getResourceName(resId);
}
catch (Resources.NotFoundException ignored) {
// Just take hex representation of string
return Integer.toHexString(resId);
}
}