Upload image path to server android json -
i have tried lot of examples , watched many videos , done stackoverflow, not getting want.
1] want upload image path server . 2] php query required image of type file, cant send string. need upload image file path. know how send string , decode image.but have no idea how send image file path , image.
plz me this,i in big trouble.
things have tried :
private static final int pick_from_camera = 1; private static final int pick_from_file = 2; private string imgpath = null; private uri mimagecaptureuri; private string encodedimage; @override public void onclick(view v) { // onregisterbuttonclick(); imgpath = null; intent intent = new intent(); intent.settype("image/*"); intent.setaction(intent.action_pick); startactivityforresult( intent.createchooser(intent, "complete action using"), pick_from_file); } public void oncreatecontextmenu(contextmenu menu, view v, contextmenuinfo menuinfo) { super.oncreatecontextmenu(menu, v, menuinfo); menu.setheadertitle("select action"); menu.add(0, v.getid(), 0, "from camera"); menu.add(0, v.getid(), 0, "from gallery"); } protected void onactivityresult(int requestcode, int resultcode, intent data) { if (resultcode != result_ok) return; if (requestcode == pick_from_file) { mimagecaptureuri = data.getdata(); imgpath = getrealpathfromuri(mimagecaptureuri); // gallery log.d("image path", imgpath.tostring()); bitmapfactory.options options = new bitmapfactory.options(); options.insamplesize = 5; bitmap b = bitmapfactory.decodefile(imgpath, options); if (imgpath == null) { imgpath = mimagecaptureuri.getpath(); // file manager } if (imgpath != null) { // ivprofile.setimagebitmap(b); // rotate(ivprofile, 270); exifinterface ei = null; try { ei = new exifinterface(imgpath); } catch (ioexception e1) { // todo auto-generated catch block e1.printstacktrace(); } int orientation = ei.getattributeint( exifinterface.tag_orientation, exifinterface.orientation_normal); switch (orientation) { case exifinterface.orientation_rotate_180: rotate(ivprofile, 180); ivprofile.setimagebitmap(rotatebitmap(b, 180)); break; case exifinterface.orientation_rotate_270: rotate(ivprofile, 270); ivprofile.setimagebitmap(rotatebitmap(b, 270)); break; } try { getbase64string(b); } catch (exception e) { e.printstacktrace(); /* utils.showalert(this, "please select local gallery pics.", false);*/ } } } else if (requestcode == pick_from_camera) { if (imgpath == null) { imgpath = mimagecaptureuri.getpath(); // file manager } bitmapfactory.options options = new bitmapfactory.options(); options.insamplesize = 5; bitmap b = bitmapfactory.decodefile(imgpath, options); if (imgpath != null) { ivprofile.setimagebitmap(b); // rotate(ivprofile, 270); try { getbase64string(b); } catch (exception e) { // utils.showalert(mcontext, "out of memory", false); e.printstacktrace(); } } } } private bitmap rotatebitmap(bitmap b, int orientation) { // todo auto-generated method stub matrix matrix = new matrix(); switch (orientation) { case exifinterface.orientation_normal: return b; case exifinterface.orientation_flip_horizontal: matrix.setscale(-1, 1); break; case exifinterface.orientation_rotate_180: matrix.setrotate(180); break; case exifinterface.orientation_flip_vertical: matrix.setrotate(180); matrix.postscale(-1, 1); break; case exifinterface.orientation_transpose: matrix.setrotate(90); matrix.postscale(-1, 1); break; case exifinterface.orientation_rotate_90: matrix.setrotate(90); break; case exifinterface.orientation_transverse: matrix.setrotate(-90); matrix.postscale(-1, 1); break; case exifinterface.orientation_rotate_270: matrix.setrotate(-90); break; default: return b; } try { bitmap bmrotated = bitmap.createbitmap(b, 0, 0, b.getwidth(), b.getheight(), matrix, true); b.recycle(); return bmrotated; } catch (outofmemoryerror e) { e.printstacktrace(); return null; } } @suppresswarnings("deprecation") public string getrealpathfromuri(uri contenturi) { string[] proj = { mediastore.images.media.data }; cursor cursor = managedquery(contenturi, proj, null, null, null); if (cursor == null) return null; int column_index = cursor .getcolumnindexorthrow(mediastore.images.media.data); cursor.movetofirst(); return cursor.getstring(column_index); } public boolean oncontextitemselected(menuitem item) { if (item.gettitle() == "from camera") { imgpath = null; intent intent = new intent(mediastore.action_image_capture); file file = new file(environment.getexternalstoragedirectory(), "tmp_avatar_" + string.valueof(system.currenttimemillis()) + ".jpg"); mimagecaptureuri = uri.fromfile(file); try { intent.putextra(android.provider.mediastore.extra_output, mimagecaptureuri); this.setrequestedorientation(activityinfo.screen_orientation_portrait); intent.putextra("return-data", true); startactivityforresult(intent, pick_from_camera); } catch (exception e) { e.printstacktrace(); } } else if (item.gettitle() == "from gallery") { imgpath = null; intent intent = new intent(); intent.settype("image/*"); intent.setaction(intent.action_pick); startactivityforresult( intent.createchooser(intent, "complete action using"), pick_from_file); } else { return false; } return true; } public string getbase64string(bitmap path) throws exception { bytearrayoutputstream baos = new bytearrayoutputstream(); path.compress(bitmap.compressformat.jpeg, 50, baos); byte[] bitearray = baos.tobytearray(); encodedimage = base64.encodetostring(bitearray, base64.default); return encodedimage; } private void rotate(view iv, float degree) { final rotateanimation rotateanim = new rotateanimation(0.0f, degree, rotateanimation.relative_to_self, 0.5f, rotateanimation.relative_to_self, 0.5f); rotateanim.setduration(0); rotateanim.setfillafter(true); iv.startanimation(rotateanim); }
and have set encodedimage parameters , send server,but not working
you can use http mime library(preferable 4.3.3) , http post upload file server.
php server recognize image file required in case.
have created class helps me upload image or file server
public class fileuploadhelper extends asynctask<void, void, void> { private multipartentitybuilder multipartentity; private string url; public fileuploadhelper(string url) { multipartentity = multipartentitybuilder.create(); this.url = url; } @suppresslint("trulyrandom") @override protected void doinbackground(void... arg0) { try { multipartentity.addtextbody("<your string key>", "<string value>"); multipartentity.setmode(httpmultipartmode.browser_compatible); httpclient httpclient; httpclient = new defaulthttpclient(); httpclient.getconnectionmanager().closeexpiredconnections(); httppost httppost = new httppost(url); httppost.setentity(multipartentity.build()); httpresponse response = httpclient.execute(httppost); int responsecode = response.getstatusline().getstatuscode(); string serverresponse = entityutils.tostring(response.getentity()); } catch (exception e) { e.printstacktrace(); } return null; } public void addfile(string key, file newfile) throws filenotfoundexception { if (newfile.exists()) { multipartentity.addbinarybody(key, newfile); } else { throw new filenotfoundexception("no file found @ path " + newfile.getpath()); } } }
to use class create object of fileuploader class, call addfile() function , call execute() class extends asynctask. in code have file object
file file = new file(environment.getexternalstoragedirectory(), "tmp_avatar_" + string.valueof(system.currenttimemillis()) + ".jpg");
just pass object addfile().
hope you
Comments
Post a Comment