Assets folder Resource Access

Source: Internet
Author: User

All files in the assets folder are in the original file format. You need to use AssetManager to read files in the form of byte streams.

1. Call getAssets () in the Activity to obtain the AssetManager reference.
2. Use AssetManager's open (String fileName, int accessMode) method to specify the file to be read and the access mode to obtain the input stream InputStream.
3. Use inputStream of the open file to read the file. After reading the file, remember inputStream. close ().
4. Call AssetManager. close () to close AssetManager.
Note that files from Resources and Assets can only be read but cannot be written.
Read from Raw files as follows:


[Java] public String getFromRaw (){
Try {
InputStreamReader inputReader = new InputStreamReader (getResources (). openRawResource (R. raw. test1 ));
BufferedReader bufReader = new BufferedReader (inputReader );
String line = "";
String Result = "";
While (line = bufReader. readLine ())! = Null)
Result + = line;
Return Result;
} Catch (Exception e ){
E. printStackTrace ();
}
}
Public String getFromRaw (){
Try {
InputStreamReader inputReader = new InputStreamReader (getResources (). openRawResource (R. raw. test1 ));
BufferedReader bufReader = new BufferedReader (inputReader );
String line = "";
String Result = "";
While (line = bufReader. readLine ())! = Null)
Result + = line;
Return Result;
} Catch (Exception e ){
E. printStackTrace ();
}
}
 

Read from assets directly


[Java] public String getFromAssets (String fileName ){
Try {
InputStreamReader inputReader = new InputStreamReader (getResources (). getAssets (). open (fileName ));
BufferedReader bufReader = new BufferedReader (inputReader );
String line = "";
String Result = "";
While (line = bufReader. readLine ())! = Null)
Result + = line;
Return Result;
} Catch (Exception e ){
E. printStackTrace ();
}
}
Public String getFromAssets (String fileName ){
Try {
InputStreamReader inputReader = new InputStreamReader (getResources (). getAssets (). open (fileName ));
BufferedReader bufReader = new BufferedReader (inputReader );
String line = "";
String Result = "";
While (line = bufReader. readLine ())! = Null)
Result + = line;
Return Result;
} Catch (Exception e ){
E. printStackTrace ();
}
}
 

Of course, if you want to get the memory stream, you can also directly return the memory stream!

 

From agods-footprint

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: info-contact@alibabacloud.com 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.