Reputation: 10068
i've occured an illegal argument exception in this portion of code:
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);
this is the error log:
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
the mainActivity line 39 is
File dir_path = getApplicationContext().getDir(full_path, MODE_PRIVATE);
how can i fix it?
Upvotes: 0
Views: 3482
Reputation: 132982
as doc says about Context.getDir (String name, int mode) :
Retrieve, creating if needed, a new directory in which the application can place its own custom data files. You can use the returned File object to create and access files in this directory. Note that files created through a File object will only be accessible by your own application; you can only set the mode of the entire directory, not of individual files.
means instead of passing file name with file separator u will need to pass only folder(Dir) name as first paramter to getDir methods as ":
File dir_path = getApplicationContext().getDir(
getResources().getString(R.string.app_name), MODE_PRIVATE);
EDIT:
to create Dir on external SDCARD. change your code as :
File onsdcarddir = new File(Environment.getExternalStorageDirectory() +
"/" +getResources().getString(R.string.app_name));
if (!onsdcarddir.exists()) {
onsdcarddir.mkdir(); // create dir here
}
also add SDCARD permission in AndroidManifest.xml
:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
Upvotes: 1