Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what does camera request code mean in Android

could someone explain what the camera request code?

why do we use it?

i was trying a practice in android and i see the code.

this is my code that i do practice;

public class imageActivity extends AppCompatActivity {

    Button image;
    private static final int CAMERA_REQUEST = 1888;
    private ImageView imageView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_resim);

        imageView = (ImageView)this.findViewById(R.id.imageView1);
        image = (Button) findViewById(R.id.capture);

        image.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
                startActivityForResult(cameraIntent, CAMERA_REQUEST);
            }
        });


    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) {
            Bitmap photo = (Bitmap) data.getExtras().get("data");
            imageView.setImageBitmap(photo);
        }
    }
}
like image 866
volkangurbuz Avatar asked Aug 29 '26 04:08

volkangurbuz


2 Answers

You can make several calls in a single Activity to startActivityForResult() which allows different Intents to do different actions. Use a request code to identify which is the Intent you are returning from.

For example:

You can start two activities for result:

private static final int CAMERA_REQUEST = 1888;
private static final int GALLERY_REQUEST = 1889;
startActivityForResult(cameraIntent, CAMERA_REQUEST);
startActivityForResult(cameraIntent, GALLERY_REQUEST);

and in your onActivityForResult()

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) {
        //Do stuff with the camara data result
    }
    else if (requestCode == GALLERY_REQUEST && resultCode == RESULT_OK) {
        //Do stuff with the gallery data result
    }
}

Do note that the private static final ints are completely arbitrary.

like image 182
Angel Guevara Avatar answered Aug 31 '26 17:08

Angel Guevara


This is what it means:

When you start an activity for result, like request Camera to take your photo then return it to your calling activity, you pass it a unique integer value like 100001 or anything you have not used already in that class.

So, in short, CAMERA_REQUEST could be any value you define in your class like this:

private static final int CAMERA_REQUEST = 10001;

Now, since you passed it when requesting a photo from the camera, you will use it to check the requestCode because it will have to match it if it is for the Camera itself.

I hope this helps you understand.

like image 28
Eenvincible Avatar answered Aug 31 '26 18:08

Eenvincible



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!