Download File from url in Android

private void downloadFile(String url) {

try {

String imageFile = url.substring(url.lastIndexOf(“/”) + 1, url.length());

      URL u = new URL(url);

      URLConnection conn = u.openConnection();

      int contentLength = conn.getContentLength();

      DataInputStream stream = new DataInputStream(u.openStream());

        byte[] buffer = new byte[contentLength];

        stream.readFully(buffer);

        stream.close();

        DataOutputStream fos = new DataOutputStream(new FileOutputStream(Outputfilepath + “/”+imageFile));

        fos.write(buffer);

        fos.flush();

        fos.close();

  } catch(FileNotFoundException e) {

      return; // swallow a 404

  } catch (IOException e) {

      return; // swallow a 404

  }

}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s