Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <pre><code>package com.ProgressDialogDemo1; 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.graphics.Bitmap; import android.graphics.BitmapFactory; import android.os.AsyncTask; import android.os.Bundle; import android.os.Environment; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.ImageView; import android.widget.ProgressBar; public class ProgressDialogDemoActivity extends Activity { public static final int DIALOG_DOWNLOAD_PROGRESS = 0; private Button startBtn; private ProgressDialog mProgressDialog; ProgressBar prgBar1; ImageView imgv1; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); imgv1 = (ImageView) findViewById(R.id.imgv1); startBtn = (Button)findViewById(R.id.startBtn); prgBar1 = (ProgressBar) findViewById(R.id.prgBar1); prgBar1.setMax(100); startBtn.setOnClickListener(new OnClickListener(){ public void onClick(View v) { startDownload(); } }); } private void startDownload() { String[] url = {"http://animal.discovery.com/birds/peacock/pictures/peacock-picture.jpg", "http://www.funrocker.com/blog/wp-content/uploads/2010/04/Animals-Wild-Life-Jungle-FunRocker.Com-03.jpg", "http://www.thewallpapers.org/photo/5790/Nature_Wallpapers-037.jpg", "http://2.bp.blogspot.com/-j56yaqpfjVE/TnzTjcqnCjI/AAAAAAAAGPM/MzqmczFkC30/s1600/natural-pictures.jpg", "http://www.fantasy-and-art.com/wp-content/gallery/nature-wallpapers/red-tree-wallpaper-hd.jpg", "http://upload.wikimedia.org/wikipedia/commons/thumb/1/1a/Bachalpseeflowers.jpg/300px-Bachalpseeflowers.jpg"}; DownloadFileAsync dloadFAsync = new DownloadFileAsync(url); dloadFAsync.execute(url); } @Override protected Dialog onCreateDialog(int id) { switch (id) { case DIALOG_DOWNLOAD_PROGRESS: mProgressDialog = new ProgressDialog(this); mProgressDialog.setCancelable(false); return mProgressDialog; default: return null; } } // Async Task class DownloadFileAsync extends AsyncTask&lt;String, String, String&gt; { int current=0; String[] paths; String fpath; boolean show = false; public DownloadFileAsync(String[] paths) { super(); this.paths = paths; for(int i=0; i&lt;paths.length; i++) System.out.println((i+1)+": "+paths[i]); } @Override protected void onPreExecute() { super.onPreExecute(); } @Override protected String doInBackground(String... aurl) { int rows = aurl.length; while(current &lt; rows) { int count; try { System.out.println("Current: "+current+"\t\tRows: "+rows); fpath = getFileName(this.paths[current]); URL url = new URL(this.paths[current]); URLConnection conexion = url.openConnection(); conexion.connect(); int lenghtOfFile = conexion.getContentLength(); InputStream input = new BufferedInputStream(url.openStream(), 512); OutputStream output = new FileOutputStream("/sdcard/"+fpath); byte data[] = new byte[512]; long total = 0; while ((count = input.read(data)) != -1) { total += count; publishProgress(""+(int)((total*100)/lenghtOfFile)); output.write(data, 0, count); } show = true; output.flush(); output.close(); input.close(); current++; } catch (Exception e) {} } // while end return null; } @Override protected void onProgressUpdate(String... progress) { prgBar1.setProgress(Integer.parseInt(progress[0])); if(show) { File dir = Environment.getExternalStorageDirectory(); File imgFile = new File(dir, getFileName(this.paths[(current-1)])); Bitmap bmp = BitmapFactory.decodeFile(imgFile.getAbsolutePath()); imgv1.setImageBitmap(bmp); show = false; } } @Override protected void onPostExecute(String unused) { System.out.println("unused: "+unused); } } public String getFileName(String wholePath) { String name=null; int start,end; start=wholePath.lastIndexOf('/'); end=wholePath.length(); //lastIndexOf('.'); name=wholePath.substring((start+1),end); name = "images/"+name; System.out.println("Start:"+start+"\t\tEnd:"+end+"\t\tName:"+name); return name; } } </code></pre>
    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. VO
      singulars
      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