android - How to send image to server using json -
anybody please check code tell me whats error in please. m uploading image server first upload show me memory out of error , after upload small image don't know gone please me.
here code
uploadbutton = (button)findviewbyid(r.id.uploadbutton); btnselectpic = (button)findviewbyid(r.id.button_selectpic); messagetext = (textview)findviewbyid(r.id.messagetext); imageview = (imageview)findviewbyid(r.id.imageview_pic); btnselectpic.setonclicklistener(this); uploadbutton.setonclicklistener(this); uploadserveruri = "http://10.0.2.2/chatapp/upload_image_android/upload_image.php"; imageview img= new imageview(this); } @override public void onclick(view arg0) { if(arg0==btnselectpic) { intent intent = new intent(); intent.settype("image/*"); intent.setaction(intent.action_get_content); startactivityforresult(intent.createchooser(intent, "complete actionusing"),1); } elseif (arg0==uploadbutton) { dialog = progressdialog.show(mainactivity.this, "", "uploading file...", true); messagetext.settext("uploading started....."); new thread(new runnable() { public void run() { uploadfile(imagepath); } }).start(); } } @override protected void onactivityresult(int requestcode, int resultcode, intent data) { if (requestcode == 1 && resultcode == result_ok) { //bitmap photo = (bitmap) data.getdata().getpath(); uri selectedimageuri = data.getdata(); imagepath = getpath(selectedimageuri); bitmap bitmap=bitmapfactory.decodefile(imagepath); imageview.setimagebitmap(bitmap); messagetext.settext("uploading file path:" +imagepath); } } public string getpath(uri uri) { string[] projection = { mediastore.images.media.data }; cursor cursor = managedquery(uri, projection, null, null, null); int column_index = cursor.getcolumnindexorthrow(mediastore.images.media.data); cursor.movetofirst(); return cursor.getstring(column_index); } public int uploadfile(string sourcefileuri) { string filename = sourcefileuri; httpurlconnection conn = null; dataoutputstream dos = null; string lineend = "\r\n"; string twohyphens = "--"; string boundary = "*****"; int bytesread, bytesavailable, buffersize; byte[] buffer; int maxbuffersize = 1 * 1024 * 1024; file sourcefile = new file(sourcefileuri); if (!sourcefile.isfile()) { dialog.dismiss(); log.e("uploadfile", "source file not exist :"+imagepath); runonuithread(new runnable() { public void run() { messagetext.settext("source file not exist :"+ imagepath); } }); return 0; } else { try { // open url connection servlet fileinputstream fileinputstream = new fileinputstream(sourcefile); url url = new url(uploadserveruri); // open http connection url conn = (httpurlconnection) url.openconnection(); conn.setdoinput(true); // allow inputs conn.setdooutput(true); // allow outputs conn.setusecaches(false); // don't use cached copy conn.setrequestmethod("post"); conn.setrequestproperty("connection", "keep-alive"); conn.setrequestproperty("enctype", "multipart/form-data"); conn.setrequestproperty("content-type", "multipart/form-data;boundary=" + boundary); conn.setrequestproperty("uploaded_file", filename); conn.setrequestproperty("submittype", "postad"); dos = new dataoutputstream(conn.getoutputstream()); dos.writebytes(twohyphens + boundary + lineend); dos.writebytes("content-disposition: form-data; name=\"uploaded_file\";filename=\"" + filename + "\"" + lineend); dos.writebytes(lineend); // create buffer of maximum size bytesavailable = fileinputstream.available(); buffersize = math.min(bytesavailable, maxbuffersize); buffer = new byte[buffersize]; // read file , write form... bytesread = fileinputstream.read(buffer, 0, buffersize); while (bytesread > 0) { dos.write(buffer, 0, buffersize); bytesavailable = fileinputstream.available(); buffersize = math.min(bytesavailable, maxbuffersize); bytesread = fileinputstream.read(buffer, 0, buffersize); } // send multipart form data necesssary after file data... dos.writebytes(lineend); dos.writebytes(twohyphens + boundary + twohyphens + lineend); // responses server (code , message) serverresponsecode = conn.getresponsecode(); string serverresponsemessage = conn.getresponsemessage(); log.i("uploadfile", "http response : " + serverresponsemessage + ": " + serverresponsecode); if(serverresponsecode == 200){ runonuithread(new runnable() { public void run() { string msg = "file upload completed.\n\n see uploaded file here : \n\n" ; messagetext.settext(msg); toast.maketext(mainactivity.this, "file upload complete.", toast.length_short).show(); } }); } //close streams // fileinputstream.close(); dos.flush(); dos.close(); } catch (malformedurlexception ex) { dialog.dismiss(); ex.printstacktrace(); runonuithread(new runnable() { public void run() { messagetext.settext("malformedurlexception exception : check script url."); toast.maketext(mainactivity.this, "malformedurlexception", toast.length_short).show(); } }); log.e("upload file server", "error: " + ex.getmessage(), ex); } catch (exception e) { dialog.dismiss(); e.printstacktrace(); runonuithread(new runnable() { public void run() { messagetext.settext("got exception : see logcat "); toast.maketext(mainactivity.this, "got exception : see logcat ", toast.length_short).show(); } }); log.e("upload file server exception", "exception : " + e.getmessage(), e); } dialog.dismiss(); return serverresponsecode; } // end else block }
bitmap bitmap=bitmapfactory.decodefile(imagepath);
this has 2 parameters, 1 imagepath , other 1 object of bitmapfactory.options. try out adding following lines.
bitmapfactory.options bmpfactoryoptions = new bitmapfactory.options(); bmpfactoryoptions.injustdecodebounds = true; bitmap bitmap = bitmapfactory.decodefile(imagepath, bmpfactoryoptions);
Comments
Post a Comment