Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java.lang.IllegalArgumentException: contains a path separator

Tags:

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";
like image 980
M.A.Murali Avatar asked May 11 '11 11:05

M.A.Murali


4 Answers

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

fis.close();

at the end.

like image 91
runix Avatar answered Nov 07 '22 08:11

runix


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 the constructor of the FileInputStream directly to pass the path with a directory in it.

like image 23
Stephan Avatar answered Nov 07 '22 07:11

Stephan


openFileInput() doesn't accept paths, only a file name if you want to access a path, use File file = new File(path) and corresponding FileInputStream

like image 33
reflog Avatar answered Nov 07 '22 08:11

reflog


I got the above error message while trying to access a file from Internal Storage using openFileInput("/Dir/data.txt") method with subdirectory Dir.

You cannot access sub-directories using the above method.

Try something like:

FileInputStream fIS = new FileInputStream (new File("/Dir/data.txt"));
like image 4
Swaran Singh Avatar answered Nov 07 '22 07:11

Swaran Singh