3

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.

Community
  • 1
  • 1
PH7
  • 3,926
  • 3
  • 24
  • 29

1 Answers1

0

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.

stefan
  • 10,215
  • 4
  • 49
  • 90
  • I think this is right. Even though "?" is used in Linux, I don't think it's allowed in Android. It's a good idea to avoid special characters in files/folders anyway to be multi-platform-friendly. – karnok Oct 11 '11 at 11:11
  • @stefan, I get your point. Unfortunately, I can't avoid using those file name as they are fetched from data source which i don't have access to and I need files to be saved in that name too. Can u give me more information or link about invalid characters? – PH7 Oct 12 '11 at 01:25
  • I'm sorry, I don't have a list of invalid characters for android. I think this might be a FAT32 issue, so this would imply that all of the following characters are invalid: /\:*?"<>| – stefan Oct 12 '11 at 08:11
  • maybe you can replace the invalid characters in a similar way it is done in every browser (you know: the %20 for a " "). Do you really need to _store_ the files with that name or do they need to be _displayed_ with the invalid characters? – stefan Oct 12 '11 at 08:14
  • @stefan, I found out disallowed characters in one SO link. I edited my question and put in the link itself. For my problem, I just did a work around for the time being as my requirement is to have the unique and consistent name for the files. Last but not least, I appreciate your help. cheers! :) – PH7 Oct 12 '11 at 14:09