Home > Software design >  Validate buttons after taking a picture in Android
Validate buttons after taking a picture in Android

Time:10-29

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

CodePudding user response:

I've noticed that you're comparing the value of requestCode with IMAGE_REQUEST and next with IMAGE_REQUEST_2.

After that in the section where your code breaks, you're comparing both in one line. But I believe that this will never work since the requestCode has only one value, and you're comparing it with two different values [IMAGE_REQUEST, IMAGE_REQUEST_2]

I suggest you to add the IMAGE_REQUEST_2 and the IMAGE_REQUEST to an array everytime you take a photo with that button.

And then comparing if both values exist in that array, if so you do what you want.

Like that:

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;

// creates a new list to store the photos taken
List<int> requestsMade = new ArrayList<int>();

@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);
        // first photo taken
        requestsMade.add(IMAGE_REQUEST);

    }
    else if (requestCode == IMAGE_REQUEST_2) {
        if (resultCode == Activity.RESULT_OK) {
        
            imageV2.setImageResource(R.drawable.image_view2_2do);
            requestsMade.add(IMAGE_REQUEST_2);
            // second photo taken

        }
    }
    // now this compares if both are taken
    else if (requestsMade.contains(IMAGE_REQUEST) && requestsMade.contains(IMAGE_REQUEST_2)) {
        if (resultCode == Activity.RESULT_OK) {
            imageV.setImageResource(R.drawable.image_view_2do);
            imageV2.setImageResource(R.drawable.image_view2_2do);
        }
    }
 }
}

CodePudding user response:

You can keep a 2 booleans that keep track of whether the images are loaded in the each imageView and update them in onActivityResult.

Check isImage1Loaded and isImage2Loaded in the below code.

ImageView imageV, imageV2;
Button btn1, btn2;
static final int IMAGE_REQUEST = 1;
static final int IMAGE_REQUEST_2 = 2;
boolean isImage1Loaded = false, isImage2Loaded = false;
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) {
        isImage1Loaded = true;
        imageV.setImageResource(R.drawable.image_view_2do);

    }
    else if (requestCode == IMAGE_REQUEST_2) {
        if (resultCode == Activity.RESULT_OK) {
            image2Loaded = true;
            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);
        }
    }
 }
} 

CodePudding user response:

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.

CodePudding user response:

You can manage it through Boolean value like below example;

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;
boolean isFirstImageSet=false;
boolean isSecondImageSet=false;
@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);
       }
    }
 });
 
 btn2.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
     if(isFirstImageSet && isSecondImageSet){
     //Both image are set
     }
     else if(isFirstImageSet ){
     //First image are set
     }
     else if(isSecondImageSet){
     //First image are set
     }
 });
}

@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);
        isFirstImageSet=true;

    }
    else if (requestCode == IMAGE_REQUEST_2) {
        if (resultCode == Activity.RESULT_OK) {
        
            imageV2.setImageResource(R.drawable.image_view2_2do);
            isSecondImageSet=true;
        }
    }
    // 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);
        }
    }
 }
}
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

  • Related