在外部存储上创建/打开目录时出现IllegalArgumentException

时间:2013-03-08 11:29:33

标签: android file illegalargumentexception

我在这部分代码中发生了非法的参数异常:

boolean mExternalStorageAvailable = false;
boolean mExternalStorageWriteable = false;
String state = Environment.getExternalStorageState();

if (Environment.MEDIA_MOUNTED.equals(state)) {
    String root = Environment.getExternalStorageDirectory().toString();
    full_path = root+getResources().getString(R.string.app_name) ;
    File dir_path = getApplicationContext().getDir(full_path, MODE_PRIVATE);

这是错误日志:

03-08 12:26:23.717: E/AndroidRuntime(9234): FATAL EXCEPTION: main
03-08 12:26:23.717: E/AndroidRuntime(9234): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.streamfilesys/com.example.streamfilesys.MainActivity}: java.lang.IllegalArgumentException: File app_/storage/emulated/0/StreamFileSys contains a path separator
03-08 12:26:23.717: E/AndroidRuntime(9234):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
03-08 12:26:23.717: E/AndroidRuntime(9234):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
03-08 12:26:23.717: E/AndroidRuntime(9234):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
03-08 12:26:23.717: E/AndroidRuntime(9234):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
03-08 12:26:23.717: E/AndroidRuntime(9234):     at android.os.Handler.dispatchMessage(Handler.java:99)
03-08 12:26:23.717: E/AndroidRuntime(9234):     at android.os.Looper.loop(Looper.java:137)
03-08 12:26:23.717: E/AndroidRuntime(9234):     at android.app.ActivityThread.main(ActivityThread.java:5041)
03-08 12:26:23.717: E/AndroidRuntime(9234):     at java.lang.reflect.Method.invokeNative(Native Method)
03-08 12:26:23.717: E/AndroidRuntime(9234):     at java.lang.reflect.Method.invoke(Method.java:511)
03-08 12:26:23.717: E/AndroidRuntime(9234):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
03-08 12:26:23.717: E/AndroidRuntime(9234):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
03-08 12:26:23.717: E/AndroidRuntime(9234):     at dalvik.system.NativeStart.main(Native Method)
03-08 12:26:23.717: E/AndroidRuntime(9234): Caused by: java.lang.IllegalArgumentException: File app_/storage/emulated/0/StreamFileSys contains a path separator
03-08 12:26:23.717: E/AndroidRuntime(9234):     at android.app.ContextImpl.makeFilename(ContextImpl.java:1966)
03-08 12:26:23.717: E/AndroidRuntime(9234):     at android.app.ContextImpl.getDir(ContextImpl.java:1816)
03-08 12:26:23.717: E/AndroidRuntime(9234):     at android.content.ContextWrapper.getDir(ContextWrapper.java:218)
03-08 12:26:23.717: E/AndroidRuntime(9234):     at com.example.streamfilesys.MainActivity.onCreate(MainActivity.java:39)
03-08 12:26:23.717: E/AndroidRuntime(9234):     at android.app.Activity.performCreate(Activity.java:5104)
03-08 12:26:23.717: E/AndroidRuntime(9234):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
03-08 12:26:23.717: E/AndroidRuntime(9234):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
03-08 12:26:23.717: E/AndroidRuntime(9234):     ... 11 more

mainActivity第39行是

File dir_path = getApplicationContext().getDir(full_path, MODE_PRIVATE);

我该如何解决?

1 个答案:

答案 0 :(得分:1)

正如doc所说的Context.getDir (String name, int mode)

  

检索,根据需要创建应用程序所在的新目录   可以放置自己的自定义数据文件。您可以使用返回的文件   用于创建和访问此目录中的文件的对象。请注意文件   通过File对象创建的只能由您自己访问   应用;你只能设置整个目录的模式,而不是   个别档案。

表示不是通过文件分隔符传递文件名,而是需要将文件夹(Dir)名称作为第一个参数传递给getDir方法“:

File dir_path = getApplicationContext().getDir(
                        getResources().getString(R.string.app_name), MODE_PRIVATE);

修改

在外部SDCARD上创建Dir。将您的代码更改为:

File onsdcarddir = new File(Environment.getExternalStorageDirectory() +
                               "/" +getResources().getString(R.string.app_name));

if (!onsdcarddir.exists()) {
   onsdcarddir.mkdir(); // create dir here
}

还在AndroidManifest.xml中添加SDCARD权限:

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