send data from one dialogfragment to another in android -
send data from one dialogfragment to another in android -
i have 3 fragments namely audiorecorderfragment fragment, uploadfragment dialogfragment , dialogfragment known uploadphotodialog. user should record sound audiorecorderfragment , on hitting upload button, uploadfragment shows. in uploadfragment, user come in details of recorded sound clip , may include image. when selecting image,the uploadphotodialog fragment shows , user can select whether take photo using photographic camera or pick 1 gallery. problem how selection uploadphotodialog uploadfragment.i not getting result. when seek phone call photographic camera intent uploadfragment, null pointer exception in
@override public void onactivitycreated(bundle savedinstancestate) { super.onactivitycreated(savedinstancestate); // file url fileuri = savedinstancestate.getparcelable("file_uri"); }
on fileuri ... line whenever want show fragment audiorecorderfragment. how can solve this? here uploadfragment code.
public class uploadfragment extends dialogfragment implements adapterview.onitemselectedlistener, view.onclicklistener { spinner genres; imageview photo; textview submit; string filename=""; private static final string key="choice"; @override public dialog oncreatedialog(bundle savedinstancestate) { alertdialog.builder builder = new alertdialog.builder(getactivity()); // layout inflater layoutinflater inflater = getactivity().getlayoutinflater(); view v = inflater.inflate(r.layout.fragment_upload, null); builder.setview(v); submit = (textview)v.findviewbyid(r.id.upload_textview_submit); genres = (spinner) v.findviewbyid(r.id.upload_spinner_genre); photo = (imageview) v.findviewbyid(r.id.upload_imageview_photo); arrayadapter<string> dataadapter = new arrayadapter<string>(getactivity(), r.layout.ngoma_spinner, getresources().getstringarray(r.array.genres)); dataadapter .setdropdownviewresource(android.r.layout.simple_dropdown_item_1line); genres.setadapter(dataadapter); bundle bundle = this.getarguments(); if(bundle != null){ filename = bundle.getstring("arg_filename"); } genres.setonitemselectedlistener(this); submit.setonclicklistener(this); photo.setonclicklistener(this); homecoming builder.create(); } @override public void onitemselected(adapterview<?> parent, view view, int position, long id) { } @override public void onnothingselected(adapterview<?> parent) { } private static final int int_code=1; @override public void onclick(view v) { switch(v.getid()){ case r.id.upload_textview_submit: getdialog().dismiss(); break; case r.id.upload_imageview_photo: uploadphotodialog uploaddialog = new uploadphotodialog(); uploaddialog.settargetfragment(this, int_code); uploaddialog.show(getactivity().getsupportfragmentmanager(), "uploadphoto"); break; } } /******************************************************************************************************/ private static final int camera_capture_image_request_code = 100; public static final int media_type_image = 1; private static int result_load_image = 1; /** * select image gallery */ private void selectfromgallery() { intent = new intent(intent.action_pick, android.provider.mediastore.images.media.external_content_uri); startactivityforresult(i, result_load_image); } /** * capturing photographic camera image lauch photographic camera app requrest image capture */ private void captureimage() { intent intent = new intent(mediastore.action_image_capture); fileuri = getoutputmediafileuri(media_type_image); intent.putextra(mediastore.extra_output, fileuri); // start image capture intent startactivityforresult(intent, camera_capture_image_request_code); } /** * here store file url null after returning photographic camera * app */ // directory name store captured images , videos private static final string image_directory_name = "ngoma"; private uri fileuri; // file url store image/video @override public void onsaveinstancestate(bundle outstate) { super.onsaveinstancestate(outstate); // save file url in bundle null on screen orientation // changes outstate.putparcelable("file_uri", fileuri); } /* @override protected void onrestoreinstancestate(bundle savedinstancestate) { super.onrestoreinstancestate(savedinstancestate); // file url fileuri = savedinstancestate.getparcelable("file_uri"); }*/ @override public void onactivitycreated(bundle savedinstancestate) { super.onactivitycreated(savedinstancestate); // file url // fileuri = savedinstancestate.getparcelable("file_uri"); } /** * receiving activity result method called after closing photographic camera * */ @override public void onactivityresult(int requestcode, int resultcode, intent data) { super.onactivityresult(requestcode,resultcode, data); // if result capturing image if (requestcode == camera_capture_image_request_code) { if (resultcode == activity.result_ok) { // captured image // display in image view previewcapturedimage(); } else if (resultcode == activity.result_canceled) { // user cancelled image capture toast.maketext(getactivity().getapplicationcontext(), "user cancelled image capture", toast.length_short) .show(); } else { // failed capture image toast.maketext(getactivity().getapplicationcontext(), "sorry! failed capture image", toast.length_short) .show(); } } else if (requestcode == result_load_image && resultcode == activity.result_ok && null != data) { uri selectedimage = data.getdata(); string[] filepathcolumn = { mediastore.images.media.data }; cursor cursor = getactivity().getcontentresolver().query(selectedimage, filepathcolumn, null, null, null); cursor.movetofirst(); int columnindex = cursor.getcolumnindex(filepathcolumn[0]); string picturepath = cursor.getstring(columnindex); cursor.close(); // string picturepath contains path of selected image previewfromgallery(picturepath); } if (requestcode == int_code) { //make sure fragment codes match { string selection = data.getstringextra(key); if(choice.equalsignorecase("camera")){ captureimage(); } else if(choice.equalsignorecase("gallery")){ selectfromgallery(); } } } /** * display image path imageview */ private void previewcapturedimage() { seek { // bimatp mill bitmapfactory.options options = new bitmapfactory.options(); // downsizing image throws outofmemory exception larger // images options.insamplesize = 8; final bitmap bitmap = bitmapfactory.decodefile(fileuri.getpath(), options); photo.setimagebitmap(bitmap); } grab (nullpointerexception e) { e.printstacktrace(); } } private void previewfromgallery(string picturepath) { photo.setimagebitmap(bitmapfactory.decodefile(picturepath)); } /** * ------------ helper methods ---------------------- * */ /** * creating file uri store image/video */ public uri getoutputmediafileuri(int type) { homecoming uri.fromfile(getoutputmediafile(type)); } /** * returning image / video */ private static file getoutputmediafile(int type) { // external sdcard location file mediastoragedir = new file( environment .getexternalstoragepublicdirectory(environment.directory_pictures), image_directory_name); // create storage directory if not exist if (!mediastoragedir.exists()) { if (!mediastoragedir.mkdirs()) { log.d(image_directory_name, "oops! error creating " + image_directory_name + " directory."); homecoming null; } } // create media file name string timestamp = new simpledateformat("yyyymmdd_hhmmss", locale.getdefault()).format(new date()); file mediafile; if (type == media_type_image) { mediafile = new file(mediastoragedir.getpath() + file.separator + "img_" + timestamp + ".jpg"); } else { homecoming null; } homecoming mediafile; } }
and uploadphotodialog fragment code
public class uploadphotodialog extends dialogfragment implements view.onclicklistener { linearlayout fromgallery, fromcamera; string choice; @override public dialog oncreatedialog(bundle savedinstancestate) { alertdialog.builder builder = new alertdialog.builder(getactivity()); // layout inflater layoutinflater inflater = getactivity().getlayoutinflater(); view v = inflater.inflate(r.layout.fragment_uploadphoto_dialog, null); // inflate , set layout dialog // pass null parent view because going in dialog layout selection = ""; builder.setview(v); fromgallery = (linearlayout) v .findviewbyid(r.id.uploadphoto_gallery_layout); fromcamera = (linearlayout) v .findviewbyid(r.id.uploadphoto_camera_layout); fromcamera.setonclicklistener(this); fromgallery.setonclicklistener(this); homecoming builder.create(); } @override public void onclick(view v) { switch (v.getid()) { case r.id.uploadphoto_gallery_layout: // intent intent = new intent(getactivity(), audiorecorder.class); //startactivity(intent); //audiorecorderfragment audiorecorder= new audiorecorderfragment(); //getactivity().getsupportfragmentmanager().begintransaction().replace(r.id.content_frame,audiorecorder).commit(); selection ="gallery"; getdialog().dismiss(); break; case r.id.uploadphoto_camera_layout: //intent intentvideo = new intent(getactivity(), videorecorderactivity.class); //startactivity(intentvideo); selection ="camera"; getdialog().dismiss(); break; } } private static final string key="choice"; private static final int int_code=1; private void sendresult(int int_code) { intent = new intent(); i.putextra(key, choice); gettargetfragment().onactivityresult(gettargetrequestcode(), int_code, i); } }
sorry know thats lot of code best way of expressing problem. help highly appreciated. in advance.
when show fragment can pass info through setarguments function of fragment or establishing interface between 2 fragments, along lines:
public interface ireceivechoice { void selected(int choice); }
then in class want take implement ireceivechoice;
public class uploadphotodialog extends dialogfragment implements view.onclicklistener, ireceivechoice { @override public void selected(int choice) { //do stuff } ....
and when take in first activity find other activity, cast ireceivechoice , phone call method selected choice.
fragment f = gettargetfragment(); if(f != null && f instanceof ireceivechoice) { f.selected(choice); }
there have ensure fragment exists alternative sending whole lot of bundles everywhere if info little , simple.
android android-fragments android-dialogfragment android-camera-intent dialogfragment
Comments
Post a Comment