0

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

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

我该如何解决?

4

1 回答 1

1

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

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

意味着您不需要将文件名与文件分隔符一起传递,而是只需要将文件夹(Dir)名称作为第一个参数传递给 getDir 方法,如“:

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

编辑:

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

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

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

还添加 SDCARD 权限AndroidManifest.xml

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
于 2013-03-08T11:39:59.903 回答