onActivityResult未被警报对话框FIXED触发

时间:2014-01-31 17:08:39

标签: java android android-fragments onactivityresult

除了我应用中的照片外,我还添加了上传视频的功能。要完成此操作,现在当您点击附加按钮b2而不是图片选择器加载时,它会加载一个警告对话框showDialog(),以便在选择时询问您要上传的内容(视频或照片),然后加载图像或视频选择器。问题是,当你从内部类调用方法doPositiveClick(Activity activity, int requestCode)时,onActivityResult没有被触发,也没有返回数据。我认为它与从内部类MyAlertDialogFragment调用它有关,但我不确定如何处理它。谢谢。

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");

代码到此为止^^^^                       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();
        }
    }
}

1 个答案:

答案 0 :(得分:1)

您需要确保为对话框设置目标片段。要在显示对话框之前添加该行:

newFragment.setTargetFragment(ChatRoomFragment.this, REQUEST_CODE);
在你的showDialog方法中

。这样做会告诉你的对话框哪个片段应该接收活动的结果。

另外,定义三个常量:

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

现在,在你的对话框中,你应该做的事情是:

...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); ...

然后在onActivityResult方法中,您需要检查请求代码和结果代码对:

if(requestCode == REQUEST_CODE && resultCode == RESULT_PHOTO) { doPositiveClick(); } ...