package ajou.butterflyeffect.touchablememory;

import android.app.ProgressDialog;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.os.AsyncTask;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.TextViewCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Base64;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;


public class Picture extends AppCompatActivity {

    ViewPager pager;

    String attachmentName = "bitmap";
    String attachmentFileName = "bitmap.bmp";
    String crlf = "\r\n";
    String twoHyphens = "--";
    String boundary =  "*****";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_picture);

        Intent intent = getIntent();
        String a = (String)intent.getExtras().get("index");

        pager = (ViewPager)findViewById(R.id.pager);
        CustomAdapter adapter = new CustomAdapter(getLayoutInflater());
        pager.setAdapter(adapter);

        ImageView img = (ImageView)findViewById(R.id.img_viewpager_childimage) ;
        img.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                try {
                    Resources r = Picture.this.getResources();
                    BitmapDrawable bd = (BitmapDrawable) r.getDrawable(R.drawable.rose);
                    Bitmap bitmap = bd.getBitmap();

                    HttpURLConnection httpURLConnection = null;
                    URL url = new URL("");
                    httpURLConnection = (HttpURLConnection) url.openConnection();
                    httpURLConnection.setUseCaches(false);
                    httpURLConnection.setDoOutput(true);

                    httpURLConnection.setRequestMethod("POST");
                    httpURLConnection.setRequestProperty("Connection", "Keep-Alive");
                    httpURLConnection.setRequestProperty("Cache-Control", "no-cahce");
                    httpURLConnection.setRequestProperty("Content-Type", "multipart/form-data;boundary="+Picture.this.boundary);

                    DataOutputStream request = new DataOutputStream(httpURLConnection.getOutputStream());
                    request.writeBytes(Picture.this.twoHyphens + Picture.this.boundary + Picture.this.crlf);
                    request.writeBytes("Content-Disposition: form-data; name=\"" + Picture.this.attachmentName + "\";filename=\"" + Picture.this.attachmentFileName + "\"" + Picture.this.crlf);
                    request.writeBytes(Picture.this.crlf);


                    byte[] pixels = new byte[bitmap.getWidth() * bitmap.getHeight()];
                    for (int i = 0; i < bitmap.getWidth(); ++i) {
                        for (int j = 0; j < bitmap.getHeight(); ++j) {
                            //we're interested only in the MSB of the first byte,
                            //since the other 3 bytes are identical for B&W images
                            pixels[i + j] = (byte) ((bitmap.getPixel(i, j) & 0x80) >> 7);
                        }
                    }

                    request.write(pixels);



                    request.writeBytes(Picture.this.crlf);
                    request.writeBytes(Picture.this.twoHyphens + Picture.this.boundary + Picture.this.twoHyphens + Picture.this.crlf);

                    request.flush();
                    request.close();


                    InputStream responseStream = new BufferedInputStream(httpURLConnection.getInputStream());

                    BufferedReader responseStreamReader =
                            new BufferedReader(new InputStreamReader(responseStream));

                    String line = "";
                    StringBuilder stringBuilder = new StringBuilder();

                    while ((line = responseStreamReader.readLine()) != null) {
                        stringBuilder.append(line).append("\n");
                    }
                    responseStreamReader.close();

                    String response = stringBuilder.toString();

                    Log.d("dbg>>",response);

                    responseStream.close();

                    httpURLConnection.disconnect();



                }
                catch(Exception e){
                    e.printStackTrace();
                }
            }
        });


        Button btn_cancel = (Button)findViewById(R.id.picture_btn_cancel);
        btn_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Picture.super.onBackPressed();
            }
        });


    }




}

@@ -1640,3360 +1640,8 @@
 );%0A%0A
-        ImageView img = (ImageView)findViewById(R.id.img_viewpager_childimage) ;%0A        img.setOnClickListener(new View.OnClickListener() %7B%0A            @Override%0A            public void onClick(View v) %7B%0A%0A                try %7B%0A                    Resources r = Picture.this.getResources();%0A                    BitmapDrawable bd = (BitmapDrawable) r.getDrawable(R.drawable.rose);%0A                    Bitmap bitmap = bd.getBitmap();%0A%0A                    HttpURLConnection httpURLConnection = null;%0A                    URL url = new URL(%22%22);%0A                    httpURLConnection = (HttpURLConnection) url.openConnection();%0A                    httpURLConnection.setUseCaches(false);%0A                    httpURLConnection.setDoOutput(true);%0A%0A                    httpURLConnection.setRequestMethod(%22POST%22);%0A                    httpURLConnection.setRequestProperty(%22Connection%22, %22Keep-Alive%22);%0A                    httpURLConnection.setRequestProperty(%22Cache-Control%22, %22no-cahce%22);%0A                    httpURLConnection.setRequestProperty(%22Content-Type%22, %22multipart/form-data;boundary=%22+Picture.this.boundary);%0A%0A                    DataOutputStream request = new DataOutputStream(httpURLConnection.getOutputStream());%0A                    request.writeBytes(Picture.this.twoHyphens + Picture.this.boundary + Picture.this.crlf);%0A                    request.writeBytes(%22Content-Disposition: form-data; name=%5C%22%22 + Picture.this.attachmentName + %22%5C%22;filename=%5C%22%22 + Picture.this.attachmentFileName + %22%5C%22%22 + Picture.this.crlf);%0A                    request.writeBytes(Picture.this.crlf);%0A%0A%0A                    byte%5B%5D pixels = new byte%5Bbitmap.getWidth() * bitmap.getHeight()%5D;%0A                    for (int i = 0; i %3C bitmap.getWidth(); ++i) %7B%0A                        for (int j = 0; j %3C bitmap.getHeight(); ++j) %7B%0A                            //we're interested only in the MSB of the first byte,%0A                            //since the other 3 bytes are identical for B&W images%0A                            pixels%5Bi + j%5D = (byte) ((bitmap.getPixel(i, j) & 0x80) %3E%3E 7);%0A                        %7D%0A                    %7D%0A%0A                    request.write(pixels);%0A%0A%0A%0A                    request.writeBytes(Picture.this.crlf);%0A                    request.writeBytes(Picture.this.twoHyphens + Picture.this.boundary + Picture.this.twoHyphens + Picture.this.crlf);%0A%0A                    request.flush();%0A                    request.close();%0A%0A%0A                    InputStream responseStream = new BufferedInputStream(httpURLConnection.getInputStream());%0A%0A                    BufferedReader responseStreamReader =%0A                            new BufferedReader(new InputStreamReader(responseStream));%0A%0A                    String line = %22%22;%0A                    StringBuilder stringBuilder = new StringBuilder();%0A%0A                    while ((line = responseStreamReader.readLine()) != null) %7B%0A                        stringBuilder.append(line).append(%22%5Cn%22);%0A                    %7D%0A                    responseStreamReader.close();%0A%0A                    String response = stringBuilder.toString();%0A%0A                    Log.d(%22dbg%3E%3E%22,response);%0A%0A                    responseStream.close();%0A%0A                    httpURLConnection.disconnect();%0A%0A%0A%0A                %7D%0A                catch(Exception e)%7B%0A                    e.printStackTrace();%0A                %7D%0A            %7D%0A        %7D);
 %0A%0A%0A 

fix nullpointException