This article mainly describes how to write data to the SD card, where the main technology is environment in the method.
1.
2. Implementation code:
/datasave/src/com/amos/datasave/savepasswordservice.java
// Write data to sdcard
public void savePasswordToSDCard (String name, String password) {
// android 2.1 /sdcard/xx.txt
// android 2.2 /mnt/sdcard/xx.txt
// self_define /excard/xx.txt
// File externalStorageDirectory = Environment.getExternalStorageDirectory ();
// String path = externalStorageDirectory.getPath ();
// System.out.println ("path:" + path);
// what to store
String content = name + ":" + password;
Log.d (tag, "Check if sdcard is available?");
// Check if sdcard exists?
if (! Environment.getExternalStorageState (). equals (Environment.MEDIA_MOUNTED)) {
Log.d (tag, "sdcard is not available!");
Toast.makeText (context, "No SDCard found!", Toast.LENGTH_LONG);
return;
};
try {
// File file = new File ("/ sdcard / qqpassword.txt");
// File file = new File (path + "/qqpassword2.txt");
File file = new File (Environment.getExternalStorageDirectory (), "/qqpassword2.txt");
FileOutputStream fos = new FileOutputStream (file);
fos.write (content.getBytes ());
fos.flush ();
fos.close ();
} catch (Exception e) {
e.printStackTrace ();
}
}
In android2.1 and previous versions, its sdcard directory became/mnt/sdcard in the root directory, 2.2, and 2.3 after its sdcard directory, as well as some manufacturers ' custom Android systems, It may also change the name of the SDcard. If you still use absolute path, the compatibility of the program will be greatly reduced. Here the main use of the Enviroment class.
Android.os.Environment
The main methods are:
- Getrootdirectory ()---->/get the root directory
- Getdatadirectory ()---->/data get the Data directory
- getExternalStorageDirectory ()--->/sdcard get SD card Directory
- Getdownloadcachedirectory ()--->/cache get the download file's cache directory
- Getexternalstoragestate---The state of >sdcard, Removed,unmounted,checking,nofs,mounted,mounted_ro,shared,unmountable, Bad_removal
The complete Savepasswordservice.java file is:
package com.amos.datasave;
import java.io.File;
import java.io.FileOutputStream;
import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Environment;
import android.util.Log;
import android.widget.Toast;
@SuppressLint ("WorldWriteableFiles")
public class savePasswordService {
private Context context;
private String tag = "savePasswordService";
public savePasswordService (Context context) {
this.context = context;
}
public void savePasswordToFile (String name, String password) {
// Set file permissions here
String content = name ":" password;
Log.d (tag, "Set read and write permissions on the file");
try {
FileOutputStream fileOutput = context.openFileOutput ("LoginTestConfig.txt", Context.MODE_WORLD_READABLE | Context.MODE_WORLD_WRITEABLE);
fileOutput.write (content.getBytes ());
fileOutput.flush ();
fileOutput.close ();
} catch (Exception e) {
Log.d (tag, "Failed to set read and write permissions for the file!");
e.printStackTrace ();
}
}
// Write data to sdcard
public void savePasswordToSDCard (String name, String password) {
// android 2.1 /sdcard/xx.txt
// android 2.2 /mnt/sdcard/xx.txt
// self_define /excard/xx.txt
// File externalStorageDirectory = Environment.getExternalStorageDirectory ();
// String path = externalStorageDirectory.getPath ();
// System.out.println ("path:" path);
// what to store
String content = name ":" password;
Log.d (tag, "Check if sdcard is available?");
// Check if sdcard exists?
if (! Environment.getExternalStorageState (). equals (Environment.MEDIA_MOUNTED)) {
Log.d (tag, "sdcard is not available!");
Toast.makeText (context, "No SDCard found!", Toast.LENGTH_LONG);
return;
};
try {
// File file = new File ("/ sdcard / qqpassword.txt");
// File file = new File (path "/qqpassword2.txt");
File file = new File (Environment.getExternalStorageDirectory (), "/qqpassword2.txt");
FileOutputStream fos = new FileOutputStream (file);
fos.write (content.getBytes ());
fos.flush ();
fos.close ();
} catch (Exception e) {
e.printStackTrace ();
}
}
}
View Code
How do I get the size of sdcard?
StatFs statFs = new StatFs(Environment.getExternalStorageDirectory().getPath());
long blockSize = statFs.getBlockSize();
long blockCount = statFs.getBlockCount();
long sdCardSize = blockSize*blockCount;
Log.d(tag,""+sdCardSize );
Here, the method in environment gets the path to the SDcard and then passes its path through the Statfs class, which primarily gets the file information (filesystem info) under the specified file path.
Gets its block size, number of blocks.