我想在外部SD卡上的Android設備上創建一個簡單的文本文件。我的代碼,我總是得到錯誤:java.io.FileNotFoundException: /storage/emulated/0/SFX/TEST1.txt: open failed: ENOENT (No such file or directory)
在Android的外部SD卡上通過FileOutputStream創建文本文件 - FileNotFoundException
我在清單文件
Button btn = (Button)findViewById(R.id.btn_Save);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//mach was
StringBuilder sb = new StringBuilder();
sb.append(txtName.getText().toString());
sb.append(System.getProperty("line.separator"));
sb.append(txtMonteur.getText().toString());
sb.append(System.getProperty("line.separator"));
sb.append(txtStraße.getText().toString());
sb.append(System.getProperty("line.separator"));
sb.append(Energiearten);
File file;
FileOutputStream out;
try
{
file = new File(Environment.getExternalStorageDirectory(), "SFX/TEST1.txt");
file.mkdirs();
out = new FileOutputStream(file);
out.write(sb.toString().getBytes());
out.close();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
Toast.makeText(ZBewegung.this, "Error:" + e.toString(), Toast.LENGTH_LONG).show();
}
}
});
如果你在> = 23 API,你必須寫檢查許可代碼也正在測試。如果沒有,那麼在這裏發佈你的錯誤日誌。 – androidnoobdev
[實現運行時權限](https://stackoverflow.com/questions/32635704/android-permission-doesnt-work-even-if-i-have-declared-it)。另請注意,您的代碼不屬於SD卡,因爲[外部存儲](https://commonsware.com/blog/2014/04/08/storage-situation-external-storage.html)不是[可移動存儲](https://commonsware.com/blog/2014/04/09/storage-situation-removable-storage.html)在大多數Android設備上。 – CommonsWare
檢查SD卡是否創建文件? –