Android : Use Camera to display image to ImageView and Save to Gallery -


i making app has option image camera or gallery , display in imageview. got android developers tutorial. using dialoginterface one:

private void selectimage() {         final charsequence[] items = { "take photo", "choose library",                 "cancel" };          alertdialog.builder builder = new alertdialog.builder(reportincident.this);         builder.settitle("add picture");         builder.setitems(items, new dialoginterface.onclicklistener() {             @override             public void onclick(dialoginterface dialog, int item) {                 if (items[item].equals("take photo")) {                     takepictureintent = new intent(mediastore.action_image_capture);                     // ensure there's camera activity handle intent                      if (takepictureintent.resolveactivity(getpackagemanager()) != null) {                         // create file photo should go                          try {                             photofile = createimagefile();                             takepictureintent.putextra(mediastore.extra_output, uri.fromfile(photofile));                              startactivityforresult(takepictureintent, 0);                           } catch (ioexception ex) {                             // error occurred while creating file                             ex.printstacktrace();                         }                      }                 } else if (items[item].equals("choose library")) {                     intent intent = new intent(                             intent.action_pick,                             android.provider.mediastore.images.media.external_content_uri);                     intent.settype("image/*");                     startactivityforresult(                             intent.createchooser(intent, "select file"), 1);                 } else if (items[item].equals("cancel")) {                     dialog.dismiss();                 }             }         });         builder.show();     } 

createimagefile creates directory/file photo saved:

private file createimagefile() throws ioexception {     // create image file name     string timestamp = new simpledateformat("yyyymmdd_hhmmss").format(new date());     string imagefilename = "fireflood_" + timestamp + "_";     file storagedir = environment.getexternalstoragepublicdirectory(             environment.directory_pictures);     file image = file.createtempfile(             imagefilename,  /* prefix */             ".jpg",         /* suffix */             storagedir      /* directory */     );      // save file: path use action_view intents     mcurrentphotopath = "file:" + image.getabsolutepath();     return image; } 

the 'choose library' part working have problem 'take photo' part. on line:

takepictureintent.putextra(mediastore.extra_output, uri.fromfile(photofile)); galleryaddpic(); startactivityforresult(takepictureintent, 0); 

having putextra() , galleryaddpic() before startactivityonresult() crashes app after taking picture saves image gallery. deleting these 2 methods code displays photo taken imageview reportimage.

startactivityforresult displays image taken imageview through activityonresult:

public void onactivityresult(int requestcode, int resultcode, intent data) {         super.onactivityresult(requestcode, resultcode, data);          if (resultcode == activity.result_ok) {             if (requestcode == 1) {                  onselectfromgalleryresult(data);             }else if (requestcode == 0) {                 bundle extras = data.getextras();                 bitmap imagebitmap = (bitmap) extras.get("data");                 reportimage.setimagebitmap(imagebitmap);                 }         }     } 

saving image gallery performed galleryaddpic:

private void galleryaddpic() {          intent mediascanintent = new intent(intent.action_media_scanner_scan_file);         file f = new file(mcurrentphotopath);         uri contenturi = uri.fromfile(f);         mediascanintent.setdata(contenturi);         this.sendbroadcast(mediascanintent);     } 

the question can put these 2 methods (putextra , galleryaddpic) make these 2 funtions work:

  1. display photo taken imageview
  2. save photo gallery @ same time.

i can't make these 2 work altogether. please help. tried put putextra , galleryaddpic on onacitivityresult still crashes. galleryaddpic won't work without putextra.

this tutorial try link http://www.androidhive.info/2013/09/android-working-with-camera-api/


Comments

Popular posts from this blog

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

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

StringGrid issue in Delphi XE8 firemonkey mobile app -