0

I may be way off here, but I am trying to download a file and store it in the downloads folder on my phone. I am getting a "java.io.FileNotFoundException" error, because the file doesn't exist, because I'm trying to download it...what am I doing wrong?

String PATH = Environment.getExternalStorageDirectory() + "/download/";
File dir = new File(PATH);
dir.mkdirs();

File outputFile = new File(dir, "downloadFile");
FileOutputStream fos = new FileOutputStream(outputFile);

This fails, with the following:

java.io.FileNotFoundException: /mnt/sdcard/download/downloadFile (Permission denied)

I am using the WRITE_EXTERNAL_STORAGE and READ_EXTERNAL_STORAGE permissions....

NagarjunaReddy
  • 8,621
  • 10
  • 63
  • 98
TomBomb
  • 3,236
  • 5
  • 31
  • 40
  • Are you want to write a file or reading a file – user1089679 Feb 03 '12 at 04:12
  • I'm trying to write to a file, and call it "downloadFile", whether it exists or not – TomBomb Feb 03 '12 at 04:18
  • You want to make file which name is downloadFile? – user1089679 Feb 03 '12 at 04:19
  • @TomBomb: You say you want to do this on your phone. Do you have it connected with a USB cable to your PC? If yes, are you sure the SD card hasn't be unmounted? – Squonk Feb 03 '12 at 04:32
  • 1
    Ahhhhhh genius MisterSquonk. Thank you very much. – TomBomb Feb 03 '12 at 04:43
  • @TomBomb: I do a lot of debugging or logcat tracking with DDMS so I have my default setting when I connect USB to be 'Charge only'. I had similar problems when I first started trying to use the SD card when connected to my PC. – Squonk Feb 03 '12 at 05:13

3 Answers3

2

Please try following updated code,

String PATH = Environment.getExternalStorageDirectory() + "/download";
        File dir = new File(PATH);
        dir.mkdirs();

        File outputFile = new File(dir, "downloadFile");
        if ( !outputFile.exists() ) 
        {
             outputFile.create();
        }
        FileOutputStream fos = new FileOutputStream(outputFile);

There is a "/" after the download. This way Android is thinking that you are creating Recursive Directory. While using mkdirs(), you can not create recursive directories.

You can also check my answer for same in Java ME here.

Community
  • 1
  • 1
Lucifer
  • 29,392
  • 25
  • 90
  • 143
  • String PATH = Environment.getExternalStorageDirectory() + "/download"; File dir = new File(PATH); dir.mkdirs(); File outputFile = new File(dir, "/downloadFile"); FileOutputStream fos = new FileOutputStream(outputFile); – TomBomb Feb 03 '12 at 04:26
  • Please try this, File outputFile = new File(dir, "//downloadFile"); Also check that download folder has been created or not ? – Lucifer Feb 03 '12 at 04:28
  • The download folder already exists on the SD card...adding "//" before the filename doesn't make a difference – TomBomb Feb 03 '12 at 04:32
  • ok try this, File outputFile = new File(dir, "/downloadFile"); if ( !outputFile.exists() ) { outputFile.create(); } – Lucifer Feb 03 '12 at 04:35
0

Here you want to make directoy? it there is already folder of download than use this code directly.

// create a File object for the parent directory
File Directory = new File("/sdcard/download/");

Directory.mkdirs();
// create a File object for the output file
File outputFile = new File(Directory, downloadFile);
// now attach the OutputStream to the file object, instead of a String representation
FileOutputStream fos = new FileOutputStream(outputFile);

It might be wise to use Environment.getExternalStorageDirectory() for getting the "SD Card" directory as this might change

user1089679
  • 2,328
  • 8
  • 41
  • 51
0

Add Permission to write external memory in your Manifest file.

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

Yuvi
  • 1,344
  • 4
  • 24
  • 46
  • Please cross check your folder name, it should be `Downloads` you can refer this link http://www.androidsnippets.com/download-an-http-file-to-sdcard-with-progress-notification or http://www.anddev.org/working_with_files-t115.html and also verify before write that dir exists or not ? – Yuvi Feb 03 '12 at 04:44