Xixou
Xixou

Reputation: 3

Open camera application android

I created a project with the theme "Navigation Drawer Activity" and I would like to have two options on the left menu :

Here is a part of the code :

`

@Override
    public boolean onNavigationItemSelected(MenuItem item) {
        // Handle navigation view item clicks here.
        int id = item.getItemId();

if (id == R.id.nav_camera) { // Handle the camera action } else if (id == R.id.nav_gallery) { } else if (id == R.id.nav_slideshow) { } else if (id == R.id.nav_manage) { } else if (id == R.id.nav_share) { } else if (id == R.id.nav_send) { } DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); drawer.closeDrawer(GravityCompat.START); return true; }

I tried several things but none of them opened the device camera.

Thank you !

Upvotes: 0

Views: 1084

Answers (2)

Zahan Safallwa
Zahan Safallwa

Reputation: 3904

For starting camera intent

 Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
 startActivityForResult(intent, 0); // 0 is the request code

For receiving the result image

  protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // TODO Auto-generated method stub
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == 0) {  //checking if its for our sent request
        if (resultCode == getActivity().RESULT_OK) { //checking if the intent sent result successfully
    Bitmap bp = (Bitmap) data.getExtras().get("data"); //converting camera intent result to bitmap image
   } 
   }
  } 

How to open gallery

 Intent intent = new Intent();
 intent.setType("image/*");
 intent.setAction(Intent.ACTION_GET_CONTENT);//
 startActivityForResult(Intent.createChooser(intent, "Select   Picture"),1111); //1111 is request code

Why request code :

It is used so that when result is received from that intent we can match that it is sent from which intent. Just like a flag for testing. you can see the use in my example code above when receiving data from camera and setting to Bitmap. Its not that you need to add 0 or 1111. You can put any integer value

Upvotes: 0

Shashank Udupa
Shashank Udupa

Reputation: 2223

For starting inbuilt camera

Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);                                                
startActivityForResult(intent, PIC_CAPTURED);

For opening gallery to select picture

Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent,
                  "Select Picture"), RESULT_LOAD_IMG);

Upvotes: 2

Related Questions