java - 如何在Java类中封装一个startActivityForResult并在调用该方法的Activity上获取onActivityResult

标签 java android android-intent fragment onactivityresult

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

我想要的是 Activity 类是这样的:

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


        }
    }
}
}

我现在遇到的问题是,我从未从 fragment 中收到 onActivityResult 的回电。

最佳答案

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

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

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()

关于java - 如何在Java类中封装一个startActivityForResult并在调用该方法的Activity上获取onActivityResult,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55464068/

相关文章:

java - 从 OpenGL ES 2.0 中读取纹理

java - 如何在生产环境中为 hibernate.hbm2ddl.auto 使用不同的值

android - 在运行时检查 Android Activity 自己的 View 层次结构

android - 错过取消注册我从未注册的 HapticFeedbackBroadcastReceiver 接收器的调用

android - 使用新的 Google 相册应用选择照片已损坏

java - 为什么 PendingIntent 会被触发?

java - 如何阻止程序退出二维数组的边界

java - ILOG JRules 中的技术规则和功能有什么区别?

android - 为 Android 构建 FFmpeg 以使用命令行参数

android - 如何像屏幕过滤器一样降低亮度