我一直试图在我的java代码中写入SD卡,但每当我检查我的卡时,文件夹和文件都不存在;我知道对于KitKat你必须使用.getExternalFilesDir
,但到目前为止没有任何工作。
我目前的代码:
String DataIn = PhoneNumber + "," + dataLong + "," + dataLat;
File storageDirectory = new File (this.getExternalFilesDir(null), "location.txt");
if(!storageDirectory.exists()) {
try {
storageDirectory.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
String Directory = storageDirectory.toString();
try
{
FileOutputStream fout = new FileOutputStream(storageDirectory, true);
OutputStreamWriter myoutwriter = new OutputStreamWriter(fout);
myoutwriter.write(DataIn);
myoutwriter.close();
Toast.makeText(getBaseContext(),"Saved", Toast.LENGTH_SHORT).show();
}
catch (Exception e)
{
Toast.makeText(getBaseContext(), e.getMessage(), Toast.LENGTH_SHORT).show();
}
答案 0 :(得分:1)
试试这个:
File storageDirectory = new File(Environment.getExternalStorageDirectory(), "location.txt");
确保您在清单中拥有此权限:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />