Android SD card read and write operation

Source: Internet
Author: User

 Packagecom.itheima.writesd;ImportJava.io.File;Importjava.io.FileNotFoundException;ImportJava.io.FileOutputStream;Importjava.io.IOException;Importandroid.app.Activity;ImportAndroid.os.Bundle;Importandroid.os.Environment;ImportAndroid.view.View;ImportAndroid.widget.Toast;/*** Write SD card must remember to add permissions*/ Public classMainactivityextendsActivity {@Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);    Setcontentview (R.layout.activity_main); }    /*** Simulate writing a video file to an SD card. */     Public voidClick (View view) {//Check if the status of the SD card is unplugged or is not available        if(Environment.getexternalstoragestate (). Equals (environment.media_mounted)) {File Sdfile /c11>= Environment.getexternalstoragedirectory ();//External storage space            LongSdsize =Sdfile.getfreespace (); if(Sdsize >5* 1024 * 1024) {File file=NewFile (Environment.getexternalstoragedirectory (),//root directory, mnt/sdcard/hlw.3gpSystem.currenttimemillis () + "hlw.3gp");//Empty File                Try{FileOutputStream fos=NewFileOutputStream (file);//file output stream to write files                    byte[] buffer =New byte[1024];//1K, empty data                     for(inti = 0; i < 5*1024; i++) {//empty data file with hlw.3gp of 5 mfos.write (buffer);                } fos.close (); } Catch(Exception e) {e.printstacktrace (); }            }Else{Toast.maketext ( This, "Insufficient SD card space", 0). Show (); }        } Else{Toast.maketext ( This, "SD card is unplugged or not available", 0). Show (); }}} manifest file:<uses-permission android:name= "Android.permission.WRITE_EXTERNAL_STORAGE"/><uses-permission android: Name= "Android.permission.READ_EXTERNAL_STORAGE"/>

Internal Storage directory:

/data/data/Package Name (application name)/files/data/data/Package name (application name)/cache

External storage directory (SD card):
mnt/sdcard/
 PackageCom.itheima.haker;ImportJava.io.BufferedReader;ImportJava.io.File;ImportJava.io.FileInputStream;Importjava.io.FileNotFoundException;Importjava.io.IOException;ImportJava.io.InputStreamReader;Importandroid.app.Activity;ImportAndroid.os.Build;ImportAndroid.os.Bundle;ImportAndroid.view.View;ImportAndroid.widget.Toast; Public classMainactivityextendsActivity {@Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);    Setcontentview (R.layout.activity_main); }     Public voidClick (View view) {Try{File File=NewFile ("/data/data/com.itheima.createfile/files/haha.txt");//Internal Storage DirectoryFileInputStream FIS =NewFileInputStream (file);//file input streamBufferedReader br =NewBufferedReader (NewInputStreamReader (FIS));//input stream for wrappingString line = Br.readline ();//read the first lineToast.maketext ( This, line, 0). Show (); } Catch(Exception e) {Toast.maketext ( This, "read failed", 0). Show ();//files in the phone's internal storage space by default is private, other applications are inaccessible, or can be changed to public. E.printstacktrace (); }    }}

Android SD card read and write operation

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.