Java.lang.IllegalArgumentException: contains path separator

I have a file name in my code:

String NAME_OF_FILE="//sdcard//imageq.png"; FileInputStream fis =this.openFileInput(NAME_OF_FILE); // 2nd line 

I get a 2nd line error message:

05-11 16: 49: 06.355: ERROR / AndroidRuntime (4570): caused by: java.lang.IllegalArgumentException: File //sdcard//imageq.png contains a path separator

I also tried this format:

 String NAME_OF_FILE="/sdcard/imageq.png"; 
+65
android
May 11 '11 at 11:33
source share
7 answers

This method opens the file in the private data area of ​​the application. You cannot open files in subdirectories in this area or from other areas using this method. Therefore, use the FileInputStream constructor directly to pass the path with the directory in it.

+57
May 11 '11 at 11:36
source share

Decision:

 FileInputStream fis = new FileInputStream (new File(NAME_OF_FILE)); // 2nd line 

The openFileInput method does not accept path separators.

Do not forget

 fis.close(); 

in the end.

+62
Jun 24 '13 at 20:34
source share

openFileInput() does not accept paths, only the file name if you want to access the path, use File file = new File(path) and the corresponding FileInputStream

+23
May 11 '11 at 11:34 11:34
source share

You cannot use the directory delimited path directly, but you must create a file file for each directory.

NOTE. This code creates directories you might not need ...

 File file= context.getFilesDir(); file.mkdir(); String[] array=filePath.split("/"); for(int t=0; t< array.length -1 ;t++) { file=new File(file,array[t]); file.mkdir(); } File f=new File(file,array[array.length-1]); RandomAccessFileOutputStream rvalue = new RandomAccessFileOutputStream(f,append); 
+3
May 11 '12 at 14:53
source share
 File file = context.getFilesDir(); file.mkdir(); String[] array = filePath.split("/"); for(int t = 0; t < array.length - 1; t++) { file = new File(file, array[t]); file.mkdir(); } File f = new File(file,array[array.length- 1]); RandomAccessFileOutputStream rvalue = new RandomAccessFileOutputStream(f, append); 
0
Mar 06 '16 at 5:50
source share

I solved this type of error by creating a directory in the onCreate event, then accessing the directory, creating a new file object in a method that should do something like saving or retrieving a file in this directory, hope this helps

  public class MyClass { private String state; public File myFilename; @Override protected void onCreate(Bundle savedInstanceState) {//create your directory the user will be able to find super.onCreate(savedInstanceState); if (Environment.MEDIA_MOUNTED.equals(state)) { myFilename = new File(Environment.getExternalStorageDirectory().toString() + "/My Directory"); if (!myFilename.exists()) { myFilename.mkdirs(); } } } public void myMethod { File fileTo = new File(myFilename.toString() + "/myPic.png"); // use fileTo object to save your file in your new directory that was created in the onCreate method } } 
0
Jul 06 '17 at 18:53 on
source share

I received the above error message when trying to access a file from internal storage using the openFileInput("/Dir/data.txt") method with the Dir subdirectory.

You cannot access subdirectories using the above method.

Try something like:

 FileInputStream fIS = new FileInputStream (new File("/Dir/data.txt")); 
0
Jan 03 '18 at 12:06
source share



All Articles