Home > Enterprise >  How can I validate different buttons after taking a picture?
How can I validate different buttons after taking a picture?

Time:10-28

I have 2 imageview with a certain image which send to the camera of the device and I want to validate, I want that when a photo is taken it changes image depending on the button from which the photo was taken.

I tried to do it this way but it didn't work.

ImageView imageV, imageV2;
static final int IMAGE_REQUEST = 1;
private static final int PERMISSION_REQUEST = 2;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

imageV = findViewById(R.id.image_view);
imageV2 = findViewById(R.id.image_view_2);

btnStatementAccount.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
       Intent camara = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
       if (camara.resolveActivity(getPackageManager()) != null) {
          startActivityForResult(camara, IMAGE_REQUEST);
       }
    }
});

btnIne.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
       Intent camara = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
       if (camara.resolveActivity(getPackageManager()) != null) {
          startActivityForResult(camara, IMAGE_REQUEST);
       }
    }
 });
}

@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == IMAGE_REQUEST) {
        if (resultCode == Activity.RESULT_OK) {
            //Images to change
            imageV.setImageResource(R.drawable.image_view_2do);
            imageV2.setImageResource(R.drawable.image_view2_2do);

        }
        else {
            //Default images
            imageV.setImageResource(R.drawable.image_view_1er);
            imageV2.setImageResource(getResources().getString(R.string.image_view2_1er));
        }
    }

}
  

CodePudding user response:

You have to add different request code in startActivityForResult. Like you have used IMAGE_REQUEST for both button.

As Example :

ImageView imageV, imageV2;
static final int IMAGE_REQUEST = 1;
static final int IMAGE_REQUEST_2 = 2;
private static final int PERMISSION_REQUEST = 2;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

imageV = findViewById(R.id.image_view);
imageV2 = findViewById(R.id.image_view_2);

btnStatementAccount.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
       Intent camara = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
       if (camara.resolveActivity(getPackageManager()) != null) {
          startActivityForResult(camara, IMAGE_REQUEST);
       }
    }
});

btnIne.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
       Intent camara = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
       if (camara.resolveActivity(getPackageManager()) != null) {
          startActivityForResult(camara, IMAGE_REQUEST_2);
       }
    }
 });
}

@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == IMAGE_REQUEST) {
        if (resultCode == Activity.RESULT_OK) {
            //Images to change
            imageV.setImageResource(R.drawable.image_view_2do);
            imageV2.setImageResource(R.drawable.image_view2_2do);

        }
        else if (requestCode == IMAGE_REQUEST_2) {
        if (resultCode == Activity.RESULT_OK) {
            //Images to change
            imageV.setImageResource(R.drawable.image_view_2do);
            imageV2.setImageResource(R.drawable.image_view2_2do);

        }
        else {
            //Default images
            imageV.setImageResource(R.drawable.image_view_1er);
            imageV2.setImageResource(getResources().getString(R.string.image_view2_1er));
        }
    }

}
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

  • Related