Android uses SDcard to read files

Source: Internet
Author: User

Usually we need to store large files on the phone like audio, video and so on, previously learned to use file for storage (using file operation to store); Due to the small storage space of the phone itself, this time we need to store the files in the SDcard, and today we also learned a bit on Android The method of storing and using SDcard;

First, if you want to use SDcard for storage in your program, we have to set the following permissions on the Androidmanifset.xml file:

The permissions to add access to SDcard in Androidmanifest.xml are as follows:

  <!--Create and delete files in SDcard permissions--  <uses-permission android:name="android.permission.MOUNT_ Unmount_filesystems"/>  <!--write data to SDcard permissions--  <uses-permission android:name= " Android.permission.WRITE_EXTERNAL_STORAGE "/>   

Then we use SDcard to read and write, we use a few static methods under the Environment class.

1 : Getdatadirectory ()  gets to data in Androi directory    2: getdownloadcachedirectory ()  Get to downloaded   cache directory 3: getExternalStorageDirectory ()  4

for the state of the external settings in the Android system, we are more commonly used media_mounted (SDcard exists and can read and write) media_mounted_read_only (SDcard exist, can only be read operation) Of course there are other states that can be found in the documentation

5 : Getrootdirectory ()  gets to Android root path    6: isexternalstorageemulated () Returns a Boolean value that determines whether the   external setting is valid 7: isexternalstorageremovable ()  Returns a Boolean value that determines whether external settings can be removed

"Note" Above the method of red marking, we will often

<span style="color: #ff0000; ">environment.getexternalstoragestate (). Equals (environment.media_mounted)  This sentence in the code: we judge the state of SDcard,</span>

The following is a demo that implements SDcard for file read and write operations:

Package Com.jiangqq.sdcard;import java.io.file;import java.io.fileinputstream;import java.io.FileOutputStream; Import Android.app.activity;import android.content.context;import android.os.bundle;import android.os.Environment; Import Android.view.view;import Android.view.view.onclicklistener;import Android.widget.button;import Android.widget.edittext;import Android.widget.Toast; Public classSdcardactivity extends Activity {PrivateButton bt1, BT2; PrivateEditText Et1, Et2; Private StaticFinal String FILENAME ="Temp_file.txt"; @Override Public voidonCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);        Setcontentview (R.layout.main); BT1= (Button) This. Findviewbyid (R.ID.BT1); BT2= (Button) This. Findviewbyid (R.ID.BT2); Et1= (EditText) This. Findviewbyid (R.ID.ET1); Et2= (EditText) This. Findviewbyid (R.id.et2); Bt1.setonclicklistener (NewMysetonclicklistener ()); Bt2.setonclicklistener (NewMysetonclicklistener ()); }    Private classMysetonclicklistener implements Onclicklistener {@Override Public voidOnClick (View v) {File file=NewFile (Environment.getexternalstoragedirectory (), FILENAME); Switch(V.getid ()) { CaseR.ID.BT1://using SDcard write operations                if(Environment.getexternalstoragestate (). Equals (environment.media_mounted)) { Try{FileOutputStream fos=Newfileoutputstream (file);                        Fos.write (Et1.gettext (). toString (). GetBytes ());                        Fos.close (); Toast.maketext (sdcardactivity. This,"Write file succeeded", Toast.length_long). Show (); } Catch(Exception e) {toast.maketext (sdcardactivity. This,"failed to write to file", Toast.length_short). Show (); }                } Else {                    //at this point the sdcard does not exist or cannot be read or written.Toast.maketext (sdcardactivity. This,                            "at this point the sdcard does not exist or is unable to read and write", Toast.length_short). Show (); }                 Break;  CaseR.ID.BT2://using SDcard read operations                if(Environment.getexternalstoragestate (). Equals (environment.media_mounted)) { Try{FileInputStream InputStream=Newfileinputstream (file); byte[] B =New byte[Inputstream.available ()];                        Inputstream.read (b); Et2.settext (NewString (b)); Toast.maketext (sdcardactivity. This,"Read file succeeded", Toast.length_long). Show (); } Catch(Exception e) {toast.maketext (sdcardactivity. This,"Read failed", Toast.length_short). Show (); }                } Else {                    //at this point the sdcard does not exist or cannot be read or written.Toast.maketext (sdcardactivity. This,                            "at this point the sdcard does not exist or is unable to read and write", Toast.length_short). Show (); }                 Break; }        }    }}

The following effect:

Demo Download LINK here :

Android uses SDcard to read files

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.