简体   繁体   English

在SD卡上写入时发生IllegalArgumentException

[英]IllegalArgumentException while writing on sdcard

I saw this problem has been met many times, but strangely I was not able to find a solution. 我看到这个问题已经解决了很多次,但是奇怪的是我找不到解决方案。

I am trying to write a binary file to the SDcard. 我正在尝试将二进制文件写入SD卡。 This is the source code: 这是源代码:

private void saveDataLongs() {
    try
    {
        ObjectOutputStream oos = new ObjectOutputStream(ctx.openFileOutput(Environment.getExternalStorageDirectory().getAbsolutePath()+"/longs.bin", ctx.MODE_WORLD_WRITEABLE));
        for (int w=0; w<longCount; w++)
            oos.writeLong(longs[w]);
        oos.close();
    }
    catch(IOException e)
    { e.printStackTrace(); }
}

The Manifest contains 清单包含

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

and I receive this error: 我收到此错误:

 01-21 22:19:57.323: E/AndroidRuntime(13713): java.lang.RuntimeException: Unable to start activity ComponentInfo{it.ccc.ccc/it.ccc.ccc.Ccc}: java.lang.IllegalArgumentException: File /sdcard/longs.bin contains a path separator

From other posts I could understand that some functions are meant to write only in the private storage of the app, so they don't expect to manage directories and paths. 从其他文章中,我可以理解,某些功能只能在应用程序的专用存储中编写,因此它们不希望管理目录和路径。 Is some one able to help me? 有人可以帮助我吗? Whall I use a different method to write the data to the sd, or just make some other action before doing it? 我会使用其他方法将数据写入sd,还是只是在执行其他操作之前执行其他操作? I'm trying to write to the sdcard a simple binary file (btw it's a precalculated sequence of number, and I need to pass it to my PC and then move it back to the assets, so, if there is a different way to obtain this goal, it's ok anyway). 我正在尝试将一个简单的二进制文件写入sdcard(顺便说一下,这是一个预先计算的数字序列,我需要将其传递到PC上,然后再将其移回资产中,因此,如果有一种不同的获取方式这个目标,还是可以的)。

Thank you very much. 非常感谢你。

You say that you are trying to write to external storage, but you are calling openFileOutput() , which is for internal storage. 您说您正在尝试写入外部存储,但是您正在调用openFileOutput() ,它用于内部存储。

Change: 更改:

new ObjectOutputStream(ctx.openFileOutput(Environment.getExternalStorageDirectory().getAbsolutePath()+"/longs.bin", ctx.MODE_WORLD_WRITEABLE));

to: 至:

new ObjectOutputStream(new FileOutputStream(new File(Environment.getExternalStorageDirectory(), "longs.bin")));

or, better yet, to: 或者更好的是:

new ObjectOutputStream(new FileOutputStream(new File(ctx.getExternalFilesDir(null), "longs.bin")));

I like CommonsWare's answer. 我喜欢CommonsWare的回答。 I would simply like to add that if you ever DO want to go down a path, don't use / . 我只想补充一点,如果您确实想走一条路,请不要使用/ Use File.separator . 使用File.separator I don't think I've ever had any errors come up when simply using / but still. 我不认为仅使用/时就不会出错。
So if you made a sub-folder called "To-dos" in the sdcard's directory, you would do something like the following: 因此,如果您在sdcard的目录中创建了一个名为“ To-dos”的子文件夹,则将执行以下操作:
new ObjectOutputStream(new File(Environment.getExternalStorageDirectory() + File.separator + "To-dos", "longs.bin"));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM