this is the code to download a file from server using progress bar!!!!
 hope might be you get some idea!!!
import android.app.Activity;
import android.os.Bundle;
import java.io.BufferedInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.net.URLConnection;




import android.app.Dialog;
import android.app.ProgressDialog;
//import android.content.Intent;
import android.os.AsyncTask;

import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class UploadingFile extends Activity {

    public static final int DIALOG_DOWNLOAD_PROGRESS = 0;
    private Button startBtn;
    private Button next;
    private ProgressDialog mProgressDialog;
Activity m_activity = this;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main3);
        startBtn = (Button)findViewById(R.id.startBtn);
        next = (Button) findViewById(R.id.Button02);




        startBtn.setOnClickListener(new OnClickListener(){
            public void onClick(View v) {
        //     handleButtonClick();

            //Intent myIntent = new Intent(v.getContext(),
UploadingFile.class);
              //  startActivityForResult(myIntent, 0);
            //   finish();
                startDownload();



            }
        });
    }

    private void startDownload() {
        String url = "
http://115.115.95.74/mobileapp/test60/uploads/phonedata.txt";;
        new DownloadFileAsync().execute(url);
    }
    @Override
    protected Dialog onCreateDialog(int id) {
        switch (id) {
case DIALOG_DOWNLOAD_PROGRESS:
mProgressDialog = new ProgressDialog(this);
mProgressDialog.setMessage("Downloading file..");
Toast.makeText(this, "File Downloaded Sucessfully.....Please check your
file into sdcard",
Toast.LENGTH_LONG).show();
mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
mProgressDialog.setCancelable(false);
mProgressDialog.show();
return mProgressDialog;
default:
return null;
        }
    }

class DownloadFileAsync extends AsyncTask<String, String, String> {

@Override
protected void onPreExecute() {
super.onPreExecute();
showDialog(DIALOG_DOWNLOAD_PROGRESS);
}

@Override
protected String doInBackground(String... aurl) {
int count;

try {

URL url = new URL(aurl[0]);
URLConnection conexion = url.openConnection();
conexion.connect();

int lenghtOfFile = conexion.getContentLength();
Log.d("ANDRO_ASYNC", "Lenght of file: " + lenghtOfFile);

InputStream input = new BufferedInputStream(url.openStream());
OutputStream output = new
FileOutputStream("/sdcard/download/phonedata.txt");

byte data[] = new byte[1024];

long total = 0;

while ((count = input.read(data)) != -1) {
total += count;
publishProgress(""+(int)((total*100)/lenghtOfFile));
 output.write(data, 0, count);
}

output.flush();
output.close();
input.close();
 if(next != null){

             m_activity.finish();


         }
 } catch (Exception e) {}
return null;

}
protected void onProgressUpdate(String... progress) {
 Log.d("ANDRO_ASYNC",progress[0]);
 mProgressDialog.setProgress(Integer.parseInt(progress[0]));
}

@Override
protected void onPostExecute(String unused) {
dismissDialog(DIALOG_DOWNLOAD_PROGRESS);
}}



}

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to