1
File path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
File logfile = new File(path, "andrometer.txt");
String[] assignArr = new String [7];
assignArr[0]= finalValue + "17";
assignArr[1]= range + "16";
assignArr[2]= activeMode;
if (assignArr.length > 0)
{
try {
if (path.canWrite()){
FileWriter filewriter = new FileWriter(logfile);
BufferedWriter out = new BufferedWriter(filewriter);
for (int i1=0; i1<assignArr.length; i1++)
{
out.write(assignArr[0] + "\n");
out.write(assignArr[1] + "\n");
out.write(assignArr[2] + "\n");
Toast.makeText(AndroMeter.this, "out: " + assignArr[2], Toast.LENGTH_LONG).show();
}
out.close();
}
} catch (IOException e) {
Log.e("TAG", "Could not write file " + e.getMessage());
}
};
這段代碼幾個星期前工作,現在我似乎無法得到它。我有正確的清單權限我相信,我試圖寫入根目錄而不是特定的文件夾,我一直在用虛擬數據代替變量名稱進行測試。目前沒有文件正在創建。如何將TXT文件寫入SD卡?
我相信你正在尋找'Environment.getExternalStorageDirectory()'。 – jww 2015-02-18 08:34:40