简体   繁体   English

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

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

Im using the next camera code in several activities and I would like to make a class that encapsulate the methods for using the camera in Android. 我在几个活动中使用下一个摄像头代码,我想创建一个类,用于封装在Android中使用摄像头的方法。

What I am trying to get is the Activity class be something like: 我想要得到的是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
}

The Camera class looks something like this: 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);


        }
    }
}
}

The issue that I have right now is that I never get a call back from onActivityResult from the fragment. 我现在遇到的问题是我从来没有从片段中获取onActivityResult回调。

OS has no support to send onActivityResult() to Fragment 's. 操作系统不支持将onActivityResult()发送到Fragment的。 Support library, though, has a mechanism to do that, that registers the call to a special table in the AppCompatActivity . 但是,支持库有一种机制可以将调用注册到AppCompatActivity的特殊表。 The trick here is that you have to use Fragment 's own startActivityForResult() , not Activity 's one. 这里的诀窍是你必须使用Fragment自己的startActivityForResult() ,而不是Activity的。

So, your Camera class code should look like: 因此,您的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);
            }
        }
    }

}

Notice last line is using Fragment 's startActivityForResult() 注意最后一行是使用FragmentstartActivityForResult()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 不从调用startActivityForResult的类调用onActivityResult - onActivityResult is not called from class where startActivityForResult is called startActivityForResult 2如何从1个父Activity激活并在只有1个孩子被分裂时获得onActivityResult - How startActivityForResult 2 Activity from 1 parent Activity and get onActivityResult when only 1 child is finisched 如何使用非活动类中的onActivityResult方法获取结果 - How to use onActivityResult method from a non-activity class to get the resuts 如何检查startActivityForResult中按下了哪个按钮,并覆盖onActivityResult方法? - How to check which button was pressed in startActivityForResult, override onActivityResult method? 如何在Java中封装多个类? - How to encapsulate multiple class in java? 获取非活动类的 onActivityResult (Android) - get onActivityResult for Non Activity Class (Android) 从适配器中的startActivityForResult返回时获取onActivityResult - Get onActivityResult when returning from startActivityForResult in Adapter 调用onActivityResult()方法太早,无法启动通话活动? - onActivityResult() method called too early for starting call activity? 如何在非活动 class android 中调用 onActivityResult - how to call onActivityResult in non Activity class android 未调用 onActivityResult 方法 - The onActivityResult method is not called
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM