The right to join the access SDcard in Androidmanifest.xml is as follows:
<!--Create and delete file permissions in SDcard-->
The code is as follows |
Copy Code |
<uses-permission android:name= "Android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/> |
<!--Write data permissions to SDcard-->
The code is as follows |
Copy Code |
<uses-permission android:name= "Android.permission.WRITE_EXTERNAL_STORAGE"/> |
Then we use SDcard to read and write, we use a few static methods below the Environment class
1:getdatadirectory () gets to the data directory in Androi
2:getdownloadcachedirectory () gets to the downloaded cache directory
3:getexternalstoragedirectory () gets to the externally stored directory generally refers to the SDcard
4:getexternalstoragestate () Gets the current state of the external setting generally refers to the sdcard,
For the state of the external setting in the Android system, we should use the media_mounted (SDcard exists and can read and write) media_mounted_read_only (SDcard exists, can only read) Of course, there are other states that can be found in the document
5:getrootdirectory () get to the Android root path
6:isexternalstorageemulated () returns a Boolean value to determine whether the external setting is valid
7:isexternalstorageremovable () returns a Boolean value that determines whether external settings can be removed
"Note" The method of red mark above, we will use
The code is as follows |
Copy Code |
<span style= "color: #ff0000;" >environment.getexternalstoragestate (). Equals (environment.media_mounted) |
This sentence in the code: we judge the state of the SDcard,</span>
Here is a demo that implements SDcard for file read and write operations:
The code is as follows |
Copy Code |
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 class Sdcardactivity extends activity { Private Button bt1, BT2; Private EditText Et1, Et2; private static final String FILENAME = "Temp_file.txt"; @Override public void OnCreate (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 (New Mysetonclicklistener ()); Bt2.setonclicklistener (New Mysetonclicklistener ()); } Private class Mysetonclicklistener implements Onclicklistener { @Override public void OnClick (View v) { file File = new file ( Environment.getexternalstoragedirectory (), FILENAME); switch (V.getid ()) { Case r.id.bt1://Use SDcard write if (Environment.getexternalstoragestate (). Equals ( environment.media_mounted)) { try { FileOutputStream fos = new FileOutputStream (file); Fos.write (Et1.gettext (). toString (). GetBytes ()); Fos.close (); Toast.maketext (Sdcardactivity.this, "Write to file successfully", Toast.length_long). Show (); catch (Exception e) { Toast.maketext (Sdcardactivity.this, "Write file Failed", Toast.length_short). Show (); } } else { The sdcard does not exist or cannot be read or write at this time Toast.maketext (Sdcardactivity.this, "At this point sdcard does not exist or cannot be read or write," Toast.length_short. Show (); } Break Case r.id.bt2://Use SDcard read operation if (Environment.getexternalstoragestate (). Equals ( environment.media_mounted)) { try { FileInputStream InputStream = new FileInputStream (file); Byte[] B = new byte[inputstream.available ()]; Inputstream.read (b); Et2.settext (New String (b)); Toast.maketext (Sdcardactivity.this, "read the file successfully", Toast.length_long). Show (); catch (Exception e) { Toast.maketext (Sdcardactivity.this, "Read failed", Toast.length_short). Show (); } } else { The sdcard does not exist or cannot be read or write at this time Toast.maketext (Sdcardactivity.this, "At this point sdcard does not exist or cannot be read or write," Toast.length_short. Show (); } Break } } } }
|