android intent - Errors with taking photos and setting it in imageview with Fragments -
i cannot seem able take photo camera , set imageview. have tried many different ways either receive errors or image view wont display photo took. i'll post have coded @ moment
@override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { view view = inflater.inflate(r.layout.fragment_snap, container, false); imageview imageview = (imageview) view.findviewbyid(r.id.camera_display); button camera_btn = (button) view.findviewbyid(r.id.camera_btn); camera_btn.setonclicklistener(new view.onclicklistener() { @override public void onclick(view view) { intent intent = new intent(mediastore.action_image_capture); getactivity().startactivityforresult(intent, capture_image_activity_request_code); } }); return view; }
then onactivityresult
@override public void onactivityresult(int requestcode, int resultcode, intent data) { if (requestcode == capture_image_activity_request_code && resultcode == getactivity().result_ok) { bundle extras = data.getextras(); bitmap imagebitmap = (bitmap) extras.get("data"); imageview.setimagebitmap(imagebitmap); } }
edit getting error
04-29 19:34:14.946 32713-32713/com.example.tim.cryptpix_10 w/bundle﹕ key output expected parcelable value java.lang.integer. default value <null> returned.
here did:
public class mainactivity extends activity {
public final static int capture_image_activity_request_code = 1034; public string photofilename = "photo.jpg"; public final string app_tag = "mycustomapp"; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); intent intent = new intent(mediastore.action_image_capture); intent.putextra(mediastore.extra_output, getphotofileuri(photofilename)); // set image file name // start image capture intent take photo startactivityforresult(intent, capture_image_activity_request_code); } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.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(); if (id == r.id.action_settings) { return true; } return super.onoptionsitemselected(item); } @override public void onactivityresult(int requestcode, int resultcode, intent data) { if (requestcode == capture_image_activity_request_code) { if (resultcode == result_ok) { uri takenphotouri = getphotofileuri(photofilename); // point have camera photo on disk log.d("mainactivity photo path",takenphotouri.getpath()); bitmap takenimage = bitmapfactory.decodefile(takenphotouri.getpath()); // load taken image preview imageview ivpreview = (imageview) findviewbyid(r.id.ivpreview); ivpreview.setimagebitmap(takenimage); } else { // result failure toast.maketext(this, "picture wasn't taken!", toast.length_short).show(); } } } // returns uri photo stored on disk given filename public uri getphotofileuri(string filename) { // safe storage directory photos file mediastoragedir = new file( environment.getexternalstoragepublicdirectory(environment.directory_pictures), app_tag); // create storage directory if not exist if (!mediastoragedir.exists() && !mediastoragedir.mkdirs()){ log.d(app_tag, "failed create directory"); } // return file target photo based on filename return uri.fromfile(new file(mediastoragedir.getpath() + file.separator + filename)); }
}
Comments
Post a Comment