Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Use Asynctask to download file from server this is the code to download a file from server with Progress dialog and works fine.. please try it..</p> <pre><code> package .....; import java.io.BufferedInputStream; import java.io.File; import java.io.FileOutputStream; import java.io.InputStream; import java.io.OutputStream; import java.net.URL; import java.net.URLConnection; import android.app.Activity; import android.app.Dialog; import android.app.ProgressDialog; import android.os.AsyncTask; import android.os.Bundle; import android.os.Environment; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.EditText; public class facebook1 extends Activity { Button btnShowProgress; private ProgressDialog pDialog; public static final int progress_bar_type = 0; private static String file_url = "yoururl"; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.layout); btnShowProgress = (Button) findViewById(R.id.submit1); btnShowProgress.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { new DownloadFileFromURL().execute(file_url); } }); } @Override protected Dialog onCreateDialog(int id) { switch (id) { case progress_bar_type: pDialog = new ProgressDialog(this); pDialog.setMessage("Please wait....."); pDialog.setIndeterminate(false); pDialog.setMax(100); pDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); pDialog.setCancelable(true); pDialog.show(); return pDialog; default: return null; } } class DownloadFileFromURL extends AsyncTask&lt;String, String, String&gt; { protected static final int BUFFER_SIZE = 0; @SuppressWarnings("deprecation") @Override protected void onPreExecute() { super.onPreExecute(); showDialog(progress_bar_type); } /** * Downloading file in background thread * */ @Override protected String doInBackground(String... f_url) { int count; try { URL url = new URL(f_url[0]); URLConnection conection = url.openConnection(); conection.connect(); int lenghtOfFile = conection.getContentLength(); InputStream input = new BufferedInputStream(url.openStream()); File downloadloc = Environment.getExternalStorageDirectory(); OutputStream output = new FileOutputStream(downloadloc + "/filenam.ext"); //type the name of file with extension. 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(); } catch (Exception e) { Log.e("Error: ", e.getMessage()); } return null; } protected void onProgressUpdate(String... progress) { pDialog.setProgress(Integer.parseInt(progress[0])); } @Override protected void onPostExecute(String file_url) { dismissDialog(progress_bar_type); }} } </code></pre> <p>Manifest File:</p> <pre><code>&lt;uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" &gt; &lt;/uses-permission&gt; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload