Getting Started with Android Development (18) file 18.4 using static resources

Source: Internet
Author: User

In addition to being able to create and use files at run time, you can also place files in the package at design time so that they can be used at runtime. For example, you want to pack some help files into a program that you can show them when they need it. In this case, you should put the file under the Res/raw folder.

To use this file in code, you need to invoke the Getresources () method of the activity and return a resources object. Then, use its Openrawresource () method to open the files in the Res/raw folder:

/** called when the "activity is" is a 

. * *
@Override public
void OnCreate (Bundle savedinstancestate) {  
    super.oncreate (savedinstancestate);  
    Setcontentview (r.layout.main);  
     
    TextBox = (edittext) Findviewbyid (R.ID.TXTTEXT1);  
         
       InputStream is = This.getresources (). Openrawresource (r.raw.textfile);  
       BufferedReader br = new BufferedReader (new InputStreamReader (IS));  
       String str = null;  
       try {  
           while (str = Br.readline ())!= null) {  
               toast.maketext (Getbasecontext (),   
                   str, toast.length_short) . Show ();  
           Is.close ();  
           Br.close ();  
       } catch (IOException e) {  
           e.printstacktrace ();  
       }  
     
}

Note that the ID of the textfile.txt is R.raw.textfile

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.