功能:讀寫SD card
AndroidManifest.xml
加入權限"android.permission.WRITE_EXTERNAL_STORAGE"
Xxx.java
// Write
private writeToSdCard() {
File vSDCard= null;
try {
// 獲得外部外接裝置位置
vSDCard= Environment.getExternalStorageDirectory();
// vSDCard.getParent() - "/" ; vSDCard.getName() - "sdcard"
// 使用這種方式有可能會得到錯誤的路徑,所以改用下面的方式
// vSDCard.getAbsolutePath() - 擷取目前位置路徑
FileWriter vFile = new FileWriter(vSDCard.getAbsolutePath() + "/Log.txt", true);
// write the data to file
vFile.append("," + status + "," + level + "," + "\n");
vFile.close();
}
catch (Exception e) {
}
}
// Read
private void readFromSdCard() {
File vSDCard= null;
try {
// 獲得外部外接裝置位置
vSDCard= Environment.getExternalStorageDirectory();
// read data form sd card file
FileReader vFile = new FileReader( vSDCard.getAbsolutePath() + "/sd_test.txt" );
StringBuffer sb = new StringBuffer();
int c;
// if value = "-1", it mean the end of the file
while ((c = vFile.read()) != -1) {
sb.append((char) c);
}
vFile.close();
}
catch (Exception e) {
}
}
沒有留言:
張貼留言