Android Open Source - SELP2013 File Downloader






From Project

Back to project page SELP2013.

License

The source code is released under:

License ======= This work is licensed under the BSD 2-clause license as follows. # BSD 2-clause license Copyright (c) 2013, Sky Welch All rights reserved. Redistribution and use in source and ...

If you think the Android project SELP2013 listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package uk.co.skywelch.selp2013;
//from w  w w .  j a va 2  s. co  m
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import android.os.AsyncTask;
import android.os.Environment;
import android.util.Log;

public class FileDownloader extends AsyncTask<String, Integer, Boolean> {
  public static final String TAG = "FileDownloader";
  public File file;

  @Override
  protected Boolean doInBackground(String... params) {
    try {
      URL url = new URL(params[0]);
      String path = Environment.getExternalStorageDirectory().getPath() + "/"
          + TimetableActivity.SDRoot + "/data/";

      File SDCardRoot = new File(path);
      file = new File(SDCardRoot, params[1]);
      Log.d(TAG, "Downloading " + file.getPath());

      // Open a connection to the given URL
      HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
      urlConnection.setRequestMethod("GET");
      urlConnection.setDoOutput(true);
      urlConnection.connect();

      // Set up folders for storage if they don't already exist
      if (!SDCardRoot.exists())
        SDCardRoot.mkdirs();
      if (!file.exists())
        file.createNewFile();
      FileOutputStream fileOutput = new FileOutputStream(file);

      InputStream inputStream = urlConnection.getInputStream();
      int totalSize = urlConnection.getContentLength();
      int downloadedSize = 0;

      // Read in 1024 bytes at a time to a buffer, publish progress
      byte[] buffer = new byte[1024];
      int bufferLength = 0;
      while ((bufferLength = inputStream.read(buffer)) > 0) {
        fileOutput.write(buffer, 0, bufferLength);
        downloadedSize += bufferLength;

        float progress = ((float) downloadedSize / (float) totalSize) * 100;
        publishProgress(Float.valueOf(progress).intValue());
      }

      fileOutput.close();
      return true;
    } catch (MalformedURLException e) {
      Log.e(TAG, "Bad URL passed to FileDownloader: " + e.getLocalizedMessage());
    } catch (IOException e) {
      Log.e(TAG, "Failed to process output file: " + e.getLocalizedMessage());
    }

    return false;
  }
}




Java Source Code List

uk.co.skywelch.selp2013.AboutActivity.java
uk.co.skywelch.selp2013.CourseInformationActivity.java
uk.co.skywelch.selp2013.CourseListActivity.java
uk.co.skywelch.selp2013.CourseListAdapter.java
uk.co.skywelch.selp2013.Course.java
uk.co.skywelch.selp2013.CoursesParseTask.java
uk.co.skywelch.selp2013.CoursesXmlParser.java
uk.co.skywelch.selp2013.DrawerListAdapter.java
uk.co.skywelch.selp2013.FileDownloader.java
uk.co.skywelch.selp2013.HourMinute.java
uk.co.skywelch.selp2013.Lecture.java
uk.co.skywelch.selp2013.ListItem.java
uk.co.skywelch.selp2013.ListSection.java
uk.co.skywelch.selp2013.StringListItem.java
uk.co.skywelch.selp2013.TimetableActivity.java
uk.co.skywelch.selp2013.TimetableFragment.java
uk.co.skywelch.selp2013.TimetableListAdapter.java
uk.co.skywelch.selp2013.TimetableParseTask.java
uk.co.skywelch.selp2013.TimetableXmlParser.java
uk.co.skywelch.selp2013.VenueInformationActivity.java
uk.co.skywelch.selp2013.Venue.java
uk.co.skywelch.selp2013.VenuesParseTask.java
uk.co.skywelch.selp2013.VenuesXmlParser.java
uk.co.skywelch.selp2013.XMLUtilities.java