Search code examples
javaandroidvalidationonactivityresult

Validate buttons after taking a picture in Android


Button validation after taking a picture in Android. In my activity I have implemented two imageview(imageview1 and imageview2) with default images and two buttons(button1 and button2) that open the device camera, when the photo is taken from a button a validation is done to change the image view image(button1 -> imageview1, button2 -> imageview2).

I want to do a third validation where it validates that the photo has already been taken from the two buttons.

How can I validate when the photos are already taken on the two buttons?

This is the code I have

ImageView imageV, imageV2;
Button btn1, btn2;
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);
btn1= findViewById(R.id.button_1);
btn2= findViewById(R.id.button_2);

btn1.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);
       }
    }
});

btn2.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) {
      
        imageV.setImageResource(R.drawable.image_view_2do);

    }
    else if (requestCode == IMAGE_REQUEST_2) {
        if (resultCode == Activity.RESULT_OK) {
        
            imageV2.setImageResource(R.drawable.image_view2_2do);
        }
    }
    // This is where the code fails me 
    //how can I validate if the photos are already taken on the two buttons?
    else if (requestCode == IMAGE_REQUEST && requestCode == IMAGE_REQUEST_2) {
        if (resultCode == Activity.RESULT_OK) {
            imageV.setImageResource(R.drawable.image_view_2do);
            imageV2.setImageResource(R.drawable.image_view2_2do);
        }
    }
 }
}

Solution

  • Maintain 2 boolean variables

    boolean isPictureTakenBtn1, isPictureTakenBtn2;

    Then in onActivityResult update the variables accordingly

      onActivityResult(int requestCode, int resultCode, Intent data) {
        Uri selectedImage = null;
        boolean isPictureTakenBtn1, isPictureTakenBtn2;
    
        if (requestCode == IMAGE_REQUEST) {
            if (resultCode == RESULT_OK) {                
                isPictureTakenBtn1 = true;
            } else if (resultCode == RESULT_CANCELED) {
                isPictureTakenBtn1 = false;
                Toast.makeText(this, "Picture was not taken", Toast.LENGTH_SHORT);
            } else {
                isPictureTakenBtn1 = false;
                Toast.makeText(this, "Picture was not taken", Toast.LENGTH_SHORT);
            }
        } else if (requestCode == IMAGE_REQUEST_2) {
            if (resultCode == RESULT_OK) {            
                isPictureTakenBtn2 = true;
            } else if (resultCode == RESULT_CANCELED) {
                isPictureTakenBtn2 = false;
                Toast.makeText(this, "Picture was not taken", Toast.LENGTH_SHORT);
            } else {
                isPictureTakenBtn2 = false;
                Toast.makeText(this, "Picture was not taken", Toast.LENGTH_SHORT);
            }
        }
    
        if (isPictureTakenBtn1) {
            //picture capture from btn1 available, do something
            /* if (data != null) {
                            selectedImage = data.getData();
                            InputStream in;
                            try {
                                in = getContentResolver().openInputStream(uri);
                                Bitmap selected_img = BitmapFactory.decodeStream(in);
                                imageV.setImageBitmap(selected_img);
                            } catch (FileNotFoundException e) {
                                e.printStackTrace();
                                Toast.makeText(this, "An error occured!", Toast.LENGTH_LONG).show();
                            }
                        }*/
        }
        if (isPictureTakenBtn2) {
            //picture capture from btn2 available, do something
            /* if (data != null) {
                            selectedImage = data.getData();
                            InputStream in;
                            try {
                                in = getContentResolver().openInputStream(uri);
                                Bitmap selected_img = BitmapFactory.decodeStream(in);
                                imageV2.setImageBitmap(selected_img);
                            } catch (FileNotFoundException e) {
                                e.printStackTrace();
                                Toast.makeText(this, "An error occured!", Toast.LENGTH_LONG).show();
                            }
                        }*/
        }
    }
    

    Now you can use these boolean values to check if the image was successfully captured and execute your remaining code logic as needed.