Android Development Series (7): stores files in SDCard, androidsdcard

Source: Internet
Author: User

Android Development Series (7): stores files in SDCard, androidsdcard

Generally, the default storage space of our mobile app is in the memory of the mobile phone, but for large files such as video and audio, we can store it in SDCard:

The following code stores the file in SDCard (only core code is implemented)


First, we need to provide the SDCard permission to store the data in SDCard:

Add permissions to AndroidManifest. xml:

<! -- Create and delete a file in SDCard --> <uses-permission android: name = "android. permission. MOUNT_FORMAT_FILESYSTEMS"/> <! -- Write data permission to SDCard --> <uses-permission android: name = "android. permission. WRITE_EXTERNAL_STORAGE"/>


Then, add a method in FileService. java:

Public void saveToSDCard (String filename, String content) throws Exception {// Environment. the getExternalStorageDirectory () function is used to obtain the path File = new file (Environment. getExternalStorageDirectory (), filename); // open the output stream FileOutputStream outStream = new FileOutputStream (file); // write the content outStream. write (content. getBytes (); outStream. close ();}
Important: Environment. getExternalStorageDirectory () function


Finally, after clicking the "save" button, call the onClick () method of the button and add:

// A new FileService object. getApplicationContext () returns the application context. The life cycle is the entire application. Only when the application destroys FileService service = new FileService (getApplicationContext ()); try {// determine whether the SDCard exists and can be read/written, // Environment. getExternalStorageState () obtains the current status: // Environment. MEDIA_MOUNTED indicates the status of SDCard: SDCardif (Environment. getExternalStorageState (). equals (Environment. MEDIA_MOUNTED) {service. saveToSDCard (filename, content); // call the save () method to save the Toast file. makeText (getApplicationContext (), R. string. success, 1 ). show (); // call a Toast to present a "saved" message} else {Toast. makeText (getApplicationContext (), R. string. sdcarderror, 1 ). show (); // call a Toast to present a message with "SDCard does not exist or write protection"} catch (Exception e) {Toast. makeText (getApplicationContext (), R. string. fail, 1 ). show (); // call the Toast object to display a message e. printStackTrace ();}






Android development returns a file through a URL and saves it to the specified SD card path

Public static void writeFileData (String fileName, String message ){
Try {
FileOutputStream output = new FileOutputStream ("/sdcard/" + fileName );
Byte [] bytes = message. getBytes ();
Output. write (bytes );
Output. close ();
} Catch (Exception e ){
E. printStackTrace ();
}
}
You can specify the file name and write the file.

How does Android store files in SDCard?

For large files like videos, we can store them in SDCard. What does SDCard do? You can think of it as a mobile hard disk or a USB flash disk. The permission to access SDCard in AndroidManifest. xml is as follows: <! -Create and delete file permissions in SDCard-> <uses-permission android: name = "android. permission. MOUNT_UNMOUNT_FILESYSTEMS"/> <! -Write Data permission to SDCard-> <uses-permission android: name = "android. permission. WRITE_EXTERNAL_STORAGE "/> to store files in SDCard, the program must first determine whether the mobile phone is equipped with SDCard and can read and write data. Note: The SDCard must be accessed in AndroidManifest. if (Environment. getExternalStorageState (). equals (Environment. MEDIA_MOUNTED) {File sdCardDir = Environment. getExternalStorageDirectory (); // get the SDCard directory File saveFile = new File (sdCardDir, inititcast.txt "); FileOutputStream outStream = new FileOutputStream (saveFile); outStream. write ("test ". getBytes (); outStream. close ();} Environment. the getExternalStorageState () method is used to obtain SDCard status. If the mobile phone has an SDCard and can be read/written, the returned status is Environment. MEDIA_MOUNTED. Environment. the getExternalStorageDirectory () method is used to obtain the SDCard directory. To obtain the SDCard directory, you can also write: File sdCardDir = new File ("/sdcard "); // get the SDCard directory File saveFile = new File (sdCardDir, “itcast.txt "); // the above two sentences of code can be combined into one sentence: file saveFile = new File ("/sdcard/itcast.txt"); FileOutputStream outStream = new FileOutputStream (saveFile); outStream. write ("test ". getBytes (); outStream. close ();

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.