java - onActivityResult not being trigger from Alert Dialog FIXED -
im adding ability upload videos in addition photos in app. accomplish this, when click attach button b2
instead of image chooser loading loads alert dialog showdialog()
ask upload (video or photo) upon selecting, loads image or video selector. problem when call method dopositiveclick(activity activity, int requestcode)
inner class onactivityresult not being triggered , no data being returned. feel has being called inner class myalertdialogfragment
i'm unsure how handle it. thank you.
public static final int request_code = 0, result_photo = 1, result_vid = 2; void showdialog() { dialogfragment newfragment = myalertdialogfragment.newinstance( r.string.alert_dialog_two_buttons_title); newfragment.settargetfragment(chatroomfragment.this, request_code); newfragment.show(getfragmentmanager(), "dialog"); } @override if(requestcode == request_code && data.getdata() != null) { log.v("response", "photo selected"); uri _uri = data.getdata(); log.v("response", "cp1/4");
code making point^^^^ b2.setimageresource(r.drawable.picattached);
if (_uri != null) { //user has pick image. cursor cursor = getactivity().getcontentresolver().query(_uri, new string[] { android.provider.mediastore.images.imagecolumns.data }, null, null, null); //cursor.movetofirst(); if (cursor == null){ uploadmsgpic = _uri.getpath(); log.i("response", "cursor == null"); log.i("response", "uploadmsgpic = " + uploadmsgpic); }else{ log.i("response", "cursor = "+ cursor); cursor.movetofirst(); log.v("response", "cp2/4"); //link image //cursor.movetofirst(); final string imagefilepath = cursor.getstring(0); log.i("response", "imagefilepath == " + imagefilepath); log.v("response", "cp3/4"); uploadmsgpic = imagefilepath; log.v("response", "4/4"); cursor.close(); if (uploadmsgpic == null) uploadmsgpic = _uri.getpath(); } log.i("response", "uploadmsgpic == " + uploadmsgpic); media_attached=true; } } if(requestcode == 6 && data != null && data.getdata() != null){ uri _uri = data.getdata(); log.v("response", "cp1/4"); b2.setimageresource(r.drawable.picattached); if (_uri != null) { //user has pick image. cursor cursor = getactivity().getcontentresolver().query(_uri, new string[] { android.provider.mediastore.images.imagecolumns.data }, null, null, null); //cursor.movetofirst(); if (cursor == null){ uploadmsgpic = _uri.getpath(); log.i("response", "cursor == null"); log.i("response", "uploadmsgpic = " + uploadmsgpic); }else{ log.i("response", "cursor = "+ cursor); cursor.movetofirst(); log.v("response", "cp2/4"); //link image //cursor.movetofirst(); final string imagefilepath = cursor.getstring(0); log.i("response", "imagefilepath == " + imagefilepath); log.v("response", "cp3/4"); uploadmsgpic = imagefilepath; log.v("response", "4/4"); cursor.close(); if (uploadmsgpic == null) uploadmsgpic = _uri.getpath(); } log.i("response", "uploadmsgpic == " + uploadmsgpic); media_attached=true; } } super.onactivityresult(requestcode, resultcode, data); } //generics: //1. long: type of reference(s) passed doinbackground() //2. string: type of reference passed onprogressupdate() //3. string: type of reference returned doinbackground() //value passed onpostexecute() public static void dopositiveclick(activity activity, int requestcode) { log.i("chatroomfragment", "dopositive clicked"); intent intent = new intent(); intent.settype("image/*"); intent.setaction(intent.action_get_content); activity.startactivityforresult(intent.createchooser(intent, "select picture"), request_code); // stuff here. log.i("chatroomfragment", "picture selector loaded"); } public void donegativeclick() { intent intent = new intent(); intent.settype("video/*"); intent.setaction(intent.action_get_content); startactivityforresult(intent.createchooser(intent, "select video"), result_vid); // stuff here. log.i("fragmentalertdialog", "negative click!"); } public static class myalertdialogfragment extends sherlockdialogfragment { public static myalertdialogfragment newinstance(int title) { myalertdialogfragment frag = new myalertdialogfragment(); bundle args = new bundle(); args.putint("title", title); frag.setarguments(args); return frag; } @override public dialog oncreatedialog(bundle savedinstancestate) { int title = getarguments().getint("title"); return new alertdialog.builder(getactivity()) //.seticon(r.drawable.alert_dialog_icon) .settitle(title) .setpositivebutton("photo", new dialoginterface.onclicklistener() { public void onclick(dialoginterface dialog, int whichbutton) { intent data = new intent(); //((fragmentalertdialogsupport)getactivity()).dopositiveclick(); chatroomfragment.dopositiveclick(getactivity(), 5); gettargetfragment().onactivityresult(gettargetrequestcode(), result_photo, data); } } ) .setnegativebutton("video", new dialoginterface.onclicklistener() { public void onclick(dialoginterface dialog, int whichbutton) { //((fragmentalertdialogsupport)getactivity()).donegativeclick(); //donegativeclick(); } } ) .create(); } } }
you need make sure set target fragment dialog. add line before show dialog:
newfragment.settargetfragment(chatroomfragment.this, request_code);
within showdialog method. doing tell dialog fragment supposed receive activity's result.
also, define 3 constants:
public static final int request_code = 0, result_photo = 1, result_vid = 2;
now, within dialog, should doing along lines of:
...setpositivebutton("photo", new dialoginterface.onclicklistener() { public void onclick(...) { intent data = new intent(); gettargetfragment().onactivityresult(gettargetrequestcode(), result_photo, data); }}) .setnegativebutton(... gettargetfragment().onactivityresult(gettargetrequestcode(), result_vid, data); ...
then in onactivityresult method, need check request code , result code pairs:
if(requestcode == request_code && resultcode == result_photo) { dopositiveclick(); } ...
Comments
Post a Comment