Home>

Save the file using the activity's openfileoutput () method,The files are stored on the phone space,The storage space of general mobile phones is not very large.It's okay to store some small files,If i want to store large files like videos,Is not feasible.For large files like videos,We can store it in sdcard. What does sdcard do?You can think of it as a removable hard drive or a USB flash drive.

To use sdcard in the simulator, you need to create an sdcard first (of course it is not a real sdcard, it is just an image file). Create sdcard can be created when eclipse creates the simulator,Can also be created using the dos command,As follows:In the dos window, enter the tools directory of the android sdk installation path, and enter the following command to create a sdcard with a capacity of 2g.Recommended.img:

mksdcard 2048m d:\ androidtool \ sdcard.img

Note:To access the sdcard in the program, you need to apply for permission to access the sdcard.

Add the permission to access sdcard in androidmanifest.xml as follows:

<uses-permission android:name="android.permission.mount_unmount_filesystems" />

<!-Write data permission to sdcard->

<uses-permission android:name="android.permission.write_external_storage" />

To store files in sdcard,The program must first determine whether the mobile phone has an sdcard and can read and write.

Note:To access the sdcard, you must add the permission to access the sdcard in androidmanifest.xml

if (environment.getexternalstoragestate (). equals (environment.media_mounted)) {
  file sdcarddir=environment.getexternalstoragedirectory ();//Get sdcard directory
  file savefile=new file (sdcarddir, "ljq.txt");
  fileoutputstream outstream=new fileoutputstream (savefile);
  outstream.write ("abc" .getbytes ());
  outstream.close ();
}

The environment.getexternalstoragestate () method is used to get the status of the sdcard. If the phone has an sdcard and can read and write

Then the state returned by the method is equal to environment.media_mounted.

The environment.getexternalstoragedirectory () method is used to get the directory of the sdcard. Of course, to get the directory of the sdcard, you can also write:

file sdcarddir=new file ("/mnt/sdcard");//Get the sdcard directory

file savefile=new file (sdcarddir, "ljq.txt");

//The above two sentences can be combined into one sentence:file savefile=new file ("/mnt/sdcard/ljq.txt");

fileoutputstream outstream=new fileoutputstream (savefile);

outstream.write ("abc" .getbytes ());

outstream.close ();

Case

main.xml layout file:

<?xml version="1.0" encoding="utf-8"?>
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:orientation="vertical"
 android:layout_width="fill_parent"
 android:layout_height="fill_parent"
 >
 <!-Relative layout->
 <relativelayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:orientation="vertical"
 android:layout_width="fill_parent"
 android:layout_height="wrap_content">
 <textview android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:text="@ string/filename"
  android:id="@ + id/filenamelable" />
 <edittext android:layout_width="250px"
  android:layout_height="wrap_content"
  android:layout_torightof="@ id/filenamelable"
  android:layout_aligntop="@ id/filenamelable"
  android:layout_marginleft="10px"
  android:text="ljq.txt"
  android:id="@ + id/filename" />
 </relativelayout>
 <textview android:layout_width="fill_parent"
 android:layout_height="wrap_content"
 android:text="@ string/content" />
 <edittext android:layout_width="fill_parent"
 android:layout_height="wrap_content"
 android:minlines="3"
 android:id="@ + id/content" />
 <relativelayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:orientation="vertical"
 android:layout_width="fill_parent"
 android:layout_height="wrap_content">
 <button android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:text="@ string/button"
  android:id="@ + id/button" />
 <button android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_torightof="@ id/button"
  android:layout_aligntop="@ id/button"
  android:layout_marginleft="10px"
  android:minlines="3"
  android:text="@ string/readbutton"
  android:id="@ + id/readbutton" />
 </relativelayout>
 <textview android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:id="@ + id/resultview" />
</linearlayout>

strings.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>
 <string name="hello">hello world, fileactivity!</string>
 <string name="app_name">Data Save</string>
 <string name="filename">file name</string>
 <string name="content">file content</string>
 <string name="button">save</string>
 <string name="readbutton">read content</string>
 <string name="error">Save failed</string>
 <string name="success">Save successfully</string>
</resources>

fileservice tool class:

