Note that there are some explanatory texts on larger screens.

plurals
  1. PODownload File Progress Dialog
    primarykey
    data
    text
    <p>I want to perform a file download and show a dialog. But when I try to show the dialog I get a error, any help?</p> <p>The problem is at <code>showDialog(DIALOG_DOWNLOAD_PROGRESS);</code></p> <p>Here is the error message : The method showDialog(int) from the type Activity is deprecated</p> <p>Heres my code:</p> <pre><code>public class download extends Activity { public static final int DIALOG_DOWNLOAD_PROGRESS = 0; private Button startBtn; private ProgressDialog mProgressDialog; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); startBtn = (Button)findViewById(R.id.startBtn); startBtn.setOnClickListener(new OnClickListener(){ public void onClick(View v) { startDownload(); } }); } private void startDownload() { String url = "http://practicalbuddhist.com/wp-content/uploads/2012/03/Synapses-Image-for-March-17-2010-Blog-Entry.jpg"; new DownloadFileAsync().execute(url); } @Override protected Dialog onCreateDialog(int id) { switch (id) { case DIALOG_DOWNLOAD_PROGRESS: mProgressDialog = new ProgressDialog(this); mProgressDialog.setMessage("Running Download Test..."); mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); mProgressDialog.setCancelable(false); mProgressDialog.show(); return mProgressDialog; default: return null; } } </code></pre> <p>class DownloadFileAsync extends AsyncTask {</p> <pre><code>@SuppressWarnings("deprecation") @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/Downloadtest.jpg"); 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) {} return null; } protected void onProgressUpdate(String... progress) { Log.d("ANDRO_ASYNC",progress[0]); mProgressDialog.setProgress(Integer.parseInt(progress[0])); } @SuppressWarnings("deprecation") @Override protected void onPostExecute(String unused) { dismissDialog(DIALOG_DOWNLOAD_PROGRESS); } </code></pre> <p>} }</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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