package com.example.download;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ProgressBar;

public class DownloadFileActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Button button = (Button) findViewById(R.id.button);
        button.setOnClickListener(new MyButtonListener());
    }

    class MyButtonListener implements OnClickListener {
        public void onClick(View v) {
            EditText editText = (EditText) findViewById(R.id.url);
            ProgressBar progress =
                    (ProgressBar) findViewById(R.id.progress);
            ImageView imageView =
                    (ImageView) findViewById(R.id.imageView);

            imageView.setImageBitmap(null);
            progress.setProgress(0);
            String url = editText.getText().toString();

            Bitmap bitmap = downloadImage(url, progress);

            if (bitmap != null) {
                imageView.setImageBitmap(bitmap);
            }
        }
    }

    private Bitmap downloadImage(String url, ProgressBar progressBar) {
        try {
            int bufferSize = 1024;

            URLConnection conn = new URL(url).openConnection();
            InputStream is = conn.getInputStream();
            int imageSize = conn.getContentLength();

            byte[] buffer = new byte[bufferSize];
            byte[] imageArray = new byte[imageSize];
            
            int numberOfBuffers = imageSize / bufferSize;
            progressBar.setMax(numberOfBuffers);

            int bytesRead = 0;
            int position = 0;
            int progress = 0;
            while ((bytesRead = is.read(buffer)) != -1) {
                // copy buffer into image array
                System.arraycopy(buffer, 0, imageArray, position,
                        bytesRead);
                position += bytesRead;
                // update the progress bar
                progressBar.setProgress(++progress);
            }

            return BitmapFactory.decodeByteArray(imageArray, 0,
                    imageArray.length);

            // You can replace the entire try block with these three
            // lines, but you wouldn't get the opportunity to update
            // the progress bar incrementally.
            //URLConnection conn = new URL(url).openConnection();
            //InputStream is = conn.getInputStream();
            // return BitmapFactory.decodeStream(is);
        }
        catch (IOException e) {
            System.err.println(e);
            return null;
        }
    }
}