在 SD 卡中保存文件时,文件名中含有特殊字符时出现问题

发布于 2024-12-09 02:42:29 字数 4212 浏览 2 评论 0原文

我的应用程序具有将文件保存到 SD 卡上的功能。

以下是我保存文件的代码

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

在遇到包含空格的文件名之前它工作得很好(例如“Hello World.txt”)(例如hello?world.txt)

这是堆栈跟踪,以防你们都需要

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)

所以,我的问题是问题到底是什么以及我该如何解决它?

**注意:我使用单个单词文件名对其进行了测试,效果非常好。 如果没有f.createNewFile(),它将给出FileNotFoundException


看到Mice的评论后,我意识到可能不是文件名中的空格。所以,这是堆栈跟踪。

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)

我的想法是文件名中应该是一些无效字符。文件名是(“谁拥有世界?吸烟者还是非吸烟者.acsm”)。有什么想法吗?


为了那些面临类似问题的人的利益,文件名中确实是无效字符。我发现这个线程在SO上有更多信息文件名中的无效字符。希望这能消除任何疑虑。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

给我一枪 2024-12-16 02:42:29

我不知道“Hello World.txt”有什么问题,但“谁拥有世界?吸烟者还是非吸烟者.acsm”包含无效字符“?”。也许您应该检查无效字符以避免崩溃。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文