Reputation: 13
I've got problem with writing files on the SD card in the emulator. Here is my code:
File directory;
directory = new File("/sdcard/b/b");
directory.mkdirs();
...
XmlSerializer serializer = Xml.newSerializer();
StringWriter writer = new StringWriter();
serializer.setOutput(writer);
...
String fileName = new Date().toString();
FileOutputStream fOut = healthCareApplication.openFileOutput("/sdcard/b/b"+fileName+".xml",Context.MODE_WORLD_READABLE);
OutputStreamWriter osw = new OutputStreamWriter(fOut);
osw.write(writer.toString());
osw.flush();
osw.close();
Firstly I create directory on the SD card, next I build some XML file. Then I try to create a file and try to save this XML File to this directory, but I get IllegalArgumentException and application crashes. "healthCareApplication" is class which extends the application class. What is wrong with this code?
EDIT
Ok, I add "Environment.getExternalStorageDirectory().getAbsolutePath();" and here is what I get in Logcat:
08-13 16:29:34.168: ERROR/AndroidRuntime(419): FATAL EXCEPTION: pool-1-thread-5
08-13 16:29:34.168: ERROR/AndroidRuntime(419): java.lang.IllegalArgumentException: File /mnt/sdcard/c/c/Sat Aug 13 16:29:34 GMT+00:00 2011.xml contains a path separator
08-13 16:29:34.168: ERROR/AndroidRuntime(419): at android.app.ContextImpl.makeFilename(ContextImpl.java:1648)
08-13 16:29:34.168: ERROR/AndroidRuntime(419): at android.app.ContextImpl.openFileOutput(ContextImpl.java:414)
08-13 16:29:34.168: ERROR/AndroidRuntime(419): at android.content.ContextWrapper.openFileOutput(ContextWrapper.java:158)
08-13 16:29:34.168: ERROR/AndroidRuntime(419): at com.myapp.runnable.ServerWorker.packageToXML(ServerWorker.java:197)
08-13 16:29:34.168: ERROR/AndroidRuntime(419): at com.myapp.runnable.ServerWorker.run(ServerWorker.java:85)
08-13 16:29:34.168: ERROR/AndroidRuntime(419): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
08-13 16:29:34.168: ERROR/AndroidRuntime(419): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
08-13 16:29:34.168: ERROR/AndroidRuntime(419): at java.lang.Thread.run(Thread.java:1019)
When I checking DDMS I see that the folder is created in sdcard but in that directory in is nothing so probably problem is occurred when application trying to write file to sdcard.
I also replace
"FileOutputStream fOut = healthCareApplication.openFileOutput("/sdcard/b/b"+fileName+".xml",Context.MODE_WORLD_READABLE);"
for
File outputFile = new File(wallpaperDirectory, fileName);
FileOutputStream fos = new FileOutputStream(outputFile);
and get another exception:
08-13 16:46:56.108: INFO/IOException(3110): /Sat Aug 13 16:46:56 GMT+00:00 2011 (Read-only file system)
Upvotes: 0
Views: 1306
Reputation: 6592
The first exception is generated because the filename being generated with new Date().toString();
contains a colon (:). This is illegal in the filename.
The second exception it looks like wallpaperDirectory is blank as the exception is being generated because you are trying to write to the / directory (which is read only).
Upvotes: 1