猿问

如何将启动活动结果封装在 Java 类中,并获取调用该方法的活动上的 onActivity结果

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

}

相机类如下所示:


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


犯罪嫌疑人X
浏览 96回答 1
1回答

FFIVE

操作系统不支持发送到 的 。但是,支持库具有执行此操作的机制,该机制将调用注册到 中的特殊表。这里的诀窍是,你必须使用 自己的启动活动结果(),而不是 的一个。onActivityResult()FragmentAppCompatActivityFragmentActivity因此,您的类代码应如下所示:Camerapublic 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()
随时随地看视频慕课网APP

相关分类

Java
我要回答