Introduction to Android Development (18) file 18.2 Save to an external storage device (SD card)

Source: Internet
Author: User

The previous section describes how to store files to an internal device. Sometimes, you need to store files to an external storage device, such as an SD card. Because SD cards have greater storage space, they can also be easily shared with other users.

Use the example in the previous section to save the text entered by the user on the SD card and modify the OnClick () event. As follows:

public class Filesactivity extends activity {edittext TextBox;  
      
    static final int read_block_size = 100; /** called the activity 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 ();  
} public void Onclicksave (view view) {        String str = Textbox.gettext (). toString ();   
            try {//---SD card Storage---File sdcard = environment.getexternalstoragedirectory ();  
            File directory = new file (Sdcard.getabsolutepath () + "/myfiles");  
            Directory.mkdirs ();  
            File File = new file (directory, "Textfile.txt");  
      
            FileOutputStream fout = new FileOutputStream (file); 
                            /* FileOutputStream fout = openfileoutput ("Textfile.txt", 
            mode_world_readable);  
      
            * * OutputStreamWriter OSW = new OutputStreamWriter (fout);  
            ---Write the string to the file---osw.write (str);   
            Osw.flush ();  
      
            Osw.close (); ---display file saved message---toast.maketext (getbasecontext), 
                    "File saved successfully!", Toast.length_short). Show ();  
        ---Clears the edittext---textbox.settext ("");  
        catch (IOException IoE) {ioe.printstacktrace (); }  
    }  
      
}

In the code above, use the getExternalStorageDirectory () method to get the path to the SD card. Normally, return "/sdcard" above the real machine and return "/mnt/sdcard" on the emulator. However, do not attempt to write the path of the dead SD card, because the handset manufacturer may go to the SD card to specify a path. Therefore, be sure to use the getExternalStorageDirectory () method to obtain the path to the SD card.

Then, create a myfiles folder. Finally, save the file in this folder.

To load a file from an external device, modify the Onclickload () method:

public void Onclickload (view view) {try {//---SD Storage---File sdcard =  
            Environment.getexternalstoragedirectory ();  
            File directory = new file (Sdcard.getabsolutepath () + "/myfiles");  
            File File = new file (directory, "Textfile.txt");  
            FileInputStream fIn = new FileInputStream (file);  
      
            InputStreamReader ISR = new InputStreamReader (fIn); 
            /* FileInputStream fIn = openfileinput ("Textfile.txt"); 
            InputStreamReader ISR = new InputStreamReader (fIn);  
            * * char[] InputBuffer = new Char[read_block_size];  
      
            String s = "";  
            int charread;  
                while ((Charread = Isr.read (inputbuffer)) >0) {//---Convert the chars to a String---  
          String readString =              String.copyvalueof (inputbuffer, 0, Charread);  
      
                s + + readString;  
            InputBuffer = new Char[read_block_size]; //---Set the edittext to the text this has been//Read---textbox.settext (s)  
      
            ; Toast.maketext (Getbasecontext (), "File loaded successfully!", Toast.length_sho  
        RT). Show ();  
        catch (IOException IoE) {ioe.printstacktrace (); }  
      
    }

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.