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.

3

3 Answers 3

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 !!

2
  • yup. I got this. But as of knew about Environment.getExternalStorageDirectory(); is there any impact in removing that?
    – ChanChow
    Oct 20, 2012 at 8:06
  • No there will not impact of it , as android directory stander is same for each device
    – dharmendra
    Oct 20, 2012 at 8:18
1

On your url variable, only use the relative path:

url = "/download/XYZ.txt"

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

2
  • 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, 2012 at 7:59
  • If the file picker is returning the full path, create a file only having the url parameter. File f_path = new File(url); Oct 20, 2012 at 8: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!!

4
  • 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, 2012 at 6: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, 2012 at 6: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, 2012 at 7: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, 2012 at 7:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.