在这部分代码中出现了一个非法参数异常:
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);
我该如何修复它?
正如doc所说的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
}
还在AndroidManifest.xml
:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>