Question

Im adding the ability to upload videos in addition to photos in my app. To accomplish this, now when you click the attach button b2instead of the image chooser loading it loads an alert dialog showDialog() to ask which you would like to upload (video or photo) upon selecting, it then loads the image or video selector. Problem is now when you call the method doPositiveClick(Activity activity, int requestCode) from the inner class onActivityResult is not being triggered and no data being returned. I feel that has something to do with it being called from the inner class MyAlertDialogFragment but I'm unsure how to 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 is making it to this point^^^^ b2.setImageResource(R.drawable.picattached);

if (_uri != null) {
                      //User has pick an 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 now = " + uploadMsgPic);
                       }else{
                           Log.i("response", "cursor = "+ cursor);
                       cursor.moveToFirst();
                       Log.v("response", "cp2/4");
                       //Link to the 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 an 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 now = " + uploadMsgPic);
                   }else{
                       Log.i("response", "cursor = "+ cursor);
                   cursor.moveToFirst();
                   Log.v("response", "cp2/4");
                   //Link to the 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 to doInBackground()
//2. String: Type of reference passed to onProgressUpdate()
//3. STRING: Type of reference returned by doInBackground()
//Value passed to 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);



        // Do 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);


        // Do 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();
        }
    }
}
Was it helpful?

Solution

You need to make sure to set your target fragment for your dialog. to do this add the line before you show the dialog:

newFragment.setTargetFragment(ChatRoomFragment.this, REQUEST_CODE);

within your showDialog method. Doing so will tell your dialog which fragment is supposed to receive the activity's result.

Also, define three constants:

public static final int REQUEST_CODE = 0, RESULT_PHOTO = 1, RESULT_VID = 2;

Now, within your Dialog, you should be doing something along the 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 your onActivityResult method, you need to do a check for the request code and result code pairs:

if(requestCode == REQUEST_CODE && resultCode == RESULT_PHOTO) { doPositiveClick(); } ...
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top