Usually we need to store large files on the phone like audio, video and so on, previously learned to use file for storage (using the file operation to store); Since the storage space of the phone itself is small, we need to store the file in SDcard. Today I also learned a bit on Android for sdcard storage usage;
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 the data directory in the Androi
2:getdownloadcachedirectory () get to the downloaded cache directory
3:getexternalstoragedirectory () Gets the directory to the external storage generally refers to SDcard
4:getexternalstoragestate () Gets the current state of the external setting generally refers to SDcard,
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 () get to Android Root path
6:isexternalstorageemulated () returns a Boolean value to determine if 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
[Java]View Plaincopy
- <span style="color: #ff0000;" >environment.getexternalstoragestate (). Equals (environment.media_mounted) code in this sentence: We judge the state of SDcard, </span >
The following is a demo that implements SDcard for file read and write operations:
[Java]View Plaincopy
- 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 operation
- 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 file succeeded",
- Toast.length_long). Show ();
- } catch (Exception e) {
- Toast.maketext (sdcardactivity. This, "write file Failed",
- Toast.length_short). Show ();
- }
- } Else {
- //At this time SDcard does not exist or can not be read and write operations
- Toast.maketext (sdcardactivity. This,
- "At this time SDcard does not exist or can not read and write operations", Toast.length_short). Show ();
- }
- Break ;
- Case R.ID.BT2://using 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 file succeeded",
- Toast.length_long). Show ();
- } catch (Exception e) {
- Toast.maketext (sdcardactivity. This, "read failed",
- Toast.length_short). Show ();
- }
- } Else {
- //At this time SDcard does not exist or can not be read and write operations
- Toast.maketext (sdcardactivity. This,
- "At this time SDcard does not exist or can not read and write operations", Toast.length_short). Show ();
- }
- Break ;
- }
- }
- }
- }
The following effect:
Demo Download Link: http://download.csdn.net/detail/jiangqq781931404/4017475
SDcard to read the file