我想做一個Android活動寫測試文件到設備的可移動SD卡。然後我需要能夠驗證它被寫入。我已經能夠寫入手機的內部存儲器(稱爲外部存儲器,雖然不是),但不能寫入可移動的SD卡。我想我需要得到正確的路徑。安卓 - 保存文件到可移動標清和讀取
我已經能夠檢查與此代碼的可移動SD卡的存在,但一直沒能將其轉化爲寫入文件:
String secStore = System.getenv("SECONDARY_STORAGE");
File path = new File(secStore);
StatFs stat = new StatFs(path.getPath());
這是我寫測試文件代碼BT就像我說的,把它寫入到設備的內部存儲:
File dir = new File(android.os.Environment.getExternalStorageDirectory(), "TestFolder");
if (!dir.exists()) {
dir.mkdirs();
}
String filename = "SD_Test.txt";
try {
File f = new File(dir + File.separator + filename);
FileOutputStream fOut = new FileOutputStream(f);
OutputStreamWriter myOutWriter = new OutputStreamWriter(fOut);
myOutWriter.append("Testing SD Card");
myOutWriter.flush();
myOutWriter.close();
fOut.close();
if(f.exists()){
Toast.makeText(getBaseContext(),
"File Created",
Toast.LENGTH_SHORT).show();
}
} catch (Exception e) {
e.printStackTrace();
}
}
請幫幫忙,我一直在這幾天,我堅持!