Summary of id and url methods for obtaining resources based on Android

  • 2021-08-12 03:44:42
  • OfStack

1. Obtain id of various resources in android project;

1.1 string types such as the following:


<< string name= " OK " >>  Client request successful  << / string>>

//string In resources   The name is OK Adj.  id  Acquisition method 
int strId = getResources().getIdentifier("OK", "string", getPackageName());

1.2 color types such as:


<< color name= " colorPrimary " >>#3F51B5 << /color>>

//color In resources  id  Get 
int colorId = getResources().getIdentifier("colorPrimary", "color", getPackageName()); 

1.3 dimens types such as:


<< dimen name= " horizontal_margin " >16dp<< /dimen>>

//deimens Resources  id  Get 
int dimenId = getResources().getIdentifier("horizontal_margin", "dimen", getPackageName());

1.4 Under the mipmap folder or under the drawable folder: for example, ic_launcher. png


// mipmap The name under the folder is ic_launcher Of the picture of id   
//mipmap Resources id
int mipmapId = getResources().getIdentifier("ic_launcher", "mipmap", getPackageName());

1.5 Layout file resources such as R.layout.activity_main


// Of the layout file id, For example: R.layout.activity_main    
// Layout resources id
int layoutId = getResources().getIdentifier("activity_main", "layout", getPackageName());

2. Obtain the resource id in Android system


// Get the resources in the system id android.R.drawable.ic_menu_share
int id = getResources().getIdentifier("ic_menu_share", "drawable", "android"); // Attention, finally 1 Parameters must be " android

3. Obtain the Uri path of Android engineering resources, 1 is generally under pictures, res or asset


//* Get Res Resource url ContentResolver.SCHEME_ANDROID_RESOURCE*/
Uri uri = Uri.parse("android.resource://" + getPackageName() + "/" + R.mipmap.ic_launcher);
Uri uri = Uri.parse("res:///" + R.mipmap.ic_launcher);
//* Get asset Resource url,ContentResolver.SCHEME_FILE*/
Uri assetUri = Uri.parse("file:///android_asset/" + "qq.png");

Related articles: