【问题标题】:How to encapsulate a startActivityForResult in a Java class and get the onActivityResult on the Activity that called the method如何在Java类中封装一个startActivityForResult并获取调用该方法的Activity上的onActivityResult
【发布时间】:2019-08-23 03:32:35
【问题描述】:

我在几个活动中使用下一个相机代码,我想创建一个类来封装在 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);


        }
    }
}
}

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

【问题讨论】:

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

标签: java android android-intent fragment onactivityresult


【解决方案1】:

操作系统不支持将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()

【讨论】:

    猜你喜欢
    • 2012-08-27
    • 2016-12-08
    • 2014-03-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-11
    • 1970-01-01
    相关资源
    最近更新 更多