How to set an image on imageView from the gallery and image taken by camera in Android? -


in app want set image on imageview gallery , want set image taken camera. have tried code, when load image gallery, working. when try take picture set on imageview, activity loaded image not showing in imageview. solution this? here code:

first activity

public class mainactivity extends actionbaractivity {     private static int result_load_image = 1;     private static final int camera_request = 1888;     private imageview imageview;     bitmap photo;     string picturepath;      @override     protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.activity_main);         button buttonloadimage = (button) findviewbyid(r.id.buttonloadpicture);         buttonloadimage.setonclicklistener(new view.onclicklistener() {              @override             public void onclick(view arg0) {                  intent = new intent(                         intent.action_pick,                         android.provider.mediastore.images.media.external_content_uri);                  startactivityforresult(i, result_load_image);             }         });         // this.imageview = (imageview)this.findviewbyid(r.id.imgview);         button photobutton = (button) this.findviewbyid(r.id.button2);         photobutton.setonclicklistener(new view.onclicklistener() {              @override             public void onclick(view v) {                 intent cameraintent = new intent(android.provider.mediastore.action_image_capture);                 startactivityforresult(cameraintent, camera_request);              }         });      }       @override     public boolean oncreateoptionsmenu(menu menu) {         // inflate menu; adds items action bar if present.         getmenuinflater().inflate(r.menu.menu_main, menu);         return true;     }      @override     public boolean onoptionsitemselected(menuitem item) {         // handle action bar item clicks here. action bar         // automatically handle clicks on home/up button, long         // specify parent activity in androidmanifest.xml.         int id = item.getitemid();          //noinspection simplifiableifstatement         if (id == r.id.action_settings) {             return true;         }          return super.onoptionsitemselected(item);     }      @override     protected void onactivityresult(int requestcode, int resultcode, intent data) {         super.onactivityresult(requestcode, resultcode, data);          if (requestcode == result_load_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]);             picturepath = cursor.getstring(columnindex);             cursor.close();             int flagval = 1;           /*  imageview imageview = (imageview) findviewbyid(r.id.imgview);             imageview.setimagebitmap(bitmapfactory.decodefile(picturepath));*/             sendimage(flagval);         }         if (requestcode == camera_request && resultcode == result_ok) {           /* photo = (bitmap) data.getextras().get("data");              //imageview.setimagebitmap(photo);             int flagvalt=2;             try {                 sendimage(flagvalt);             }             catch (exception e){                 toast.maketext(mainactivity.this, "method error", toast.length_short).show();              }*/              try {                 loadcampic();                 toast.maketext(mainactivity.this, "method invoked", toast.length_short).show();             } catch (exception e) {                 toast.maketext(mainactivity.this, "method error", toast.length_short).show();             }         }      }      public void sendimage(int flag) {          if (flag == 1) {             toast.maketext(mainactivity.this, "loc:" + picturepath, toast.length_short).show();             intent myintent1 = new intent(mainactivity.this, imageuploadactivity.class);             myintent1.putextra("key", picturepath);              mainactivity.this.startactivity(myintent1);         }         /*else if(flag==2) {             intent intent = new intent(mainactivity.this, imageuploadactivity.class);             intent.putextra("bitmapimage", photo);         }*/     }      void loadcampic() {         string basedir = environment.getexternalstoragedirectory().getabsolutepath();         string pathname = basedir + "/dcim/camera";         file parentdir = new file(pathname);          file[] files = parentdir.listfiles();         date lastdate = null;         string lastfilename;         boolean isfirstfile = true;         (file file : files) {             if (isfirstfile) {                 lastdate = new date(file.lastmodified());                 isfirstfile = false;             }             if (file.getname().endswith(".jpg") || file.getname().endswith(".jpeg")) {                 date lastmoddate = new date(file.lastmodified());                 if (lastmoddate.after(lastdate)) {                     lastdate = lastmoddate;                     lastfilename = file.getname();                     string pathname2 = pathname + lastfilename;                      // log.e(tag, "method invoked");                       intent intent = new intent(mainactivity.this, imageuploadactivity.class);                     bundle extras = new bundle();                     extras.putstring("file_path", pathname2);                     extras.putstring("file_name", lastfilename);                     intent.putextras(extras);                     mainactivity.this.startactivity(intent);                   }             }         }     } } 

second activity

