I am currently trying to paste current time into a file for an Android application. The code looks like this but the file is not created. I have enabled the permission of my application to write on the SD card through the manifest. Any ideas?
Time today = new Time(Time.getCurrentTimezone());
today.setToNow();
try {
File myFile = new File("/sdcard/mysdfile.txt");
myFile.createNewFile();
FileOutputStream fOut = new FileOutputStream(myFile);
OutputStreamWriter myOutWriter =
new OutputStreamWriter(fOut);
SimpleDateFormat dF = new SimpleDateFormat("HHMMSS");
StringBuilder current = new StringBuilder(dF.format(today));
myOutWriter.append(current);
myOutWriter.close();
fOut.close();
}
You should use Environment.getExternalStorageDirectory() instead of hard-coding the
/sdcard/path.File file = new File(Environment.getExternalStorageDirectory(), "mysdfile.txt");I’ve tried running your code and it crashes due to
dF.format(today).Instead of having this,
It works well with this
This code works on my device.