May 7, 2012

How to download a file from a remote site in android? – Another simple example – Method -3

Hello all …….

I have shown many examples on how to download and upload files in android through this site.

These are other methods for downloadign a file in android.

1. How to Download an image in ANDROID programatically?
2. How to download a file to your android device from a remote server with a custom progressbar showing progress?

I have shown four methods to upload an image to a server.
Check these posts to refer this.

1. Uploading audio, video or image files from Android to server
2. How to Upload Multiple files in one request along with other string parameters in android?
3. ANDROID – Upload an image to a server.
4. How to upload an image from Android device to server? – Method 4

This is yet another example on how to do download a file.

So this is the java code that downloads the file.

package com.coderzheaven.pack;import;import;import;import;import;import;import;import;import;import;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;public class DownloadFileDemo extends Activity {String dwnload_file_path = "";String dest_file_path = "/sdcard/dwnloaded_file.png";Button b1;ProgressDialog dialog = null;@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);b1 = (Button)findViewById(;b1.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {dialog =, "", "Downloading file...", true);new Thread(new Runnable() {public void run() {downloadFile(dwnload_file_path, dest_file_path);}}).start();}});}public void downloadFile(String url, String dest_file_path) {try {File dest_file = new File(dest_file_path);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(dest_file));fos.write(buffer);fos.flush();fos.close();hideProgressIndicator();} catch(FileNotFoundException e) {hideProgressIndicator();return;} catch (IOException e) {hideProgressIndicator();return;}}void hideProgressIndicator(){runOnUiThread(new Runnable() {public void run() {dialog.dismiss();}});}}

This is the xml file that contains the button.


<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android=""android:orientation="vertical"android:layout_width="fill_parent"android:layout_height="fill_parent"><TextViewandroid:layout_width="fill_parent"android:layout_height="wrap_content"android:text="File Download Demo from Coderzheaven \n\nFile to download : \n\nSaved Path : sdcard/\n"/><Buttonandroid:text="Download File"android:id="@+id/Button01"android:layout_width="wrap_content"android:layout_height="wrap_content"></Button></LinearLayout>

Note : Please add these two permissions in the AndroidManifest.xml

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

Please leave your comments and also share this post if you like it.

Website PinFacebook TwitterMyspaceFriendfeedTechnoratidel.icio.usDiggGoogleStumbleUponPremium Responsive

Leave a Reply Cancel reply