Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using intent to use Camera in Android

Tags:

android

camera

I am using the following code to use camera by using intent. In the parameter of intent I am passing android.provider.MediaStore.ACTION_IMAGE_CAPTURE. It is able to open the camera. But the problem is that it stops unexpectedly. The problem is that it gives null pointer exception on OnActivityResults. I have used the below code:

public class demo extends Activity {

Button ButtonClick;
int CAMERA_PIC_REQUEST = 2; 
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    ButtonClick =(Button) findViewById(R.id.Camera);
    ButtonClick.setOnClickListener(new OnClickListener (){
        @Override
        public void onClick(View view)
        {
            Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
            // request code

            startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST);

        }
    });

}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) 
{
    if( requestCode == CAMERA_PIC_REQUEST)
    {
    //  data.getExtras()
        Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
        ImageView image =(ImageView) findViewById(R.id.PhotoCaptured);
        image.setImageBitmap(thumbnail);
    }
    else 
    {
        Toast.makeText(demo.this, "Picture NOt taken", Toast.LENGTH_LONG);
    }
    super.onActivityResult(requestCode, resultCode, data);
}
}

Can anyone help me to solve this problem?

like image 879
Rakesh Gondaliya Avatar asked Aug 26 '10 10:08

Rakesh Gondaliya


People also ask

How do I open camera with Intent?

This is done as follows: Intent camera_intent = new Intent(MediaStore. ACTION_IMAGE_CAPTURE); startActivityForResult(camera_intent, pic_id); Now use the onActivityResult() method to get the result, here is the captured image.

How do I access camera on Android application?

You should go onto your apps screen on your android device, where it will display all apps that have been downloaded onto your phone. Also, the camera app comes built into the device, so that means it cannot be accidentally deleted or uninstalled. The camera app will likely be on your apps screen.


2 Answers

Try request code 1337.

startActivityForResult(cameraIntent , 1337);
like image 136
Aduait Pokhriyal Avatar answered Sep 20 '22 17:09

Aduait Pokhriyal


This how I use it

Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, 1337);
like image 30
Ebin Sebastian Avatar answered Sep 22 '22 17:09

Ebin Sebastian