Android game development-04-operate sdcard

Source: Internet
Author: User

It is very simple to put Code directly.

Package COM. example. demogame; import Java. io. file; import Java. io. fileinputstream; import android. app. activity; import android. OS. bundle; import android. view. view; import android. widget. button; import android. widget. edittext; import android. widget. toast; public class mainactivity extends activity {button but; // open the button reference @ overridepublic void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. activity_main); but = (button) findviewbyid (R. id. button01);. setonclicklistener (new view. onclicklistener () {public void onclick (view v) {string contentresult = loadcontentfromsdcard ("androidsummary.txt"); // call the file reading method to obtain the edittext etcontent = (edittext) findviewbyid (R. id. edittext01); // instantiate edittextetcontent. settext (contentresult) ;}});} Public String loadcontentfromsdcard (string filename) {// read the content from the SD card string content = NULL; // The SD card content string try {file F = new file ("/sdcard/" + filename); // the file to be read int length = (INT) F. length (); byte [] buff = new byte [length]; fileinputstream FD = new fileinputstream (f); FD. read (buff); // bytes from the input stream. length data is read into a byte array. close (); // close this input stream and release all system resources associated with this stream content = new string (buff, "UTF-8");} catch (exception e) {toast. maketext (this, "Sorry, no file found", toast. length_short ). show () ;}return content ;}}

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.