从相机或图库上传图片时,startActivityForResult不会在片段中工作

时间:2016-06-01 06:20:26

标签: android android-fragments imageview fragment gallery

public class Profile extends Fragment implements Profile_frg{


imageView.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

                final Dialog d = new Dialog(mainActivity);
                d.requestWindowFeature(Window.FEATURE_NO_TITLE);
                d.setContentView(R.layout.activity_custom_dialog);
                d.setCanceledOnTouchOutside(true);


                gallery = (ImageView) d.findViewById(R.id.imageView1);
                camera = (ImageView) d.findViewById(R.id.imageView2);
                cancel = (ImageView) d.findViewById(R.id.imageView3);

                cancel.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        // TODO Auto-generated method stub
                        d.dismiss();
                    }
                });


                gallery.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        try {
                            Intent gintent = new Intent();
                            gintent.setType("image/*");
                            gintent.setAction(Intent.ACTION_GET_CONTENT);
                            startActivityForResult(Intent.createChooser(
                                    gintent, "Select Picture"), PICK_IMAGE);
                        } catch (Exception e) {
                            Toast.makeText(mainActivity,
                                    e.getMessage(), Toast.LENGTH_LONG).show();
                            Log.e(e.getClass().getName(), e.getMessage(), e);
                        }

                        d.dismiss();
                    }

                });

                camera.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View v) {

                        // define the file-name to save photo taken by Camera
                        // activity
                        String fileName = "new-photo-name.jpg";
                        // create parameters for Intent with filename
                        ContentValues values = new ContentValues();
                        values.put(MediaStore.Images.Media.TITLE, fileName);
                        values.put(MediaStore.Images.Media.DESCRIPTION,
                                "Image captured by camera");
                        // imageUri is the current activity attribute, define
                        // and save it for later usage (also in
                        // onSaveInstanceState)
                        imageUri = context.getContentResolver().insert(
                                MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
                                values);
                        // create new Intent
                        Intent intent = new Intent(
                                MediaStore.ACTION_IMAGE_CAPTURE);
                        intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
                        intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1);

                        startActivityForResult(intent, PICK_Camera_IMAGE);

                        d.dismiss();
                    }

                });
                d.show();
            }
        });

}// Work Fine till here...



public void onActivityResult(int requestCode,int resultCode,Intent data)
    {
        super.onActivityResult(requestCode, resultCode, data);

    }//didn't detect this method

2 个答案:

答案 0 :(得分:2)

是的,片段中没有 onActivityResult()回调。 您必须覆盖主机活动中的activityResult方法(定义了您的片段)

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
   if(requestCode == GALLERY/CAMERA_REQUEST_CODE && resultCode == Activity.RESULT_OK) {
       Fragment yourFragment = getSupportFragmentManager().findFragmentByTag("FRAGMENT_TAG"); // same tag while adding fragment for the first time.
       if (yourFragment != null) {
           yourFragment.onActivityResult(requestCode, resultCode, data); //calling method that should be defined in your fragment.
       }
   }
   super.onActivityResult(requestCode, resultCode, data);
}

在你的片段中你喜欢这样:

public void onActivityResult(int requestCode,int resultCode,Intent data) {
   ...
   Pull your image data from data object
   do your further process from here.
   ...
}

答案 1 :(得分:0)

是的,如果您从任何片段调用它, startActivityForResult 将无法直接使用。获得结果后,回调将点击托管活动的 onActivityResult,您必须手动将其重定向到相应的片段

以下是您的Activity的onActivityResult示例代码。请注意,这只会将结果重定向到相应的片段。

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    switch (requestCode) {
        case 1001:
            Fragment frag = getSupportFragmentManager().findFragmentByTag("TAG"); // TAG should be same as the one you entered while adding the fragment
            if (frag != null) {
                frag .onActivityResult(requestCode, resultCode, data);
            }
            break;
    }
    super.onActivityResult(requestCode, resultCode, data);
}