package com.example.patrice.rerservation01;


import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

public class upload_file
{

    private Bitmap bitmap = null;

    public void upload_image()
    {

        async_download_image objet = new async_download_image();
        objet.execute();
    }


    public class async_download_image extends AsyncTask<String, String, Bitmap>
    {

        protected void onPreExecute()
        {
            super.onPreExecute();
        }

        @Override
        protected Bitmap doInBackground(String... arg0)
        {
            HttpURLConnection connection = null;
            URL url = null;

            try
            {
                url = new URL("http://www.analog-design.net/WORKAREA/WEBSITE/Codebar/kateupton.jpeg");
                connection = (HttpURLConnection) url.openConnection();
                //connection.addRequestProperty(BConstant.WEB_SERVICES_COOKIES, cookie);
                connection.setDoOutput(true);
                connection.connect();

                ////// OK /////
                //URL url_str = new URL("http://www.analog-design.net/WORKAREA/WEBSITE/Codebar/caps.jpg");
                //InputStream is = new BufferedInputStream(url_str.openStream(), 8192);
                //bitmap = BitmapFactory.decodeStream(is);
            }
            catch (IOException e1)
            {
                e1.printStackTrace();
            }

            String filename = "kateupton.jpg";
            File sd = Environment.getExternalStorageDirectory();
            File dest = new File(sd, filename);

            try
            {
                InputStream inputStream = connection.getInputStream();
                BufferedInputStream bufferedInputStream = null;
                bufferedInputStream = new BufferedInputStream(inputStream, 1024 * 5);

                // bitmap = BitmapFactory.decodeStream(inputStream);

                FileOutputStream fileOutputStream = new FileOutputStream(dest); // folderDir + "/" + fName
                byte[] buffer = new byte[1024];
                int len1 = 0;

                while ((len1 = inputStream.read(buffer)) != -1)
                {
                    fileOutputStream.write(buffer, 0, len1);
                }

                bitmap = BitmapFactory.decodeStream(inputStream);

                bufferedInputStream.close();
                fileOutputStream.close();
                inputStream.close();
                connection.disconnect();
            }
            catch (Exception e)
            {
                e.printStackTrace();
            }

            return bitmap;
        }

        @Override
        protected void onPostExecute(Bitmap image)
        {

            if (image != null)
            {
                String resultat = "Bingo";
            }
        }

/*
        private boolean isNetworkAvailable()
        {


            ConnectivityManager connMgr = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);


            NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();

            if (networkInfo != null && networkInfo.isAvailable())
                available = true;


            return available;
        }
*/

        private Bitmap downloadUrl(String strUrl) throws IOException
        {
            Bitmap bitmap = null;
            InputStream iStream = null;

            try {
                URL url = new URL(strUrl);
                /** Creating an http connection to communcate with url */
                HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();

                /** Connecting to url */
                urlConnection.connect();

                /** Reading data from url */
                iStream = urlConnection.getInputStream();

                /** Creating a bitmap from the stream returned from the url */
                bitmap = BitmapFactory.decodeStream(iStream);

            } catch (Exception e) {
                Log.d("Exception url", e.toString());
            } finally {
                iStream.close();
            }

            return bitmap;
        }

        private class DownloadTask extends AsyncTask<String, Integer, Bitmap>
        {
            Bitmap bitmap = null;

            @Override

            protected Bitmap doInBackground(String... url)
            {
                try {
                    bitmap = downloadUrl(url[0]);
                } catch (Exception e) {
                    Log.d("Background Task", e.toString());
                }

                return bitmap;
            }

            @Override
            protected void onPostExecute(Bitmap result) {
                //ImageView iView = (ImageView) findViewById(R.id.iv_image);
                //iView.setImageBitmap(result);
                String res = "Super";
            }
        }

    }
}