java - android upload file opened via by storage api presented in kitkat -
used below code open file
intent intent = new intent(intent.action_open_document); intent.addcategory(intent.category_openable); intent.settype("image/*"); startactivityforresult(intent, read_request_code);
used below code read file
parcelfiledescriptor parcelfiledescriptor =attachimageonpost.this.getcontentresolver() .openfiledescriptor(data.getdata(), "r"); filedescriptor filedescriptor = parcelfiledescriptor.getfiledescriptor(); rect rect=new rect(100, 100, 100, 100); image.setimagebitmap(bitmapfactory.decodefiledescriptor(filedescriptor, rect));
is there way can upload file using httpclient
import java.io.file; import java.io.ioexception; import org.apache.http.client.clientprotocolexception; import org.apache.http.client.httpclient; import org.apache.http.client.methods.httppost; import org.apache.http.entity.mime.multipartentity; import org.apache.http.entity.mime.content.contentbody; import org.apache.http.entity.mime.content.filebody; import org.apache.http.impl.client.defaulthttpclient; import org.apache.http.params.coreprotocolpnames; import org.apache.http.util.entityutils; import org.apache.http.httpentity; import org.apache.http.httpresponse; import org.apache.http.httpversion; public class sendimagehttppost { public static void sendpost(string url, string imagepath) throws ioexception, clientprotocolexception { httpclient httpclient = new defaulthttpclient(); httpclient.getparams().setparameter(coreprotocolpnames.protocol_version, httpversion.http_1_1); httppost httppost = new httppost(url); file file = new file(imagepath); multipartentity mpentity = new multipartentity(); contentbody cbfile = new filebody(file, "image/jpeg"); mpentity.addpart("userfile", cbfile); httppost.setentity(mpentity); log.e("executing request " + httppost.getrequestline()); httpresponse response = httpclient.execute(httppost); httpentity resentity = response.getentity(); log.e(""+response.getstatusline()); if (resentity != null) { log.e(entityutils.tostring(resentity)); } if (resentity != null) { resentity.consumecontent(); } httpclient.getconnectionmanager().shutdown(); } }
however highly recommend use open source library ,to make task easier , neat , e.g ion :
ion.with(getcontext()) .load("https://koush.clockworkmod.com/test/echo") .uploadprogressbar(uploadprogressbar) .setmultipartparameter("goop", "noop") .setmultipartfile("archive", "application/zip", new file("/sdcard/filename.zip")) .asjsonobject() .setcallback(...)
or volley :
Comments
Post a Comment