Question

My application has a function to save file on SD card.

Following is my code to save the file

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();
}

This works totally fine before it bumped into filename containing space in it (eg. "Hello World.txt") (eg. hello? world.txt)

Here is the stacktrace in case you all need

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)

So, my question is what exactly might be the problem and how do i solve it?

**Note: I tested it using single word file name and it is perfectly fine. Without f.createNewFile(), it will give FileNotFoundException.


After seeing Mice comment, I realized that it may not be the space in the file name. So, here is the stacktrace.

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.<init>(FileOutputStream.java:94)
10-11 18:06:20.385: WARN/System.err(5280):     at java.io.FileOutputStream.<init>(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)

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?


For the benefit of those who faces similar problem, it is indeed invalid character in the file name. I found out this thread on SO which has more info about invalid characters in file name. Hope this will clear any doubts.

Was it helpful?

Solution

I have no idea what's wrong with "Hello World.txt" but "Who owns the World? Smokers or Non-smokers .acsm" contains the invalid character "?". Maybe you should implement a check for invalid characters to avoid crashes.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top