public class imageuploadactivity extends actionbaractivity {      private button upload;     private bitmap bitmap;     private progressdialog dialog;     string picturepath;     string filename;     imageview imageview;      @override     protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.activity_image_upload);         //image sdcard          intent intent1 = getintent();         picturepath = intent1.getextras().getstring("key");         imageview = (imageview) findviewbyid(r.id.imgview);         imageview.setimagebitmap(bitmapfactory.decodefile(picturepath));         bitmap = (bitmapfactory.decodefile(picturepath));           //camera image            /*  intent intent = getintent();             bitmap = (bitmap) intent.getparcelableextra("bitmapimage");             imageview = (imageview) findviewbyid(r.id.imgview);             imageview.setimagebitmap(bitmap);*/          if (picturepath == null) {             intent intent = getintent();             bundle extras = intent.getextras();             filename = extras.getstring("file_name");             string filepath = extras.getstring("file_path");             toast.maketext(imageuploadactivity.this, "success:" + filepath, toast.length_short).show();             imageview = (imageview) findviewbyid(r.id.imgview);             imageview.setimagebitmap(bitmapfactory.decodefile(filepath));             bitmap = (bitmapfactory.decodefile(filepath));         }         upload = (button) findviewbyid(r.id.upload);          upload.setonclicklistener(new view.onclicklistener() {              public void onclick(view v) {                 if (bitmap == null) {                     toast.maketext(getapplicationcontext(),                             "please select image", toast.length_short).show();                 } else {                     dialog = progressdialog.show(imageuploadactivity.this, "uploading",                             "please wait...", true);                     new imageuploadtask().execute();                 }             }         });       }      @override     public boolean oncreateoptionsmenu(menu menu) {         // inflate menu; adds items action bar if present.         getmenuinflater().inflate(r.menu.menu_image_upload, menu);         return true;     }      @override     public boolean onoptionsitemselected(menuitem item) {         // handle action bar item clicks here. action bar         // automatically handle clicks on home/up button, long         // specify parent activity in androidmanifest.xml.         int id = item.getitemid();          //noinspection simplifiableifstatement         if (id == r.id.action_settings) {             return true;         }          return super.onoptionsitemselected(item);     }      class imageuploadtask extends asynctask<void, void, string> {         @override         protected string doinbackground(void... unsued) {             try {                 httpclient httpclient = new defaulthttpclient();                 httpcontext localcontext = new basichttpcontext();                 httppost httppost = new httppost("http://10.10.10.15/test/upload.php");                  multipartentity entity = new multipartentity(                         httpmultipartmode.browser_compatible);                  bytearrayoutputstream bos = new bytearrayoutputstream();                 bitmap.compress(bitmap.compressformat.jpeg, 100, bos);                 byte[] data = bos.tobytearray();                 /* entity.addpart("uploaded_file", new bytearraybody(data,                         "myimage.jpg"));*/                  // string newfilename= filename.concat("file");                 // newfilename=filename+newfilename;                  entity.addpart("uploaded_file", new bytearraybody(data,                         filename));                 //  log.e(tag, "method invoked");                 httppost.setentity(entity);                 httpresponse response = httpclient.execute(httppost,                         localcontext);                 bufferedreader reader = new bufferedreader(                         new inputstreamreader(                                 response.getentity().getcontent(), "utf-8"));                  stringbuilder builder = new stringbuilder();                 string aux = "";                  while ((aux = reader.readline()) != null) {                     builder.append(aux);                 }                  string sresponse = builder.tostring();                   return sresponse;             } catch (exception e) {                 if (dialog.isshowing())                     dialog.dismiss();                 toast.maketext(getapplicationcontext(), "exception message 1", toast.length_long).show();                 log.e(e.getclass().getname(), e.getmessage(), e);                 return null;             }              // (null);         }          @override         protected void onprogressupdate(void... unsued) {          }          @override         protected void onpostexecute(string sresponse) {             try {                 if (dialog.isshowing())                     dialog.dismiss();                  if (sresponse != null) {                      toast.maketext(getapplicationcontext(),                             "photo uploaded successfully",                             toast.length_short).show();                       log.e("response", sresponse);                 }              } catch (exception e) {                 toast.maketext(getapplicationcontext(),                         "exception message",                         toast.length_long).show();                 log.e(e.getclass().getname(), e.getmessage(), e);             }         }     } } 

take @ selected best answer here take photo w/ camera intent , display in imageview or textview? , find out how take picture , set background of imageview programmatically


Comments

Popular posts from this blog

powershell Start-Process exit code -1073741502 when used with Credential from a windows service environment -

twig - Using Twigbridge in a Laravel 5.1 Package -

c# - LINQ join Entities from HashSet's, Join vs Dictionary vs HashSet performance -