Document directory
- Requirement Description:
- Package Resources
- 1. Read the resource files in the jar package through the AssetManager class
-
- Use the ClassLoader class to read resource files
-
Requirement Description:
During project development, You Need To package public resources into jar packages, and the subsequent apps directly import jar packages into the project to share public resources.
Package Resources
Package project resources, including class, assets, res
1. Read the resource files in the jar package through the AssetManager class
The AssetManager class provides an interface for reading files and xml files.
Note: The restriction is that the file to be read must be placed in the Assets folder.
The following is an example of reading resources.
Public static Drawable getAssertDrawable (Context context, String fileName) {try {InputStream inStream = context. getAssets (). open (fileName); return new BitmapDrawable (BitmapFactory. decodeStream (inStream);} catch (IOException e) {Log. e (LOG_TAG, "Assert" + fileName + "does not exist");} return null ;}
Use the ClassLoader class to read resource files
The classLoader class provides interfaces for reading resource paths and resources (in stream form.
The difference between the classLoader class and the AssetManager class is that the classLoader class can also read Resources in the res folder.
The following is a small example of ClassLoader reading resources.
BufferedImage image=ImageIO.read(ClassLoader.getSystemResourceAsStream(“/res/drawable/icon.png”));