package com.ljq.service;
import java.io.bytearrayoutputstream;
import java.io.inputstream;
import java.io.outputstream;
public class fileservice {
 /**
 * save data
 *
 * @param outputstream
 * @param content
 * @throws exception
 * /
 public static void save (outputstream outputstream, string content) throws exception {
 outputstream.write (content.getbytes ());
 outputstream.close ();
 }
 /**
 * Read data
 *
 * @param inputstream
 * @return
 * @throws exception
 * /
 public static string read (inputstream inputstream) throws exception {
 bytearrayoutputstream bytearrayoutputstream=new bytearrayoutputstream ();//write data to memory
 byte [] buffer=new byte [1024];//buffer
 int len ​​= -1;
 while ((len=inputstream.read (buffer))!=-1) {
  bytearrayoutputstream.write (buffer, 0, len);
 }
 byte [] data=bytearrayoutputstream.tobytearray ();//store data
 bytearrayoutputstream.close ();
 inputstream.close ();
 return new string (data);
 }
}

sdcardactivity class:

package com.ljq.sdcard;
import java.io.file;
import java.io.fileinputstream;
import java.io.fileoutputstream;
import java.io.inputstream;
import android.app.activity;
import android.os.bundle;
import android.os.environment;
import android.util.log;
import android.view.view;
import android.widget.button;
import android.widget.edittext;
import android.widget.textview;
import android.widget.toast;
import com.ljq.service.fileservice;
public class sdcardactivity extends activity {
 private final string tag="fileactivity";
 private edittext filenametext;
 private textview resultview;
 private edittext contenttext;
 @override
 public void oncreate (bundle savedinstancestate) {
 super.oncreate (savedinstancestate);
 setcontentview (r.layout.main);
 filenametext=(edittext) this.findviewbyid (r.id.filename);
 contenttext=(edittext) this.findviewbyid (r.id.content);
 resultview=(textview) this.findviewbyid (r.id.resultview);
 button button=(button) this.findviewbyid (r.id.button);
 button.setonclicklistener (listener);
 button readbutton=(button) this.findviewbyid (r.id.readbutton);
 readbutton.setonclicklistener (listener);
 }
 private view.onclicklistener listener=new view.onclicklistener () {
 public void onclick (view v) {
  button button=(button) v;
  string filename=filenametext.gettext (). tostring ();
  //environment.getexternalstoragedirectory () is equivalent to new file ("/sdcard") ---->Get the sdcard directory
  //Get the sdcard directory
  //file file=new file ("/sdcard" + filename);
  file file=new file (environment.getexternalstoragedirectory (), filename);
  switch (button.getid ()) {
  case r.id.button:
  int resid=r.string.success;//default success
  string content=contenttext.gettext (). tostring ();
  //sdcard exists and can read and write
  if (environment.getexternalstoragestate (). equals (environment.media_mounted)) {
   try {
   fileoutputstream fileoutputstream=new fileoutputstream (file);
   fileservice.save (fileoutputstream, content);
   } catch (exception e) {
   log.e (tag, e.tostring ());
   resid=r.string.error;
   }
   toast.maketext (sdcardactivity.this, resid, toast.length_long) .show ();
  } else {
   toast.maketext (sdcardactivity.this, "sdcard does not exist or is write protected", toast.length_long) .show ();
  }
  break;
  case r.id.readbutton:
  try {
   inputstream inputstream=new fileinputstream (file);
   string text=fileservice.read (inputstream);
   resultview.settext (text);
   } catch (exception e) {
   log.e (tag, e.tostring ());
   toast.maketext (sdcardactivity.this, "Read failed", toast.length_long) .show ();
   }
  break;
  }
 }
 };
}

Manifest file:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
 package="com.ljq.sdcard" android:versioncode="1"
 android:versionname="1.0">
 <application android:icon="@ drawable/icon"
 android:label="@ string/app_name">
 <activity android:name=". Sdcardactivity"
  android:label="@ string/app_name">
  <intent-filter>
  <action android:name="android.intent.action.main" />
  <category
   android:name="android.intent.category.launcher" />
  </intent-filter>
 </activity>
 </application>
 <uses-sdk android:minsdkversion="7" />
 <!-Create and delete file permissions in sdcard->
 <uses-permission
 android:name="android.permission.mount_unmount_filesystems" />
 <!-Write data permission to sdcard->
 <uses-permission android:name="android.permission.write_external_storage" />
</manifest>

operation result:

  • Previous C # method to calculate program execution time
  • Next C # simple implementation of the child form to the parent form value