onActivityResult RESULT_OK 无法解析为 android 中的变量?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23085100/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 06:45:03  来源:igfitidea点击:

onActivityResult RESULT_OK can not be resolved to a variable in android?

androidandroid-fragments

提问by Tulsiram Rathod

I am trying to launch camera in fragment but onActivityResult in fragment doesn't resolve RESULT_OK. What should i do?

我正在尝试在片段中启动相机,但片段中的 onActivityResult 无法解析 RESULT_OK。我该怎么办?

I am launching camera using:

我正在使用以下方法启动相机:

public static final int CAMERA_REQUEST_CODE = 1999;

Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_REQUEST_CODE);

get captured image using:

使用以下方法获取捕获的图像:

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == CAMERA_REQUEST_CODE && resultCode == RESULT_OK) {
        Bitmap bitmap = (Bitmap) data.getExtras().get("data");
        if (bitmap != null) {
        }
    }
}

and i want captured image in current fragment!

我想在当前片段中捕获图像!

回答by Pankaj Kumar

RESULT_OKis constant of Activity class. In Activity class you can access directly but in other classes you need to write class name (Activity) also.

RESULT_OK是 Activity 类的常量。在 Activity 类中,您可以直接访问,但在其他类中,您也需要编写类名(Activity)。

Use Activity.RESULT_OKinstead of RESULT_OK.

使用Activity.RESULT_OK代替 RESULT_OK。



In your case it will be

在你的情况下,它将是

if (requestCode == CAMERA_REQUEST_CODE && resultCode == Activity.RESULT_OK) {

回答by Ekta Bhawsar

In fragment we must use getActivity()method as prefix with RESULT_OK.

在片段中,我们必须使用getActivity()方法作为前缀RESULT_OK

In your case it will be:-

在您的情况下,它将是:-

if (requestCode == CAMERA_REQUEST_CODE && resultCode == getActivity().RESULT_OK)

回答by Fivos

Alternatively you can add import static android.app.Activity.RESULT_OK;and use it in your case like if (requestCode == CAMERA_REQUEST_CODE && resultCode == RESULT_OK) {..}

或者,您可以import static android.app.Activity.RESULT_OK;在您的情况下添加和使用它,例如if (requestCode == CAMERA_REQUEST_CODE && resultCode == RESULT_OK) {..}