Petar
Petar

Reputation: 2283

Android - FileNotFoundException when opening file with blank characters in the name

I am building an android application and have the following problem. When I want to open a file from the sdcard using FileInputStream and if the file name contains a blank character, I get the FileNotFoundException. If there are no blank characters, everything works fine. So, if I am to open for example: "My file.ext" it raises an exception, and if the file name is "Myfile.ext" everything is fine.

Any clues on how to resolve that will be much appreciated.

Regards

Upvotes: 1

Views: 511

Answers (2)

sokie
sokie

Reputation: 1986

You could try to let android parse a certain file and open it

 File file = new File(Uri.parse(path+"file name.txt"));
 FileInputStream inputStream = new FileInputStream(file);

Uri should parse your filenames correctly.

Upvotes: 0

prolink007
prolink007

Reputation: 34564

You will need to use an escape character to specific that there is a blank in the file name. I believe it is a '\'...

So it would be like this "my\ textfile.txt"

Upvotes: 3

Related Questions