Create a file in the SDCard
public class IO_SDCardActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//判斷SD卡是否存在
if (!Environment.getExternalStorageState().equals(Environment.MEDIA_REMOVED)) {
try {
//取得SD卡路徑
File SDCardpath = Environment.getExternalStorageDirectory();
File myDataPath = new File (SDCardpath.getAbsolutePath() + "/fileio");
if (!myDataPath.exists()){ // 如果SDCard底下沒有此資料夾,請產生新的資料夾
myDataPath.mkdirs();
//將資料寫入到SD卡
FileWriter mFile = new FileWriter(SDCardpath.getAbsolutePath() + "/fileio/test01.txt");
mFile.write("This is a test. 這是個實驗啦。");
mFile.close();
}else { //如果已經有了,直接產生新的檔案
//將資料寫入到SD卡
FileWriter mFile = new FileWriter(SDCardpath.getAbsolutePath() + "/fileio/test01.txt");
mFile.write("This is a test. 這是個實驗啦。");
mFile.close();
}
}catch (Exception e) {
e.printStackTrace();
}
}else {
Toast.makeText(this, "Please add <uses-permission android:name=\"android.permission.WRITE_EXTERNAL_STORAGE\"/> in Manifest.xml", Toast.LENGTH_SHORT).show();
}
}// end of onCreate()
}
沒有留言:
張貼留言