Android resource management tool Resources and AssetManager

Preface   :

       Android project often need to reference the resource at run time. Use  Resources to get  res a variety of device-related resource directory. Use  AssetManager to get the  assets resource directory.

       Resources include system resources, engineering resources, third-party resources, plug-ins and other resources, divided into two categories:

  •        Storage can be compiled resource file, compiled, the system will automatically generate the next ID res directory R.java in the resource file, so that access to resources is relatively simple, R.id.filenam e can by calling in the program.    
  •        The original resource files are stored under the assets directory, because the system will not be compiled at compile time assets under resource file, so we can not R.id.filename way to access them. What I can not pass the absolute path of the resource to access them? Because apk after installation will be placed under /data/app/**.apk directory, assets are bound in the apk, apk exists in the form, and not extract it to / data / data / YourApp directory down, so we can not directly to get the absolute path of the assets, because they simply do not exist independently

 similarities res / raw and assets of:  
    file 1 after both the directory stored in the intact package will apk package, will not be translated into binary.

differences res / raw and assets of:   
   1.res / RAW files are mapped to R.java file when accessed directly using the resource ID that is R.id.filename; file assets folder will not be mapped to R.java, the time of the visit need AssetManager class.    
   2.res / raw not have a directory structure, the directory structure can assets (in which directory folders can build)   
   3. Read res / raw file resource in the acquired input stream by:

      InputStream is=getResources().openRawResource(R.id.filename);

      Read the file resources under assets, takes an input stream in the following ways:

      InputStream is =getResources()..getAssets().open("filename");  

 

       No matter what kind of resources, and ultimately be able to get through an entrance, and the entrance is the Resources object. Resources object describes android resource files, such as under the res android engineering, asset catalog and other resources in addition to the .class file, we can say that all the places related to access to resources, you can use Resources to obtain. In fact, we often use directly in reading the resource directory code Assetmanager asset to the acquisition stream data object directly, but also belong to a property Resources AssetManager object, simultaneously pointing to the same object.

 

Resources Analysis:

Several important member properties 1.Resources object:

static Resources mSystem = null ; // mSystem as a static object that represents the default resource management. 
Final Object mAccessLock = new new Object ();
 Final the Configuration mTmpConfig = new new the Configuration (); 
the TypedValue mTmpValue = new new the TypedValue ();
 Final AssetManager mAssets; // mAssets point to the default instance, create an initialization process mAsset is done in C ++ layer of. 
Private  Final the Configuration mConfiguration = new new the Configuration ();
 Final DisplayMetrics mMetrics = new new DisplayMetrics ();
 Private NativePluralRules mPluralRule;

 

 1 private Resources() {
 2         mAssets = AssetManager.getSystem();
 3         // NOTE: Intentionally leaving this uninitialized (all values set
 4         // to zero), so that anyone who tries to do something that requires
 5         // metrics will get a very wrong value.
 6         mConfiguration.setToDefaults();
 7         mMetrics.setToDefaults();
 8         updateConfiguration(null, null);
 9         mAssets.ensureStringBlocks();
10     }

       When we develop their own applications, it is to have the resources they need to be placed under the res file in the project directory folder and asset folder. When compiling the project time, resources, and source files will be packaged inside the apk. When running an application, the default path AssetManager Resources in the pointing file apk, if an ordinary application, generally apk file is placed in the data / app directory, the system apk application package on the system / app.

       So when you start the application, Resources by Assetmanager, AssetManager then find apk resources based on a set path, you can find the correct resources of their own.

        Since AssetManager access to resources is done in accordance with point path, in theory, we change the path AssetManager read, you can specify load their own resources and, indeed, such a design. So if we let AssetManager point Apk resource path that you set, you can accomplish the objectives of the resources extracted apk. Some implementations principle of dynamic theme package mainly by AssetsManager.addAssetPath(String path) creating a corresponding theme package This interface  Resources objects to achieve.

 

Demo:

New Class ResourcesManager for resource management, wherein the method comprises an important attribute of:

 1     AssetManager mAssetManager = null;
 2     Resources mResources = null;
 3     LayoutInflater mLayoutInflater = null;
 4     Theme mTheme = null;
 5     ClassLoader mClassLoader = null;
 6     //ResApk.apk  资源文件
 7     String packageName = "com.example.testapk";
 8     String libPath = Environment.getExternalStorageDirectory().toString()
 9             + File.separator + "ResApk.apk";
10  
11     protected void initAssetManager() {
12         try {
13             AssetManager assetManager = AssetManager.class.newInstance();
14             Method addAssetPath = assetManager.getClass().getMethod(
15                     "addAssetPath", String.class);
16             addAssetPath.invoke(assetManager, libPath);
17             mAssetManager = assetManager;
18         } catch (Exception e) {
19             e.printStackTrace();
20         }
21         Resources superRes = super.getResources();
22         mResources = new Resources(mAssetManager, superRes.getDisplayMetrics(),
23                 superRes.getConfiguration());
24     }

mResources ResApk.apk points to the inside of the resources, you can get by the name of the corresponding resources.

 1 public int getDrawableId(String imgName) {
 2         return mResources.getIdentifier(imgName, "drawable", apkPackageName);
 3     }
 4  
 5     /**
 6      * 获取图片资源
 7      * 
 8      * @param imgName
 9      * @return drawable
10      */
11     public Drawable getResApkDrawable(String imgName) {
12         return mResources.getDrawable(getDrawableId(imgName));
13     }

Of particular note is that because each application context object is not the same, we should always remember that if you are not using your own resource file to be used with caution and context-sensitive function method.

     Original Reference blog link: https://blog.csdn.net/MeteorLuoyidong/article/details/49530839

                                     https://www.cnblogs.com/jpfss/p/9876370.html

 

Guess you like

Origin www.cnblogs.com/jiani/p/11654487.html