1

Continuing my previous post, upload file with relative path I tried to run using

url = '/mnt/sdcard/download/XYZ.txt';
File dir = Environment.getExternalStorageDirectory();
File f_path = new File(dir, url);
InputStream  fis = null;
fis = new BufferedInputStream(new FileInputStream(f_path));

But is throwing me errors when I run this from my Android device.

java.io.FileNotFoundException: /mnt/sdcard/mnt/sdcard/download/XYZ.txt (No such file or directory)

The output of f_path is '/mnt/sdcard/download/XYZ.txt'

Where is the problem, and how to fix it? I can observe that it is adding mnt/sdcard to f_path.

halfer
  • 19,824
  • 17
  • 99
  • 186
ChanChow
  • 1,346
  • 7
  • 28
  • 57

3 Answers3

4

Try with that code .

url = '/mnt/sdcard/download/XYZ.txt';
File f_path = new File(url);
InputStream  fis = null;
fis = new BufferedInputStream(new FileInputStream(f_path));

The exception had come coz Environment.getExternalStorageDirectory(); it self give equal to

/mnt/sdcard

Good luck !!

dharmendra
  • 7,835
  • 5
  • 38
  • 71
1

On your url variable, only use the relative path:

url = "/download/XYZ.txt"

The /mnt/sdcard will come from your Environment.getExternalStorageDirectory();

Thiago Moura
  • 358
  • 2
  • 12
  • the url value I mentioned is returned to me by a file picker activity. So do I need to trim to get the url you mentioned? – ChanChow Oct 20 '12 at 07:59
  • If the file picker is returning the full path, create a file only having the url parameter. `File f_path = new File(url);` – Thiago Moura Oct 20 '12 at 08:11
0

You have to do that as following :

 File sdcard = Environment.getExternalStorageDirectory();
 File file = new File(sdcard, "/download/XYZ.txt");

That will automatially get your file..

After that you can use it as following:

InputStream  fis = null;
fis = new BufferedInputStream(new FileInputStream(file.getAbsolutePath()));

That will get your file's path perfectly!!

Jai
  • 1,974
  • 2
  • 22
  • 42
  • It didn't work. Actually I am browsing the file into a edittext field. When I select the file, it returns the file path so that I can use to upload. – ChanChow Oct 20 '12 at 06:57
  • if you want to show file path into editext then of course you can get it using "file.getAbsolutePath()" function after writing: File sdcard = Environment.getExternalStorageDirectory(); File file = new File(sdcard, "/download/XYZ.txt"); – Jai Oct 20 '12 at 06:59
  • for my requirement I cannot give the hard coded value for file("/download/XYZ.txt". I am trying to browse through my mobile to select the file. Please look at my code. I need to upload the file after that. – ChanChow Oct 20 '12 at 07:00
  • I tried this and it worked.InputStream fis = null;fis = new BufferedInputStream(new FileInputStream(url); I removed first two lines of code. – ChanChow Oct 20 '12 at 07:31