0

I need function that download files by passing name and urls, so that i can download series of files in android app.

So went ahead and created something like this:

public void doDownload (String fileName, String url){
    File root = android.os.Environment.getExternalStorageDirectory();               
    File dir = new File (root.getAbsolutePath() + "/Content/"); 
    if(dir.exists()==false) {
        dir.mkdirs();
    }

    try{
        URL url = new URL(url);
        URLConnection connection = url.openConnection();
        connection.connect();
        // this will be useful so that you can show a typical 0-100% progress bar
        int fileLength = connection.getContentLength();
        Log.i("ANDRO_ASYNC", "Lenght of file: " + fileLength);

        // download the file
        InputStream input = new BufferedInputStream(url.openStream());
        OutputStream output = new FileOutputStream(dir + fileName);   
        byte data[] = new byte[1024];
        long total = 0;
        int count;
        while ((count = input.read(data)) != -1) {
            total += count;

            output.write(data, 0, count);
        }

        output.flush();
        output.close();
        input.close();
    }catch(Exception e){
    }
} 

XML: Added permission

Now when i call this function:

doDownload(String img.png, String http://server.com);

It returns no files. What is wrong here?

sparkhee93
  • 1,381
  • 3
  • 21
  • 30
DPP
  • 12,716
  • 3
  • 49
  • 46
  • currently u are calling `doDownload` method inside loop to download more then one file? – ρяσѕρєя K Mar 21 '13 at 04:48
  • No im calling this method in, after user pick the option, e.g: once user pick option 1, than option 1 files url and name will execute with doDownload(url, filename) to download and store file in sdcard. – DPP Mar 21 '13 at 04:52
  • Have a look at similar question http://stackoverflow.com/questions/3028306/download-a-file-with-android-and-showing-the-progress-in-a-progressdialog – GrIsHu Mar 21 '13 at 04:59
  • Yes, and i did try to do function like `protected String doInBackground(String... sUrl, String fileName)` and try to pass file name but didnt work. – DPP Mar 21 '13 at 05:01
  • What error messages are you getting? When you step through your code, where is it breaking? – 323go Mar 21 '13 at 05:49
  • ERROR: `android.os.NetwirkOnMainThreadException` – DPP Mar 21 '13 at 05:58
  • Sounds like you're not threading your download. Is this true? – Grambot Mar 22 '13 at 00:37
  • @TheCapn: Sorry but im new to android, so should i be doing it? – DPP Mar 22 '13 at 00:40
  • @Dixit - Yes, your `NetworkOnMainThreadException` indicates you're trying to download something from the main thread which isn't allowed. They do this to prevent lengthy pauses which appear to the user as if the program froze. – Grambot Mar 22 '13 at 17:46
  • Thanks you all, I have fixed it! Also you can add me on google + https://plus.google.com/103405375045630693169/ – DPP Mar 24 '13 at 23:14

1 Answers1

0

Another option is using the download manager to download the files. you can try using DownloadManager. see the android developer site. check this link: http://developer.android.com/reference/android/app/DownloadManager.html

krishna
  • 142
  • 7
  • 12