package com.downloadimageshowinghorizontalprogressbar_android_examples.com;

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.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.graphics.drawable.Drawable;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;


public class MainActivity extends Activity {

	Button button;
    ProgressDialog progressdialog;
	public static final int Progress_Dialog_Progress = 0;
	String ImageURL = "http://www.android-examples.com/wp-content/uploads/2016/04/demo_download_image.jpg" ;
	URL url;
	URLConnection urlconnection ;
	int FileSize;
	InputStream inputstream;
	OutputStream outputstream;
	byte dataArray[] = new byte[1024];
	long totalSize = 0;
	ImageView imageview;
	String GetPath ;
	
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        button = (Button)findViewById(R.id.button1);
        imageview = (ImageView)findViewById(R.id.imageView1);
        
        button.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
		
		        new ImageDownloadWithProgressDialog().execute(ImageURL);
				
			}
		});
    }

public class ImageDownloadWithProgressDialog extends AsyncTask<String, String, String> {
   
	@Override
	protected void onPreExecute() {
		
		super.onPreExecute();
		
		showDialog(Progress_Dialog_Progress);
	}

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

	try {

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

	FileSize = urlconnection.getContentLength();

	inputstream = new BufferedInputStream(url.openStream());
	outputstream = new FileOutputStream("/sdcard/demo_photo1.jpg");


		while ((count = inputstream.read(dataArray)) != -1) {
			
			totalSize += count;
			
			publishProgress(""+(int)((totalSize*100)/FileSize));
			
			outputstream.write(dataArray, 0, count);
		}

		outputstream.flush();
		outputstream.close();
		inputstream.close();
		
	} catch (Exception e) {}
	return null;

	}
	protected void onProgressUpdate(String... progress) {
		
		 progressdialog.setProgress(Integer.parseInt(progress[0]));
	}

	@Override
	protected void onPostExecute(String unused) {
		
		dismissDialog(Progress_Dialog_Progress);
		
		GetPath = Environment.getExternalStorageDirectory().toString() + "/demo_photo1.jpg";
		
        imageview.setImageDrawable(Drawable.createFromPath(GetPath));
		
		Toast.makeText(MainActivity.this, "Image Downloaded Successfully", Toast.LENGTH_LONG).show();
	}
}

@Override
protected Dialog onCreateDialog(int id) {
    switch (id) {
	case Progress_Dialog_Progress:
		
		progressdialog = new ProgressDialog(MainActivity.this);
		progressdialog.setMessage("Downloading Image From Server...");
		progressdialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
		progressdialog.setCancelable(false);
		progressdialog.show();
		return progressdialog;
		
	default:
		
		return null;
    }
}

}