Note that there are some explanatory texts on larger screens.

plurals
  1. POTrouble saving file in SD card with special characters in filename
    text
    copied!<p>My application has a function to save file on SD card. </p> <p>Following is my code to save the file</p> <pre><code>try { File mediaDir = new File( Environment.getExternalStorageDirectory(), "Media"); if (!mediaDir.exists()) { mediaDir.mkdirs(); } File f = new File(mediaDir, fileName); f.createNewFile(); OutputStream os = new FileOutputStream(f); byte[] data = toWriteBytes; os.write(data); os.close(); } catch (Exception e) { Log.w("ExternalStorage", "Error writing "); e.printStackTrace(); } </code></pre> <p>This works totally fine before it bumped into filename containing space in it <strike>(eg. "Hello World.txt")</strike> (eg. hello? world.txt) </p> <p>Here is the stacktrace in case you all need</p> <pre><code>10-11 17:18:48.225: WARN/ExternalStorage(4519): Error writing 10-11 17:18:48.225: WARN/System.err(4519): java.io.IOException: Invalid argument 10-11 17:18:48.235: WARN/System.err(4519): at java.io.File.createNewFileImpl(Native Method)10-11 17:18:48.245: WARN/System.err(4519): at java.io.File.createNewFile(File.java:1257) 10-11 17:18:48.245: WARN/System.err(4519): at com.xxxx.Utility.createExternalStoragePrivateFile(Utility.java:87) 10-11 17:18:48.245: WARN/System.err(4519): at com.xxxxxx$1.handleMessage(Utility.java:52) 10-11 17:18:48.245: WARN/System.err(4519): at android.os.Handler.dispatchMessage(Handler.java:99) 10-11 17:18:48.245: WARN/System.err(4519): at android.os.Looper.loop(Looper.java:123) 10-11 17:18:48.245: WARN/System.err(4519): at android.app.ActivityThread.main(ActivityThread.java:3839) 10-11 17:18:48.245: WARN/System.err(4519): at java.lang.reflect.Method.invokeNative(Native Method) 10-11 17:18:48.245: WARN/System.err(4519): at java.lang.reflect.Method.invoke(Method.java:507) 10-11 17:18:48.245: WARN/System.err(4519): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841) 10-11 17:18:48.245: WARN/System.err(4519): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599) 10-11 17:18:48.245: WARN/System.err(4519): at dalvik.system.NativeStart.main(Native Method) </code></pre> <p>So, my question is what exactly might be the problem and how do i solve it?</p> <p>**Note: I tested it using single word file name and it is perfectly fine. Without <code>f.createNewFile()</code>, it will give <code>FileNotFoundException</code>.</p> <hr> <p>After seeing Mice comment, I realized that it may not be the space in the file name. So, here is the stacktrace. </p> <pre><code>10-11 18:06:20.365: WARN/System.err(5280): java.io.FileNotFoundException: /mnt/sdcard/Media/Who owns the World? Smokers or Non-smokers .acsm (Invalid argument) 10-11 18:06:20.385: WARN/System.err(5280): at org.apache.harmony.luni.platform.OSFileSystem.open(Native Method) 10-11 18:06:20.385: WARN/System.err(5280): at dalvik.system.BlockGuard$WrappedFileSystem.open(BlockGuard.java:232) 10-11 18:06:20.385: WARN/System.err(5280): at java.io.FileOutputStream.&lt;init&gt;(FileOutputStream.java:94) 10-11 18:06:20.385: WARN/System.err(5280): at java.io.FileOutputStream.&lt;init&gt;(FileOutputStream.java:66) 10-11 18:06:20.385: WARN/System.err(5280): at com.xxxx.Utility.createExternalStoragePrivateFile(Utility.java:88) 10-11 18:06:20.385: WARN/System.err(5280): at com.xxxxx.Utility$1.handleMessage(Utility.java:52) 10-11 18:06:20.385: WARN/System.err(5280): at android.os.Handler.dispatchMessage(Handler.java:99) 10-11 18:06:20.385: WARN/System.err(5280): at android.os.Looper.loop(Looper.java:123) 10-11 18:06:20.385: WARN/System.err(5280): at android.app.ActivityThread.main(ActivityThread.java:3839) 10-11 18:06:20.385: WARN/System.err(5280): at java.lang.reflect.Method.invokeNative(Native Method) 10-11 18:06:20.385: WARN/System.err(5280): at java.lang.reflect.Method.invoke(Method.java:507) 10-11 18:06:20.385: WARN/System.err(5280): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841) 10-11 18:06:20.385: WARN/System.err(5280): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599) 10-11 18:06:20.385: WARN/System.err(5280): at dalvik.system.NativeStart.main(Native Method) </code></pre> <p>My thought is it should be some invalid character in the file name. The file name is ("Who owns the World? Smokers or Non-smokers .acsm"). Any thought?</p> <hr> <p>For the benefit of those who faces similar problem, it is indeed invalid character in the file name. I found out <a href="https://stackoverflow.com/questions/2679699/what-characters-allowed-in-file-names-on-android">this thread</a> on SO which has more info about invalid characters in file name. Hope this will clear any doubts.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload