android - Picture coming from camera or gallery? -


i have intent chooser allows me pick image gallery or camera this:

    intent galleryintent = new intent(intent.action_get_content,null);     galleryintent.settype("image/*");     galleryintent.addcategory(intent.category_openable);      intent cameraintent = new intent(android.provider.mediastore.action_image_capture);        intent chooser = new intent(intent.action_chooser);     chooser.putextra(intent.extra_intent, galleryintent);           chooser.putextra(intent.extra_title, "title");      intent[] intentarray =  {cameraintent};      chooser.putextra(intent.extra_initial_intents, intentarray);     startactivityforresult(chooser,request_code); 

i want onactivityresult method this:

protected void onactivityresult(int requestcode, int resultcode, intent data) {     if(condition == picture_coming_from_gallery)     {      //my code here     }     else if(condition == picture_coming_from_camera)     {      //another code here     } } 

what condition allows me know source image coming from?

updated:

now it's working , here solution:

    protected void onactivityresult(int requestcode, int resultcode, intent data)  {     if (requestcode == request_code && resultcode == activity.result_ok)     {         if(data.getdata()!=null)         {             try              {             if (bitmap != null)                  {                     bitmap.recycle();                 }              inputstream stream = getcontentresolver().openinputstream(data.getdata());             bitmap = bitmapfactory.decodestream(stream);             stream.close();             imageview.setimagebitmap(bitmap);             }          catch (filenotfoundexception e)              {                 e.printstacktrace();             }          catch (ioexception e)              {                 e.printstacktrace();             }         }           else          {             bitmap=(bitmap) data.getextras().get("data");              imageview.setimagebitmap(bitmap);         }          super.onactivityresult(requestcode, resultcode, data);     } } 

thank you help!

although current piece of code neat way of presenting options choose from, found severely difficult manage. @ least in use case was. need store images taken off camera process further in aviary sdk (if user chooses).

to end, propose workaround.

this not address question per se. offers alternative considering need know image coming (camera / gallery).

alertdialog.builder builder = new alertdialog.builder(statusupdate.this); builder.settitle("choose image source"); builder.setitems(new charsequence[] {"gallery", "camera"},          new dialoginterface.onclicklistener() {      @override     public void onclick(dialoginterface dialog, int which) {         switch (which) {         case 0:              // image gallery             intent intent = new intent(intent.action_get_content);             intent.settype("image/*");              intent chooser = intent.createchooser(intent, "choose picture");             startactivityforresult(chooser, action_request_gallery);              break;          case 1:             intent getcameraimage = new intent("android.media.action.image_capture");              file camerafolder;              if (android.os.environment.getexternalstoragestate().equals                     (android.os.environment.media_mounted))                 camerafolder = new file(android.os.environment.getexternalstoragedirectory(),                         "some_directory_to_save_images/");             else                 camerafolder= statusupdate.this.getcachedir();             if(!camerafolder.exists())                 camerafolder.mkdirs();              simpledateformat dateformat = new simpledateformat("yyyymmdd't'hhmmss");             string timestamp = dateformat.format(new date());             string imagefilename = "picture_" + timestamp + ".jpg";              file photo = new file(environment.getexternalstoragedirectory(),                      "some_directory_to_save_images/" + imagefilename);             getcameraimage.putextra(mediastore.extra_output, uri.fromfile(photo));             initialuri = uri.fromfile(photo);              startactivityforresult(getcameraimage, action_request_camera);              break;          default:             break;         }     } });  builder.show(); 

this result (i still maintain code gives better selection set, again, not simplest thing in use case or in mine either):

enter image description here

now can process result based on source of selection:

protected void onactivityresult(int requestcode, int resultcode, intent data) {     if (resultcode == result_ok)    {          switch (requestcode) {         case action_request_gallery:              break;          case action_request_camera:              break;                   }      } }; 

updated:

found it!! there answer here on so addresses need. still * workaround of sorts*. in sense not rely on different requestcodes. works nonetheless.

strange missed when stuck this. :-(

note: i not posting code here , linking instead. props go original author. :-)


Comments