Android assets folder resource access, androidassets
1. Files in the assets folder are kept in the original file format.
2. Files in assets can only be read but cannot be written.
3. The resource files in the assets Directory do not automatically generate IDS in R. java. Therefore, the file path must be specified to read files in the assets Directory.
4. assets can have a directory structure, that is, a folder can be created under the assets Directory.
5. Read files in the assets Directory
AssetManager assetManager = getAssets() ;InputStream inputStream = assetManager.open( "fileName" ) ;
InputStream is = getResources().getAssets().open( "aa.txt" ) ;
6. Read the text in the assets Directory
String s = getAssetsString ("aa.txt");/*** read the String * @ param fileName * @ return */private String getAssetsString (String fileName) in the local file) {StringBuilder stringBuilder = new StringBuilder (); try {BufferedReader bf = new BufferedReader (new InputStreamReader (getAssets (). open (fileName), "UTF-8"); String line; while (line = bf. readLine ())! = Null) {stringBuilder. append (line) ;}} catch (IOException e) {e. printStackTrace () ;}return stringBuilder. toString ();}
7. Read images in the assets Directory
Bitmap bgImg = getImageFromAssetFile ("background.png");/*** read image from assets */private Bitmap getImageFromAssetsFile (String fileName) {Bitmap image = null; assetManager am = getResources (). getAssets (); try {InputStream is = am. open (fileName); image = BitmapFactory. decodeStream (is); is. close ();} catch (IOException e) {e. printStackTrace ();} return image ;}