如何在Java类中封装startActivityForResult,并获取调用该方法的活动的onActivityResult

如何在Java类中封装startActivityForResult,并获取调用该方法的活动的onActivityResult,java,android,android-intent,fragment,onactivityresult,Java,Android,Android Intent,Fragment,Onactivityresult,我在几个活动中使用下一个摄像头代码,我想创建一个类来封装在Android中使用摄像头的方法 我想要得到的是活动类,类似于: Public class Myfragment extends Fragment{ @Nullable @Override public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState)

我在几个活动中使用下一个摄像头代码,我想创建一个类来封装在Android中使用摄像头的方法

我想要得到的是活动类,类似于:

Public class Myfragment extends Fragment{
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.profile_fragment, container, false);
        mButtonProfilePhoto = v.findViewById(R.id.buttonProfilePhoto);

        mButtonProfilePhoto.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
        //Here I call the camera intent.
            Camera.dispatchTakePictureIntent(getActivity(), mPhotoFile);
            }
            });

    return v;
    }
        @Override
        public void onActivityResult(int requestCode, int resultCode, Intent data) {
              //handle the camera result
}
Camera类如下所示:

public class Camera{
public static void dispatchTakePictureIntent(Activity activity, File file) {
    Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    // Ensure that there's a camera activity to handle the intent
    if (takePictureIntent.resolveActivity(activity.getPackageManager()) != null) {
        // Create the File where the photo should go
        file = null;
        try {
            file = Camera.createImageFile(activity);
        } catch (IOException ex) {
            // Error occurred while creating the File

        }
        // Continue only if the File was successfully created
        if (file  != null) {
            Uri photoURI = FileProvider.getUriForFile(activity,
                    "com.itcom202.weroom.fileprovider",
                    file );
            takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
            startActivityForResult( takePictureIntent, REQUEST_IMAGE_CAPTURE);


        }
    }
}
}

我现在遇到的问题是,我从未收到来自片段的
onActivityResult
的回调。

操作系统不支持将
onActivityResult()
发送到
fragment
。不过,Support library有一种机制可以做到这一点,即在
AppCompatActivity
中注册对特殊表的调用。这里的诀窍是您必须使用
片段
自己的,而不是
活动

因此,您的
相机
类代码应该如下所示:

public class Camera{

    public static void dispatchTakePictureIntent(Activity activity, Fragment fragment, File file) {
        Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        // Ensure that there's a camera activity to handle the intent
        if (takePictureIntent.resolveActivity(activity.getPackageManager()) != null) {
            // Create the File where the photo should go
            file = null;
            try {
                file = Camera.createImageFile(activity);
            } catch (IOException ex) {
                // Error occurred while creating the File

            }
            // Continue only if the File was successfully created
            if (file  != null) {
                Uri photoURI = FileProvider.getUriForFile(activity,
                        "com.itcom202.weroom.fileprovider",
                        file );
                takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
                fragment.startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
            }
        }
    }

}

请注意,最后一行使用的是
Fragment
startActivityForResult()

您好,欢迎来到s.O.您的意思是要在片段本身而不是其父活动中“捕获”结果事件吗?您好,Barackos,我想从片段中读取onactivityResult,但是摄影机的方法将在类(而不是活动或片段)中实现。