如何在一个活动中两次使用onActivityResult

时间:2019-06-01 10:49:10

标签: android onactivityresult

error:method onActivityResult(int,int, Intent) is already defined in class scan.

因此,我正在尝试构建一个应用程序,在其中捕捉照片,然后该应用程序检测照片上的文本。可悲的是,我两次使用方法onActivityResult。 由于我是编码的新手,而且总体来说有点愚蠢,所以我不知道该如何解决。

在我第一次使用它们的整数后面添加数字1,但那没有做到

private void camera(){
    Intent intent2 = new Intent (MediaStore.ACTION_IMAGE_CAPTURE);
    startActivityForResult(intent2, 0);
}
@Override
protected void onActivityResult (int requestCode1, int resultCode1 , Intent data1) {
    super.onActivityResult(requestCode1,resultCode1,data1);
    if (resultCode1==RESULT_OK){
        Bitmap btp = (Bitmap) data1.getExtras().get("data");
        imageView.setImageBitmap(btp);
    }
}

public void detect (View v){
    if (bitmap == null){
        Toast.makeText(getApplicationContext(),"Bitmap is null",Toast.LENGTH_SHORT).show();
    }
    else {
        FirebaseVisionImage firebaseVisionImage = FirebaseVisionImage.fromBitmap(bitmap);
        FirebaseVisionTextRecognizer firebaseVisionTextRecognizer = FirebaseVision.getInstance().getOnDeviceTextRecognizer();
        firebaseVisionTextRecognizer.processImage(firebaseVisionImage)
                .addOnSuccessListener(new OnSuccessListener<FirebaseVisionText>() {
                    @Override
                    public void onSuccess(FirebaseVisionText firebaseVisionText) {
                        process_text(firebaseVisionText);
                    }
                });

    }

}

private void process_text(FirebaseVisionText firebaseVisionText) {
    List<FirebaseVisionText.TextBlock> blocks = firebaseVisionText.getTextBlocks();
    if (blocks.size() == 0){
        Toast.makeText(getApplicationContext(),"no text detected",Toast.LENGTH_LONG).show();
    }
    else {
        for (FirebaseVisionText.TextBlock block:firebaseVisionText.getTextBlocks()){
            String text = block.getText();
            textView.setText(text);
        }
    }
}

public void pick_image(View v){
    Intent i = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.INTERNAL_CONTENT_URI);
    startActivityForResult(i,1);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if(requestCode == 1 && resultCode == RESULT_OK)
    {
        Uri uri = data.getData();
        try {
            bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(),uri);
            imageView.setImageBitmap(bitmap);
        }catch (IOException e){
            e.printStackTrace();
        }

    }
}

错误:在类扫描中已经定义了方法onActivityResult(int,int,Intent)

3 个答案:

答案 0 :(得分:0)

您只需要将其放在一个onActivityResult中即可。

    @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if(requestCode == 1 && resultCode == RESULT_OK)
    {
        Uri uri = data.getData();
        try {
            bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(),uri);
            imageView.setImageBitmap(bitmap);
        }catch (IOException e){
            e.printStackTrace();
        }

    }
if (resultCode==RESULT_OK){
        Bitmap btp = (Bitmap) data.getExtras().get("data");
        imageView.setImageBitmap(btp);
    }
}

答案 1 :(得分:0)

在类中定义请求代码 static final int REQUEST_PHOTO_1 = 1; static final int REQUEST_PHOTO_2 = 2;

然后在通话时使用

startActivityForResult(intent1, REQUEST_PHOTO_1);
startActivityForResult(intent2, REQUEST_PHOTO_2);

现在,在onActivityResult中,您可以执行以下操作。

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == REQUEST_PHOTO_1 && resultCode == RESULT_OK)
{
    ....
}
else if(requestCode == REQUEST_PHOTO_2 && resultCode == RESULT_OK)
{
    ....
}
else if (resultCode==RESULT_OK){
    Bitmap btp = (Bitmap) data.getExtras().get("data");
    imageView.setImageBitmap(btp);
}
}

答案 2 :(得分:0)

看看at the documentationthe reference

startActivityForResult(Intent intent, int requestCode)的第二个参数是一个请求代码,它将作为onActivityResult(int requestCode, int resultCode, Intent data)的第一个参数传递。

示例:

private void camera(){
    Intent intent2 = new Intent (MediaStore.ACTION_IMAGE_CAPTURE);
    startActivityForResult(intent2, 0);
}

public void pick_image(View v){
    Intent i = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.INTERNAL_CONTENT_URI);
    startActivityForResult(i, 1);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if(resultCode == RESULT_OK)
    {
        switch(requestCode){
            case 0: // handle camera
                Bitmap btp = (Bitmap) data1.getExtras().get("data");
                imageView.setImageBitmap(btp);
            break;
            case 1: // handle pick_image
               Uri uri = data.getData();
               try {
                   bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), uri);
                   imageView.setImageBitmap(bitmap);
               } catch (IOException e){
                   e.printStackTrace();
               }
            break;
        }
    }
}

如文档所示,您应该将请求代码重构为类常量,以提高可读性和更容易理解。