java - Android upload - phone lagging -


hello i'm using class upload image. problem phone stops untill file uploaded. example image large 1.5mb , phone 'freezes' 10~20 seconds depends of internet speed. it's not slowest mobile, guess problem in script.

package com.project;  import java.io.datainputstream; import java.io.dataoutputstream; import java.io.file; import java.io.fileinputstream; import java.net.httpurlconnection; import java.net.url;  import android.app.activity; import android.content.sharedpreferences; import android.os.bundle; import android.util.log;  public class upload extends activity {  /** called when activity first created. */ @override public void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     setcontentview(r.layout.post);   }      } class uploadclass {      string pathtoourfile;      string getid; boolean uploadfile() {      httpurlconnection connection = null;      dataoutputstream outputstream = null;     datainputstream inputstream = null;      string urlserver = "http://path.to-my.site/upload.php?byid="+getid;     string lineend = "\r\n";     string twohyphens = "--";     string boundary =  "*****";      int bytesread, bytesavailable, buffersize;     byte[] buffer;     int maxbuffersize = 1*1024*1024;      try     {     fileinputstream fileinputstream = new fileinputstream(new file(pathtoourfile) );      url url = new url(urlserver);     connection = (httpurlconnection) url.openconnection();      // allow inputs & outputs     connection.setdoinput(true);     connection.setdooutput(true);     connection.setusecaches(false);      // enable post method     connection.setrequestmethod("post");      connection.setrequestproperty("connection", "keep-alive");     connection.setrequestproperty("content-type", "multipart/form-data;boundary="+boundary);       outputstream = new dataoutputstream( connection.getoutputstream() );     outputstream.writebytes(twohyphens + boundary + lineend);     outputstream.writebytes("content-disposition: form-data; name=\"uploadedfile\";filename=\"" + pathtoourfile +"\"" + lineend);     outputstream.writebytes(lineend);      bytesavailable = fileinputstream.available();     buffersize = math.min(bytesavailable, maxbuffersize);     buffer = new byte[buffersize];      // read file     bytesread = fileinputstream.read(buffer, 0, buffersize);      while (bytesread > 0)     {     outputstream.write(buffer, 0, buffersize);     bytesavailable = fileinputstream.available();     buffersize = math.min(bytesavailable, maxbuffersize);     bytesread = fileinputstream.read(buffer, 0, buffersize);     }      outputstream.writebytes(lineend);     outputstream.writebytes(twohyphens + boundary + twohyphens + lineend);      // responses server (code , message)     string serverresponsecode = integer.tostring(connection.getresponsecode());     string serverresponsemessage = connection.getresponsemessage();      fileinputstream.close();     outputstream.flush();     outputstream.close();      }     catch (exception ex)     {         return false;     }     return true;     } } 

all operations run on ui thread unless this. option client-server communication in separate thread. refer tutorial


Comments

Popular posts from this blog

c# - how to write client side events functions for the combobox items -

exception - Python, pyPdf OCR error: pyPdf.utils.PdfReadError: EOF marker not found -