php - How to make image attatchment button -
enter image description herehow make attachment activity... means have create activity in have give edit text box user suggestion , image button adding image if necessary related suggestion.. , send data server...
i have created edit text , send data web server.. have no idea image upload..
my mainactivity.java
public class mainactivity extends activity implements onclicklistener { edittext ed1; button submit; button cancel; button btnselectphoto; string tobesent; string imagepath=null; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); ed1 = (edittext) findviewbyid(r.id.edit1); submit = (button) findviewbyid(r.id.submit); cancel = (button) findviewbyid(r.id.cancel); submit.setonclicklistener(this); cancel.setonclicklistener(this); } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.main, menu); return true; } @override public boolean onoptionsitemselected(menuitem item) { // handle action bar item clicks here. action bar // automatically handle clicks on home/up button, long // specify parent activity in androidmanifest.xml. int id = item.getitemid(); if (id == r.id.action_settings) { return true; } return super.onoptionsitemselected(item); } @override public void onclick(view v) { if (v.getid()==r.id.submit) { // todo auto-generated method stub if (ed1.gettext().tostring().length() < 1) { // out of range toast.maketext(this, "please enter something", toast.length_long) .show(); } else { tobesent = ed1.gettext().tostring(); new myasynctask(mainactivity.this, tobesent,imagepath).execute(); } } else if (v.getid()==r.id.cancel) { toast.maketext(mainactivity.this, "thank comming", toast.length_short).show(); finish(); } } private class myasynctask extends asynctask<void, void, void> { private context context; private string data; private inputstream is; private string response; private string imagepath; public myasynctask(context context, string data,string imagepath) { this.context = context; this.data = data; this.imagepath=imagepath; } @suppresswarnings("deprecation") @override protected void doinbackground(void... params) { try { // add data list<namevaluepair> namevaluepairs = new arraylist<namevaluepair>(); namevaluepairs .add(new basicnamevaluepair("datatobesent", data)); namevaluepairs.add(new basicnamevaluepair("image_path",imagepath)); defaulthttpclient httpclient = new defaulthttpclient(); httppost httppost = new httppost("http://www.example.com/municipal/dummytestfolder/insertquery.php");// //url come here urlencodedformentity ent= new urlencodedformentity(namevaluepairs, http.utf_8); httppost.setentity(ent); httpresponse httpresponse = httpclient.execute(httppost); httpentity httpentity = httpresponse.getentity(); = httpentity.getcontent(); int statuscode=httpresponse.getstatusline().getstatuscode(); log.e("status code<><><>", ""+statuscode); } catch (unsupportedencodingexception e) { // todo auto-generated catch block e.printstacktrace(); } catch (ioexception e) { // todo auto-generated catch block e.printstacktrace(); } try { bufferedreader reader = new bufferedreader( new inputstreamreader(is, "iso-8859-1"), 8); stringbuilder sb = new stringbuilder(); string line = null; while ((line = reader.readline()) != null) { sb.append(line + "\n"); } is.close(); response = sb.tostring(); log.v("login webservice response", "login response == " + response); } catch (exception e) { log.e("buffer error", "error converting result " + e.tostring()); } return null; } @override protected void onpostexecute(void result) { if((response!=null)){ toast.maketext(context, "successfully uploaded comment", toast.length_long).show(); ed1.settext(""); } }
use code image uploading
public class uploadimage extends asynctask<void, void, void> { bufferedreader reader = null; string data = utils.profile_upload_url; string text = ""; string filename = sourcefile.substring(sourcefile.lastindexof("/") + 1); progressdialog pd; int bytesread, bytesavailable, buffersize; byte[] buffer; int maxbuffersize = 1 * 1024 * 1024; @override protected void onpreexecute() { super.onpreexecute(); pd = new progressdialog(profiledetails.this, r.style.mytheme); pd.setcancelable(false); pd.setprogressstyle(android.r.style.widget_progressbar_large); pd.show(); } @override protected void doinbackground(void... params) { try { httpurlconnection conn = null; dataoutputstream dos = null; string lineend = "\r\n"; string twohyphens = "--"; string boundary = "*****"; fileinputstream fileinputstream = new fileinputstream(sourcefile); url url = new url(utils.profile_upload_url); conn = (httpurlconnection) url.openconnection(); conn.setdoinput(true); conn.setdooutput(true); conn.setusecaches(false); 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); 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); bytesavailable = fileinputstream.available(); buffersize = math.min(bytesavailable, maxbuffersize); buffer = new byte[buffersize]; 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); } dos.writebytes(lineend); dos.writebytes(twohyphens + boundary + twohyphens + lineend); outputstreamwriter wr = new outputstreamwriter(conn.getoutputstream()); wr.write(data); wr.flush(); reader = new bufferedreader(new inputstreamreader(conn.getinputstream())); stringbuilder sb = new stringbuilder(); string line = null; while ((line = reader.readline()) != null) { // append server response in string sb.append(line + "\n"); } text = sb.tostring(); log.i("res", text.tostring()); } catch (exception e) { } return null; } @override protected void onpostexecute(void avoid) { super.onpostexecute(avoid); string res = ""; try { jsonobject jobj = new jsonobject(text); res = jobj.getstring("status_txt"); if (res.equalsignorecase("ok")) { string responce = jobj.getjsonobject("data").getstring("thumb_url"); editor.putstring(utils.thumb_url, responce); editor.commit(); log.i("responce123", responce); pd.dismiss(); } else { } } catch (jsonexception jerror) { } } }
Comments
Post a Comment