如何将 startActivityForResult 封装在 Java class 中并在调用该方法的 Activity 上获取 onActivityResult

How to encapsulate a startActivityForResult in a Java class and get the onActivityResult on the Activity that called the method

我在几个活动中使用了下一个摄像头代码,我想制作一个 class 来封装 Android 中使用摄像头的方法。

我想要得到的是 Activity class 是这样的:

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
}

相机 class 看起来像这样:

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 的回电。

OS 不支持将 onActivityResult() 发送到 Fragment。不过,支持库有一种机制可以做到这一点,即在 AppCompatActivity 中注册对特殊 table 的调用。这里的技巧是你必须使用 Fragment 自己的 startActivityForResult(),而不是 Activity

因此,您的 Camera class 代码应如下所示:

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

}

注意最后一行使用了 FragmentstartActivityForResult()