java - Android Upload Image Progress Bar -
i trying find way show uploading file's progress in progress bar , percentage. need add progress bar , transfer percentage of file.
here's code. it's transferring file has no progress bar.
package com.example.christian.progressbar5; import android.content.intent; import android.graphics.bitmap; import android.net.uri; import android.os.bundle; import android.provider.mediastore; import android.support.v7.app.appcompatactivity; import android.util.base64; import android.view.view; import android.widget.button; import android.widget.edittext; import android.widget.imageview; import android.widget.progressbar; import android.widget.toast; import com.android.volley.authfailureerror; import com.android.volley.request; import com.android.volley.requestqueue; import com.android.volley.response; import com.android.volley.volleyerror; import com.android.volley.toolbox.stringrequest; import com.android.volley.toolbox.volley; import java.io.bytearrayoutputstream; import java.io.ioexception; import java.util.hashmap; import java.util.map; public class mainactivity extends appcompatactivity { imageview ivimage; button btnupload; progressbar progressbar; edittext etfilename; final int gallery_request = 38473; bitmap bitmap; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); etfilename = (edittext) findviewbyid(r.id.etfilename); ivimage = (imageview) findviewbyid(r.id.ivimage); btnupload = (button) findviewbyid(r.id.btnupload); progressbar = (progressbar) findviewbyid(r.id.progressbar); ivimage.setonclicklistener(new view.onclicklistener() { @override public void onclick(view view) { intent intent = new intent(intent.action_pick, mediastore.images.media.external_content_uri); startactivityforresult(intent, gallery_request); } }); btnupload.setonclicklistener(new view.onclicklistener() { @override public void onclick(view view) { uploadimage(); } }); } @override protected void onactivityresult(int requestcode, int resultcode, intent data) { if(resultcode == result_ok) { if(requestcode == gallery_request){ uri uri = data.getdata(); try { bitmap = mediastore.images.media.getbitmap(getcontentresolver(), uri); ivimage.setimagebitmap(bitmap); } catch (ioexception e) { e.printstacktrace(); } } } } private void uploadimage() { final string filename = etfilename.gettext().tostring(); requestqueue queue = volley.newrequestqueue(mainactivity.this); string url = "http://inventsystem.esy.es/uploadimage2.php"; bitmap resized = getresizedbitmap(bitmap, 500); final string photo = imagetostring(resized); if(bitmap == null || bitmap.equals("")){ toast.maketext(this, "no image", toast.length_short).show(); return; } if(filename.equals("")){ toast.maketext(this, "what's filename?", toast.length_short).show(); return; } stringrequest stringrequest = new stringrequest(request.method.post, url, new response.listener<string>() { @override public void onresponse(string response) { toast.maketext(mainactivity.this, "image has been uploaded", toast.length_short).show(); } }, new response.errorlistener() { @override public void onerrorresponse(volleyerror error) { toast.maketext(mainactivity.this, "something went wrong...", toast.length_short).show(); } }){ @override protected map<string, string> getparams() throws authfailureerror { map<string, string> params = new hashmap<>(); params.put("image", photo); params.put("name", filename); return params; } }; queue.add(stringrequest); } private string imagetostring(bitmap bitmap) { bytearrayoutputstream bytearray = new bytearrayoutputstream(); bitmap.compress(bitmap.compressformat.jpeg,100,bytearray); byte[] imgbytes = bytearray.tobytearray(); return base64.encodetostring(imgbytes,base64.default); } public bitmap getresizedbitmap(bitmap image, int maxsize) { int width = image.getwidth(); int height = image.getheight(); float bitmapratio = (float)width / (float) height; if (bitmapratio > 1) { width = maxsize; height = (int) (width / bitmapratio); } else { height = maxsize; width = (int) (height * bitmapratio); } return bitmap.createscaledbitmap(image, width, height, true); } }
you can use volleyplus instead of volley add feature.
jsonrequest.setonprogresslistener(new response.progresslistener() { @override public void onprogress(long transferredbytes, long totalsize) { int percentage = (int) ((transferredbytes / ((float) totalsize)) * 100); } });
there's nice code snipplet how here: https://github.com/dworks/volleyplus/issues/53
Comments
Post a Comment