Tuesday, April 23, 2013

Download a File In Android From Remote Server

Download a File In Android From Remote Server


In Your class file put add this code 


package com.facebook.samples.sessionlogin;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URL;
import java.net.URLConnection;
import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button; 


public class HTTPTest extends Activity {
String dwnload_file_path = "https://fbcdn-profile-a.akamaihd.net/hprofile-ak-snc6/187259_10000060421658402_744490318028_q.jpg";
    String dest_file_path = "/sdcard/dwnloaded_file.png";
    Button b1;
    ProgressDialog dialog = null;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
         
        b1 = (Button)findViewById(R.id.Button01);
        b1.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                 
                dialog = ProgressDialog.show(HTTPTest.this, "", "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();
            }
        });  
    }
}

In your manifestation file add permission for

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

In your xml file add 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="File Download Demo from Coderzheaven \n\nFile to download : http://coderzheaven.com/sample_folder/sample_file.png \n\nSaved Path : sdcard/\n"
    />
<Button
    android:text="Download File"
    android:id="@+id/Button01"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">
</Button>
</LinearLayout>


And you are good to go.........

4 comments:

  1. hey I did just this and nothing happen? my sdcard folder is empty I need help urgently!

    ReplyDelete
  2. Hi I am new to android, it works fine, thanks. but I want to download a file with progessupdate.

    ReplyDelete
  3. Thnaks fr the example. can u advise me few server where i can save them and retrieve them as need for my application.

    ReplyDelete