java lang illegalargumentexception contains a path separator

java lang illegalargumentexception contains a path separator  using -'android'

I have a filename in my code as :

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

I get an error on 2nd line :

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

I tried this format also:

String NAME_OF_FILE="/sdcard/imageq.png";


asked Sep 29, 2015 by abhimca2006
0 votes

3 Answers

0 votes

This method opens a file in the private data area of the application. You cannot open any files in subdirectories in this area or from entirely other areas using this method. So use a FileInputStream or such.

answered Sep 29, 2015 by girisha
0 votes

The solution is:

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

The openFileInput method doesn't accept path separators.

Don't forget to


at the end.

answered Sep 29, 2015 by rajesh
0 votes

You cannot use path with directory separators directly, but you will have to make a file object for every directory.

NOTE: This code makes directories, yours may not need that...

File file= context.getFilesDir();

String[] array=filePath.split("/");
for(int t=0; t< array.length -1 ;t++)
    file=new File(file,array[t]);

File f=new File(file,array[array.length-1]);

RandomAccessFileOutputStream rvalue = new RandomAccessFileOutputStream(f,append);
answered Sep 29, 2015 by sandeep bhadauria