php - The app forced to close when trying to upload picture to server -


i have copied example website , dont know why not working.

my intended flow is: picking picture gallery, showing inside imageview , uploading server.

the first 2 step -picking picture , showing it- working fine when click on upload button app forcing close i.e runtime error. think there may problem php file, way use not sure.

you may ask more info if required solve problem.

package com.example.imagepickanduplaod;  public class mainactivity extends activity {      private imageview image;     private button uploadbutton;     private bitmap bitmap;     private button selectimagebutton;      // number of images select     private static final int pick_image = 1;      /**      * called when activity first created      */     @override     protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.activity_main);          // find views         image = (imageview) findviewbyid(r.id.uploadimage);         uploadbutton = (button) findviewbyid(r.id.uploadbutton);          // on click select image         selectimagebutton = (button) findviewbyid(r.id.selectimagebutton);         selectimagebutton.setonclicklistener(new onclicklistener() {              @override             public void onclick(view v) {                 selectimagefromgallery();              }         });          // when uploadbutton clicked         uploadbutton.setonclicklistener(new onclicklistener() {              @override             public void onclick(view v) {                 new imageuploadtask().execute();             }         });     }      /**      * opens dialog picker, user can select image gallery.      * result returned in method <code>onactivityresult()</code>      */     public void selectimagefromgallery() {         intent intent = new intent();         intent.settype("image/*");         intent.setaction(intent.action_get_content);         startactivityforresult(intent.createchooser(intent, "select picture"),                 pick_image);     }      /**      * retrives result returned selecting image, invoking method      * <code>selectimagefromgallery()</code>      */     @override     protected void onactivityresult(int requestcode, int resultcode, intent data) {         super.onactivityresult(requestcode, resultcode, data);          if (requestcode == pick_image && resultcode == result_ok                 && null != data) {             uri selectedimage = data.getdata();             string[] filepathcolumn = { mediastore.images.media.data };              cursor cursor = getcontentresolver().query(selectedimage,                     filepathcolumn, null, null, null);             cursor.movetofirst();              int columnindex = cursor.getcolumnindex(filepathcolumn[0]);             string picturepath = cursor.getstring(columnindex);             cursor.close();              decodefile(picturepath);          }     }      /**      * method decodes image file avoid out of memory issues. sets      * selected image in imageview.      *       * @param filepath      */     public void decodefile(string filepath) {         // decode image size         bitmapfactory.options o = new bitmapfactory.options();         o.injustdecodebounds = true;         bitmapfactory.decodefile(filepath, o);          // new size want scale         final int required_size = 1024;          // find correct scale value. should power of 2.         int width_tmp = o.outwidth, height_tmp = o.outheight;         int scale = 1;         while (true) {             if (width_tmp < required_size && height_tmp < required_size)                 break;             width_tmp /= 2;             height_tmp /= 2;             scale *= 2;         }          // decode insamplesize         bitmapfactory.options o2 = new bitmapfactory.options();         o2.insamplesize = scale;         bitmap = bitmapfactory.decodefile(filepath, o2);          image.setimagebitmap(bitmap);     }      /**      * class connects server , uploads photo      *       *       */     class imageuploadtask extends asynctask<void, void, string> {         private string webaddresstopost = "http://menaria.zz.mu/picupload.php";          // private progressdialog dialog;         private progressdialog dialog = new progressdialog(mainactivity.this);          @override         protected void onpreexecute() {             dialog.setmessage("uploading...");             dialog.show();         }          @override         protected string doinbackground(void... params) {             try {                 httpclient httpclient = new defaulthttpclient();                 httpcontext localcontext = new basichttpcontext();                 httppost httppost = new httppost(webaddresstopost);                  multipartentity entity = new multipartentity(                         httpmultipartmode.browser_compatible);                  bytearrayoutputstream bos = new bytearrayoutputstream();                 bitmap.compress(compressformat.jpeg, 100, bos);                 byte[] data = bos.tobytearray();                 string file = base64.encodebytes(data);                 entity.addpart("uploaded", new stringbody(file));                  entity.addpart("someotherstringtosend", new stringbody(                         "your string here"));                  httppost.setentity(entity);                 httpresponse response = httpclient.execute(httppost,                         localcontext);                 bufferedreader reader = new bufferedreader(                         new inputstreamreader(                                 response.getentity().getcontent(), "utf-8"));                  string sresponse = reader.readline();                 return sresponse;             } catch (exception e) {                 // went wrong. connection server error             }             return null;         }          @override         protected void onpostexecute(string result) {             dialog.dismiss();             toast.maketext(getapplicationcontext(), "file uploaded",                     toast.length_long).show();         }      }      } 

and here stacktrace

07-11 16:01:17.791: i/timeline(27694): timeline: activity_idle id: android.os.binderproxy@436a8d70 time:57708003 07-11 16:01:21.111: e/dalvikvm(27694): not find class 'org.apache.http.entity.mime.multipartentity', referenced method com.example.imagepickanduplaod.mainactivity$imageuploadtask.doinbackground 07-11 16:01:21.111: w/dalvikvm(27694): vfy: unable resolve new-instance 1607 (lorg/apache/http/entity/mime/multipartentity;) in lcom/example/imagepickanduplaod/mainactivity$imageuploadtask; 07-11 16:01:21.111: d/dalvikvm(27694): vfy: replacing opcode 0x22 @ 0x0011 07-11 16:01:21.111: i/dalvikvm(27694): dexopt: unable optimize static field ref 0x14d2 @ 0x13 in lcom/example/imagepickanduplaod/mainactivity$imageuploadtask;.doinbackground 07-11 16:01:21.111: d/dalvikvm(27694): dexopt: unable opt direct call 0x3067 @ 0x15 in lcom/example/imagepickanduplaod/mainactivity$imageuploadtask;.doinbackground 07-11 16:01:21.111: d/dalvikvm(27694): dexopt: unable opt direct call 0x3069 @ 0x36 in lcom/example/imagepickanduplaod/mainactivity$imageuploadtask;.doinbackground 07-11 16:01:21.111: d/dalvikvm(27694): dexopt: unable opt direct call 0x3069 @ 0x42 in lcom/example/imagepickanduplaod/mainactivity$imageuploadtask;.doinbackground 07-11 16:01:21.151: w/dalvikvm(27694): threadid=12: thread exiting uncaught exception (group=0x41f36d58) 07-11 16:01:21.151: e/androidruntime(27694): fatal exception: asynctask #2 07-11 16:01:21.151: e/androidruntime(27694): process: com.example.imagepickanduplaod, pid: 27694 07-11 16:01:21.151: e/androidruntime(27694): java.lang.runtimeexception: error occured while executing doinbackground() 07-11 16:01:21.151: e/androidruntime(27694):    @ android.os.asynctask$3.done(asynctask.java:300) 07-11 16:01:21.151: e/androidruntime(27694):    @ java.util.concurrent.futuretask.finishcompletion(futuretask.java:355) 07-11 16:01:21.151: e/androidruntime(27694):    @ java.util.concurrent.futuretask.setexception(futuretask.java:222) 07-11 16:01:21.151: e/androidruntime(27694):    @ java.util.concurrent.futuretask.run(futuretask.java:242) 07-11 16:01:21.151: e/androidruntime(27694):    @ android.os.asynctask$serialexecutor$1.run(asynctask.java:231) 07-11 16:01:21.151: e/androidruntime(27694):    @ java.util.concurrent.threadpoolexecutor.runworker(threadpoolexecutor.java:1112) 07-11 16:01:21.151: e/androidruntime(27694):    @ java.util.concurrent.threadpoolexecutor$worker.run(threadpoolexecutor.java:587) 07-11 16:01:21.151: e/androidruntime(27694):    @ java.lang.thread.run(thread.java:841) 07-11 16:01:21.151: e/androidruntime(27694): caused by: java.lang.noclassdeffounderror: org.apache.http.entity.mime.multipartentity 07-11 16:01:21.151: e/androidruntime(27694):    @ com.example.imagepickanduplaod.mainactivity$imageuploadtask.doinbackground(mainactivity.java:173) 07-11 16:01:21.151: e/androidruntime(27694):    @ com.example.imagepickanduplaod.mainactivity$imageuploadtask.doinbackground(mainactivity.java:1) 07-11 16:01:21.151: e/androidruntime(27694):    @ android.os.asynctask$2.call(asynctask.java:288) 07-11 16:01:21.151: e/androidruntime(27694):    @ java.util.concurrent.futuretask.run(futuretask.java:237) 07-11 16:01:21.151: e/androidruntime(27694):    ... 4 more 07-11 16:01:21.241: i/process(27694): sending signal. pid: 27694 sig: 9 

here php code

public function savemobileattachment($imagefile,$userid,$filename,$filedescription){ $newrow = $this->createrow(); $buffer = base64_decode($imagefile); $date = new zend_date(); $currentdate = $date->get(); $newrow->filepath = "attachments/".$currentdate.".jpg"; $file = fopen("attachments/".$currentdate.".jpg", "wb"); fwrite($file, $buffer);   fclose($file); $newrow->filedescription = $filedescription; $newrow->filename = $filename; $useratch = new userattachments(); $useratchrow = $useratch->createrow(); $useratchrow->imageid = $newrow->save(); $useratchrow->userid = $userid; $useratchrow->save(); } 

noclassdeffounderror means can't find jar contains class.

since aren't having problems compiling code, must mean jar isn't being included in apk.

the solution depends on how building app.

if using android studio, go file->project structure->app->dependencies , make sure apache http library jar listed.

if using eclipse, go project->properties->java build path->libraries->android dependencies , make sure jar listed. go order , export tab , make sure "android dependencies" checked.


Comments

Popular posts from this blog

android - Gradle sync Error:Configuration with name 'default' not found -

java - Andrioid studio start fail: Fatal error initializing 'null' -

html - jQuery UI Sortable - Remove placeholder after item is dropped -