Android assets folder resource access, androidassets

Source: Internet
Author: User

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 = "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 ;}


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.