java - My app force closes after selecting an image from gallery -
i trying make upload function in app, app force closes after selecting image on gallery. code
addcatergory.java
public class addcategory extends appcompatactivity implements view.onclicklistener { private button buttonchoose; private button buttonupload; private imageview imageview; private edittext edittextname; private bitmap bitmap; private int pick_image_request = 1; private string upload_url ="http://knyjayjay.16mb.com/products/upload.php"; private string key_image = "image"; private string key_name = "name"; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_add_category); buttonupload = (button) findviewbyid(r.id.btn_addcat); buttonchoose = (button) findviewbyid(r.id.chooseimgbtn); buttonchoose = (button) findviewbyid(r.id.chooseimgbtn); buttonupload = (button) findviewbyid(r.id.btn_addcat); edittextname = (edittext) findviewbyid(r.id.input_name); imageview = (imageview) findviewbyid(r.id.imageview); buttonchoose.setonclicklistener(this); buttonupload.setonclicklistener(this); } public string getstringimage(bitmap bmp){ bytearrayoutputstream baos = new bytearrayoutputstream(); bmp.compress(bitmap.compressformat.jpeg, 100, baos); byte[] imagebytes = baos.tobytearray(); string encodedimage = base64.encodetostring(imagebytes, base64.default); return encodedimage; } private void uploadimage(){ //showing progress dialog final progressdialog loading = progressdialog.show(this,"uploading","please wait...",false,false); stringrequest stringrequest = new stringrequest(request.method.post, upload_url, new response.listener<string>() { @override public void onresponse(string s) { //disimissing progress dialog loading.dismiss(); //showing toast message of response toast.maketext(addcategory.this, s , toast.length_long).show(); } }, new response.errorlistener() { @override public void onerrorresponse(volleyerror volleyerror) { //dismissing progress dialog loading.dismiss(); //showing toast toast.maketext(addcategory.this, volleyerror.getmessage().tostring(), toast.length_long).show(); } }){ @override protected map<string, string> getparams() throws authfailureerror { //converting bitmap string string image = getstringimage(bitmap); //getting image name string name = edittextname.gettext().tostring(); //creating parameters map<string,string> params = new hashtable<string, string>(); //adding parameters params.put(key_image, image); params.put(key_name, name); //returning parameters return params; } }; //creating request queue requestqueue requestqueue = volley.newrequestqueue(this); //adding request queue requestqueue.add(stringrequest); } private void showfilechooser() { intent intent = new intent(); intent.settype("image/*"); intent.setaction(intent.action_get_content); startactivityforresult(intent.createchooser(intent, "select picture"), pick_image_request); } @override protected void onactivityresult(int requestcode, int resultcode, intent data) { super.onactivityresult(requestcode, resultcode, data); if (requestcode == pick_image_request && resultcode == result_ok && data != null && data.getdata() != null) { uri filepath = data.getdata(); try { //getting bitmap gallery bitmap = mediastore.images.media.getbitmap(getcontentresolver(), filepath); //setting bitmap imageview imageview.setimagebitmap(bitmap); } catch (ioexception e) { e.printstacktrace(); } } } @override public void onclick(view v) { if(v == buttonchoose){ showfilechooser(); } if(v == buttonupload){ uploadimage(); } } }
and error in logcat this
java.lang.runtimeexception: failure delivering result resultinfo{who=null, request=1, result=-1, data=intent { dat=content://media/external/images/media/10608 }} activity {com.jayjaudian.warehousemanagement/com.jayjaudian.warehousemanagement.activity.addcategory}: java.lang.nullpointerexception @ android.app.activitythread.deliverresults(activitythread.java) @ android.app.activitythread.handlesendresult(activitythread.java) @ android.app.activitythread.access$1300(activitythread.java) @ android.app.activitythread$h.handlemessage(activitythread.java) @ android.os.handler.dispatchmessage(handler.java) @ android.os.looper.loop(looper.java) @ android.app.activitythread.main(activitythread.java) @ java.lang.reflect.method.invokenative(native method) @ java.lang.reflect.method.invoke(method.java:515) @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:836) @ com.android.internal.os.zygoteinit.main(zygoteinit.java:652) @ de.robv.android.xposed.xposedbridge.main(xposedbridge.java:132) @ dalvik.system.nativestart.main(native method) caused by: java.lang.nullpointerexception @ com.jayjaudian.warehousemanagement.activity.addcategory.onactivityresult(addcategory.java:148) @ android.app.activity.dispatchactivityresult(activity.java) ... 13 more
i know it's kinda late in case needed
you should use input stream uri , this
inputstream = getcontentresolver().openinputstream(data.getdata());
and then
bitmap = bitmapfactory.decodestream(is);
it works me
Comments
Post a Comment