onActivityResult 有不同的意图?

onActivityResult with different intents?

我有一个应用程序,我想在其中使用两个按钮,一个用于打开相机,另一个用于打开画廊和 select 图片。

创建 onActivityResult 的正确方法是什么?我从相机开始,它工作正常。然后我添加了 select from gallery 按钮,但第一个 "if" 子句总是被触发,因为 REQUEST_TAKE_PHOTO 总是 true/1。在我发现的所有示例中,它们总是使用最终静态整数来完成此类任务。 在触发 onActivityState 之前,我不应该使用最终静态整数并将它们设置为正确的状态吗? 人们通常如何解决这个问题?

   static final int REQUEST_TAKE_PHOTO = 1;
    static final int PICK_IMAGE_REQUEST = 1;
    ...
    cameraButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        // Ensure that there's a camera activity to handle the intent
        if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
            // Create the File where the photo should go
            File photoFile = null;
            try {
                photoFile = createImageFile();
            } catch (IOException ex) {
                // Error occurred while creating the File
                ex.printStackTrace();
            }
            // Continue only if the File was successfully created
            if (photoFile != null) {
                Uri photoURI = FileProvider.getUriForFile(this,
                        "com.example.fileprovider",
                        photoFile);
                takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
                startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);
            }
        }
    }
            }
        });
galleryButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
                intent.setType("image/*");

                startActivityForResult(Intent.createChooser(intent, "Välj Bild"), PICK_IMAGE_REQUEST);
            }
        });
 @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        //super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK && data != null) {

            if (requestCode == REQUEST_TAKE_PHOTO) {
                try {
                    setPic();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            } else if (requestCode == PICK_IMAGE_REQUEST && data.getData() != null) {
                Uri uri = data.getData();
                mImageView.setImageURI(uri);

            }
        } else {
            Log.d("Error", "Error");
        }
    }

为什么两个请求都使用相同的请求代码。改变这些。

 static final int REQUEST_TAKE_PHOTO = 1;
static final int PICK_IMAGE_REQUEST = 1;

请求代码应该是唯一的,所以使用类似。

 static final int REQUEST_TAKE_PHOTO = 1;
static final int PICK_IMAGE_REQUEST = 2;

onActivityresult() 可以像下面这样。

 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == REQUEST_TAKE_PHOTO) {
        if (resultCode == RESULT_OK) {
            // camera picture
        } else {
            // request canceled
        }
    } else if (requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK) {
        if (data != null) {
            Uri uri = data.getData();
            mImageView.setImageURI(uri);
        }
    }
}

您可以像这样在 OnActivityResult 中使用 switch 语句:

并且您也需要更改您的 int 值,如下所示:

static final int REQUEST_TAKE_PHOTO = 1;
static final int PICK_IMAGE_REQUEST = 2;


 @Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    try {
        switch (requestCode) {
            case REQUEST_TAKE_PHOTO :
              /// your code for this 
            break;
            case PICK_IMAGE_REQUEST :
             // your code here for image request.
            break;
}
}catch (Exception e) {
}