An example of this article describes the Android programming method for file read and write on SD card. Share to everyone for your reference, specific as follows:
A lot of knowledge can be extrapolate only after you have a real understanding of mastery. Java in the file operation and the Android SD card file operation, you think there is no difference, obviously no essential difference, if reluctantly said there, it is not enough for the road, but we in the actual use of the following points, or the problem will haunt you.
1, first want to the Android system SD card file operation needs to add permission to use:
Android system will not allow foreign programs to move their own memory, if there is no license, I am sorry, do not allow you to move my site, in my site to listen to me. In the configuration file inside Androidmanifest.xml add SD card Read and write data permissions.
Copy Code code as follows:
<uses-permission android:name= "Android.permission.WRITE_EXTERNAL_STORAGE"/>
2, get the absolute path of the file:
There are two ways of doing this.
Method 1: Use the/mnt/sdcard/+ file name directly
such as: final String file_name = "/mnt/sdcard/00000.h264";
Method 2: Obtain the SD path through the system-supplied method, then add the file name later . This method is a bit long-winded, temporarily did not feel the beauty of it, but here or listed, for future reference. See the example that follows.
3, get the file path after the operation , do not jump to FileInputStream, FileOutputStream, FileReader, filewriter the use of four classes. Refer to the previous "Java programming file read and write"
The following is a simple application code to write data to SD card files:
Method 1 Using the code:
public class Filetestactivity extends activity
{
final String file_name = "/mnt/sdcard/007.test";
public void OnCreate (Bundle savedinstancestate)
{
super.oncreate (savedinstancestate);
Setcontentview (r.layout.main);
Button btn = (button) Findviewbyid (r.id.button1);
Final byte[] buf = {0};
Btn.setonclicklistener (New Onclicklistener ()
{public
void OnClick (View arg0)
{
try
{
FileOutputStream fout = new FileOutputStream (file_name,
true);
Bufferedoutputstream bout = new Bufferedoutputstream (fout);
Bout.write (BUF);
Bout.flush ();
Bout.close ();
} catch (FileNotFoundException e)
{
e.printstacktrace ();
} catch (IOException e)
{
E.printstacktrace ();}}}
);
}
Method 2 using the instance code
public class Filetestactivity extends activity {final String file_name = "/007.test"; p
ublic void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.main);
Button btn = (button) Findviewbyid (R.id.button1);
Final byte[] buf = {0}; Btn.setonclicklistener (New Onclicklistener () {public void OnClick (View arg0) {if (environment.getexternalstoragestate
(). Equals (environment.media_mounted)) {File Sddir = Environment.getexternalstoragedirectory ();
System.out.println (Sddir);
FileOutputStream Fout; try {System.out.println (Sddir.getcanonicalpath () + file_name); fout = new FileOutputStream (Sddir.getcanonicalpath () +
file_name, True);
Bufferedoutputstream bout = new Bufferedoutputstream (fout);
Bout.write (BUF);
Bout.flush ();
Bout.close ();
catch (FileNotFoundException e) {e.printstacktrace ();} catch (IOException e) {e.printstacktrace ();}}
}
}); }
}
I hope this article will help you with your Android programming.