0

I'm getting the links to the files using this code:

private List<String> getFilesList(String path, String idcity){
    List<String> files = new ArrayList<String>();

    String readJSON = readJSON(path,idcity);

    try {
        JSONArray jsonArray = new JSONArray(readJSON);
        for (int i = 0; i < jsonArray.length(); i++) {
            JSONObject jsonObject = jsonArray.getJSONObject(i);
            files.add(jsonObject.getString("file"));
        }
    } catch (Exception e) {
        Log.e("getFilesList", "Error parsing data ", e);
    }

    return files;
}

Now I want to download all this files to SDCard.

I think that this can be achieved using AsyncTask (and show a ProgressDialog with the information of how many files are left to download).

I'm using Android API 4, so I can't use DownloadManager.

Can you guys provide me some code, assuming that the links are in a List<String>?

Thanks in advance!

silentw
  • 4,835
  • 4
  • 25
  • 45

2 Answers2

3

Use this function in a loop which loops through your List of URLs

Edited: Using earlier code, if you download a file larger than 4MB, it would give OutOfMemoryError. Using this one, it works just great!

Source: SO Question

public boolean downloadFromUrl(String url, String outputFileName) {

        File dir = new File(Environment.getExternalStorageDirectory() + "/"
                + getString(R.string.directory_appBase) + "/");
        if (!dir.exists()) {
            dir.mkdirs();
        }
        try {
            File file = new File(dir, outputFileName);
            URL downloadUrl = new URL(url);
            URLConnection ucon = downloadUrl.openConnection();
            ucon.connect();

            InputStream is = ucon.getInputStream();

            FileOutputStream fos = new FileOutputStream(file);  

            byte data[] = new byte[1024];

            int current = 0;
            while ((current = is.read(data)) != -1) {
                fos.write(data, 0, current);
            }        
            is.close();
            fos.flush();
            fos.close();
            isFileDownloaded=true;
        } catch (IOException e) {
            e.printStackTrace();
            isFileDownloaded = false;
            System.out.println(outputFileName + " not downloaded");

        }
        if (isFileDownloaded)
            System.out.println(outputFileName + " downloaded");
        return isFileDownloaded;

    }
Community
  • 1
  • 1
Vinay W
  • 9,912
  • 8
  • 41
  • 47
  • It works good for small size files, but when it tries to download a file with a size of 4MB+ it gives me a OutOfMemory Error... Any solution? – silentw Jul 12 '12 at 11:09
  • are you using an emulator or an actual device? – Vinay W Jul 12 '12 at 11:22
  • Emulator. I edited your answer with a code that is working perfectly on the emulator. Thanks a lot for your answer, my problem is now fixed! :) – silentw Jul 12 '12 at 11:34
1

just take the DownloadManager source and add it to your project. it has very few dependencies.

http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/2.3_r1/android/app/DownloadManager.java#DownloadManager

DownloadManager does a lot of stuff that would be a major pain to implement on your own. sure, it call comes down to doing an HTTP GET, but the devil's in the details.

Jeffrey Blattman
  • 22,176
  • 9
  • 79
  • 134
  • As you can see, [DownloadManager](http://developer.android.com/reference/android/app/DownloadManager.html) requires API Level 9, and I am working with API Level 4, so DownloadManager is not an option. – silentw Jul 11 '12 at 18:36
  • no, you misunderstood. down't use it from the SDK, grab the source, and add the source file to your project. like said, it appears to habe very few dependencies on the android SDK that wouldn't be present in API level 4. – Jeffrey Blattman Jul 11 '12 at 19:34
  • Can you please provide some code, using that source, to loop through the files and download each of them and wrap it in an asynctask (because I want to place a progressdialog)... – silentw Jul 11 '12 at 19:37
  • i linked to the code above. someone took a lot of time to write that and get it right. you should use it. – Jeffrey Blattman Jul 11 '12 at 20:37