kinyas kinyas - 4 months ago 46
Android Question

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

Answer

You can make several calls in a single Activity to startActivityForResult() what opens different Intents to do different actions. You can use the RequestCode is to identify which is the Intent you are returning from.

For example:

You can start two activity 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 you 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 } }

Hope this help.