android - How to upload image on server using multipart using Retrofit library -
using retrofit library how can upload image on server? detail scenario have signup module in want send user profile image , other user detail on server using php web api. 1 know come in answers. want know best , fast method upload image on server
after searching bellow code made uploading asset image on server can upload sd card image replacing asset image path sd card path. hope effort solution of question work other people also. in case work fine , complete project purpose image uploading using retrofit. code given bellow.
file= copyreadassets(); okhttp3.requestbody requestbody=okhttp3.requestbody.create(okhttp3.mediatype.parse("multipart/form-data"), file); // multipartbody.part used send actual file name body =multipartbody.part.createformdata("uploadfile", file.getname(), requestbody); btnupload.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { hashmap<string,string> map=new hashmap<string, string>(); map.put("user_id","331"); map.put("uploadfile","uploadfile"); map.put("version_key_android","1.0"); call<string> call= null; try { string requeststring=getwebservicejsobjrequestforvolley("createuser",map); log.d("requeststring==>",requeststring+""); progressbar.setvisibility(view.visible); call = apiservice.upload(requeststring,body); } catch (jsonexception e) { e.printstacktrace(); } call.enqueue(new callback<string>() { @override public void onresponse(call<string> call, response<string> response) { if(progressbar!=null){ progressbar.setvisibility(view.gone); } if(response.issuccessful()){ log.d("status==>",response.body()+""); } } @override public void onfailure(call<string> call, throwable t) { if(progressbar!=null){ progressbar.setvisibility(view.gone); } } }); } }); } private file copyreadassets() { assetmanager assetmanager = getassets(); inputstream in = null; outputstream out = null; file file = new file(getfilesdir(), "temp.jpg"); try { in = assetmanager.open("screen_1.jpg"); out = openfileoutput(file.getname(), context.mode_world_readable); copyfile(in, out); in.close(); in = null; out.flush(); out.close(); out = null; } catch (exception e) { log.e("tag", e.getmessage()); } log.d("file==>",file.tostring()+""); return file; } private void copyfile(inputstream in, outputstream out) throws ioexception { byte[] buffer = new byte[1024]; int read; while((read = in.read(buffer)) != -1){ out.write(buffer, 0, read); } } public string getwebservicejsobjrequestforvolley(string mmethodname, hashmap<string, string> mmap) throws jsonexception { string retstr; //string strparams = null; // map.put("device_id", prefs.getdevicetokenpref(baseactivity.this)); jsonobject json = new jsonobject(); if (mmap.size() > 0) { (map.entry<string, string> e : mmap.entryset()) { string key = e.getkey(); string value = e.getvalue(); json.put(key, value); } } jsonobject fjson = new jsonobject(); fjson.put("method_name", mmethodname); fjson.put("body", json); retstr = fjson.tostring(); return retstr; }
Comments
Post a Comment