1

アプリで写真に加えて動画をアップロードする機能を追加します。これを実現するためb2に、イメージ チューザーをロードする代わりに添付ボタンをクリックshowDialog()すると、選択時にどちら (ビデオまたは写真) をアップロードするかを尋ねるアラート ダイアログがロードされ、イメージまたはビデオ セレクターがロードされます。問題は、内部クラス onActivityResult からメソッドを呼び出すと、doPositiveClick(Activity activity, int requestCode)トリガーされず、データが返されないことです。内部クラスから呼び出されていることに関係があると思いますが、それ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();
        }
    }
}
4

1 に答える 1

1

ダイアログのターゲットフラグメントを確実に設定する必要があります。これを行うには、ダイアログを表示する前に次の行を追加します。

newFragment.setTargetFragment(ChatRoomFragment.this, REQUEST_CODE);

showDialog メソッド内で。そうすることで、どのフラグメントがアクティビティの結果を受け取ることになっているかをダイアログに伝えます。

また、次の 3 つの定数を定義します。

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(); } ...
于 2014-01-31T17:34:52.550 に答える