java - Null Error Trying to Upload picture from android phone to a php file to my computer -


i'm getting null error when try upload picture phone php file sends computer. file not getting computer. app works when upload picture shows when try send computer sends null error.

this line of code gives error.

httpresponse response = httpclient.execute(httppost); 

any ideas? thanks! java class below , php file follows.

 package test.example;  import java.io.bytearrayoutputstream; import java.io.ioexception; import java.io.inputstream; import java.nio.bytebuffer; import java.util.arraylist;  import org.apache.http.httpresponse; import org.apache.http.namevaluepair; import org.apache.http.client.httpclient; import org.apache.http.client.entity.urlencodedformentity; import org.apache.http.client.methods.httppost; import org.apache.http.impl.client.defaulthttpclient; import org.apache.http.message.basicnamevaluepair;  import android.app.activity; import android.content.intent; import android.database.cursor; import android.graphics.bitmap; import android.graphics.bitmapfactory; import android.graphics.bitmap.compressformat; import android.net.uri; import android.os.bundle; import android.provider.mediastore; import android.view.view; import android.widget.button; import android.widget.imageview; import android.widget.toast;  public class imagegallerydemoactivity extends activity {     inputstream inputstream;      private static int result_load_image = 1;      @override     public void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.main2);          button buttonloadimage = (button) findviewbyid(r.id.buttonloadpicture);         buttonloadimage.setonclicklistener(new view.onclicklistener() {              @override             public void onclick(view arg0) {                  intent = new intent(                         intent.action_pick,                         android.provider.mediastore.images.media.external_content_uri);                  startactivityforresult(i, result_load_image);             }         });     }      @override     protected void onactivityresult(int requestcode, int resultcode, intent data) {         super.onactivityresult(requestcode, resultcode, data);          if (requestcode == result_load_image && resultcode == result_ok                 && null != data) {             uri selectedimage = data.getdata();             string[] filepathcolumn = { mediastore.images.media.data };              cursor cursor = getcontentresolver().query(selectedimage,                     filepathcolumn, null, null, null);             cursor.movetofirst();              int columnindex = cursor.getcolumnindex(filepathcolumn[0]);             string picturepath = cursor.getstring(columnindex);             cursor.close();              imageview imageview = (imageview) findviewbyid(r.id.imgview);             // imageview.setimagebitmap(bitmapfactory.decodefile(picturepath));             bitmap bitmap;             try {                 bitmap = bitmapfactory.decodefile(picturepath);                 imageview.setimagebitmap(bitmap);                 bytearrayoutputstream stream = new bytearrayoutputstream();                 bitmap.compress(compressformat.png, 90, stream); // compress                                                                     //                                                                     // format                                                                     // want.                 int bytes = bitmap.getwidth()*bitmap.getheight()*4; //calculate how many bytes our image consists of. use different value 4 if don't use 32bit images.                 bytebuffer bytebuffer = bytebuffer.allocate(bytes);                  bitmap.copypixelstobuffer(bytebuffer);                  byte[] byte_arr = bytebuffer.array();//stream.tobytearray();                 string image_str = base64.encodebytes(byte_arr);                 arraylist<namevaluepair> namevaluepairs = new arraylist<namevaluepair>();                  namevaluepairs.add(new basicnamevaluepair("image", image_str));                  try {                     httpclient httpclient = new defaulthttpclient();                     httppost httppost = new httppost(                             "http://192.168.1.4/upload_image_android/upload_image.php");                      httppost.setentity(new urlencodedformentity(namevaluepairs));                  httpresponse response = httpclient.execute(httppost);                      string the_string_response =                      convertresponsetostring(response);                      toast.maketext(imagegallerydemoactivity.this,                  "response " + the_string_response, toast.length_long)              .show();                 } catch (exception e) {                     toast.maketext(imagegallerydemoactivity.this,                             "error " + e.getmessage(), toast.length_long)                             .show();                     system.out.println("error in http connection "                             + e.getclass().getname() + " " + e.tostring());                 }             } catch (exception e) {                 toast.maketext(                         imagegallerydemoactivity.this,                         "error " + e.getclass().getname() + " "                                 + e.getmessage(), toast.length_long).show();                 system.out.println("error in http connection " + e.tostring());             }          }     }      public string convertresponsetostring(httpresponse response)             throws illegalstateexception, ioexception {          string res = "";         stringbuffer buffer = new stringbuffer();         inputstream = response.getentity().getcontent();         int contentlength = (int) response.getentity().getcontentlength(); // getting                                                                             // content                                                                             // length…..         toast.maketext(imagegallerydemoactivity.this,                 "contentlength : " + contentlength, toast.length_long).show();         if (contentlength < 0) {         } else {             byte[] data = new byte[512];             int len = 0;             try {                 while (-1 != (len = inputstream.read(data))) {                     buffer.append(new string(data, 0, len)); // converting                                                                 // string ,                                                                 // appending                                                                 // stringbuffer…..                 }             } catch (ioexception e) {                 e.printstacktrace();             }             try {                 inputstream.close(); // closing stream…..             } catch (ioexception e) {                 e.printstacktrace();             }             res = buffer.tostring(); // converting stringbuffer string…..              toast.maketext(imagegallerydemoactivity.this, "result : " + res,                     toast.length_long).show();             // system.out.println("response => " +             // entityutils.tostring(response.getentity()));         }         return res;      } } 

upload image.php

<?php     $base=$_request['image'];      $binary=base64_decode($base);     header('content-type: bitmap; charset=utf-8');     $file = fopen('uploaded_image.jpg', 'wb');     fwrite($file, $binary);     fclose($file);     echo 'image upload complete!!, please check php file directory……'; ?> 


Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

CSS3 Transition to highlight new elements created in JQuery -