Android resize image from gallery before upload via php -


i've application uploading image choosen gallery. before upload show selected image in imageview , let user choose image or upload current. downscale image if widht or height bigger 1000 i'm able scale bitmap in imageview. me or give me suggestion? here code, appreciated, in advance.

    protected void onactivityresult(int requestcode, int resultcode, intent data) {      if (requestcode == 1 && resultcode == result_ok) {         //bitmap photo = (bitmap) data.getdata().getpath();         //uri imagename=data.getdata();         uri selectedimageuri = data.getdata();         imagepath = getpath(selectedimageuri);         bitmap bitmap=bitmapfactory.decodefile(imagepath);         int width = bitmap.getwidth();         int height = bitmap.getheight();          if(width>=1000 || height >= 1000){             width = width/3;             height = height/3;             bitmap = bitmap.createscaledbitmap(bitmap, width, height, false);             }         imageview.setimagebitmap(bitmap);         uploadbutton.setvisibility(view.visible);      } } 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) {      int day, month, year;     int second, minute, hour;     gregoriancalendar date = new gregoriancalendar();      day = date.get(calendar.day_of_month);     month = date.get(calendar.month);     year = date.get(calendar.year);      second = date.get(calendar.second);     minute = date.get(calendar.minute);     hour = date.get(calendar.hour);      string name=(hour+""+minute+""+second+""+day+""+(month+1)+""+year);     string tag=name+".jpg";     string filename = sourcefileuri.replace(sourcefileuri,tag);      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);              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);              serverresponsecode = conn.getresponsecode();             string serverresponsemessage = conn.getresponsemessage();              log.i("uploadfile", "http response : "                     + serverresponsemessage + ": " + serverresponsecode);              if(serverresponsecode == 200){                  runonuithread(new runnable() {                     public void run() {                          toast.maketext(uploadimage.this, getstring(r.string.uploadone), toast.length_short).show();                     }                 });                 intent intent = new intent(uploadimage.this, profileactivity.class);                 intent.addflags(intent.flag_activity_clear_top);                 startactivity(intent);                 finish();             }              //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(uploadimage.this, getstring(r.string.errorupload), toast.length_long).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(uploadimage.this, getstring(r.string.errorupload), toast.length_short).show();                 }             });             log.e("upload file server", "exception : "  + e.getmessage(), e);         }         dialog.dismiss();         return serverresponsecode;      } 

you can decode image bitmap using 1 of static methods bitmapfactory class, whether using existing fileinputstream opening or passing in file path.

following can use createscaledbitmap method resize down, , use compress put outputstream.


Comments

Popular posts from this blog

python - No exponential form of the z-axis in matplotlib-3D-plots -

php - Best Light server (Linux + Web server + Database) for Raspberry Pi -

c# - "Newtonsoft.Json.JsonSerializationException unable to find constructor to use for types" error when deserializing class -