9

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?

Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
Rakesh Gondaliya
  • 1,050
  • 3
  • 25
  • 42
  • Please be clear, whats ur problem, null pointer exception or camera intent stopping unexpectedly – the100rabh Aug 26 '10 at 11:04
  • Here i am sharing what happens after i run my application. 1> Screen appears with Button Take picture. 2> After clicking button my camera opens 3> Then Camera stops unexpectedly – Rakesh Gondaliya Aug 26 '10 at 11:40
  • P.S.- Toast will not be displayed. Toast.makeText(demo.this, "Picture NOt taken", Toast.LENGTH_LONG).show(); – hitesh Aug 02 '17 at 14:02

5 Answers5

13

Try request code 1337.

startActivityForResult(cameraIntent , 1337);
RThomas
  • 10,702
  • 2
  • 48
  • 61
Aduait Pokhriyal
  • 1,529
  • 14
  • 30
  • 1
    whats 1337?? i am using "CAMERA_REQUEST" as parameter whose value is 1888. does that make any difference? – Archie.bpgc Aug 08 '12 at 12:40
  • I don't think so. Actually I was facing the same problem and just came across [link](http://mobile.tutsplus.com/tutorials/android/android-sdk-quick-tip-launching-the-camera/) and in the example it was using code 1337 , without any specific reason i used it and it worked. :) Except this It makes no difference its just define within your application as the request code returned by the Camera image capture Intent and use this value to differentiate between different types of results. – Aduait Pokhriyal Aug 09 '12 at 09:46
  • is there any relation between 1337, 2500, 1888 with the hardware specification? I am afraid that I could use 1337 on my phone, but for my users 's phone, it might not work – Thai Tran Mar 16 '14 at 04:26
3

This how I use it

Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, 1337);
Csabi
  • 3,097
  • 17
  • 59
  • 107
Ebin Sebastian
  • 1,291
  • 1
  • 13
  • 15
1

do you have the following declarations in your manifest ?:

<uses-feature android:name="android.hardware.camera" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera.autofocus" />

?? I used to do the same... here is my call to intent:

File file = new File( _path );
    Uri outputFileUri = Uri.fromFile( file );

    Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE );
    intent.putExtra( MediaStore.EXTRA_OUTPUT, outputFileUri );
    intent.putExtra( MediaStore.EXTRA_VIDEO_QUALITY,1);

the only slight difference between my and your code - I have file path in URI sending among call

Pratik Bhat
  • 7,166
  • 2
  • 35
  • 57
Steve
  • 19
  • 1
  • 3
0

I have used the following code and it worked!

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub
    Intent intent = new Intent(
            android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
    if (intent.resolveActivity(getPackageManager()) != null) {
        startActivityForResult(intent, REQUEST_IMAGE_CAPTURE);
    }
}

@Override
protected void onActivityResult(int requestCode, int resultCode,
        final Intent data) {
    // TODO Auto-generated method stub
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
        im.setImageDrawable(null);
        im.destroyDrawingCache();
        Bundle extras = data.getExtras();
        Bitmap imagebitmap = (Bitmap) extras.get("data");
        im.setImageBitmap(imagebitmap);
    }

}
László Papp
  • 51,870
  • 39
  • 111
  • 135
0

Using intent to use Camera in Android

##

   Uri imageUri;
1:- 

     TextView camera = (TextView)findViewById(R.id.camera);
         camera.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            final Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            File photo = new File(Environment.getExternalStorageDirectory(), new Date().getTime() + "myPic.jpg");
            cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT,
                    Uri.fromFile(photo));
            imageUri = Uri.fromFile(photo);
            startActivityForResult(cameraIntent, IMAGE_CAMERA_REQUEST);}


2:-



        @Override
            public void onActivityResult(int requestCode, int resultCode, Intent data) {
                super.onActivityResult(requestCode, resultCode, data);
                try {

                    if (requestCode == IMAGE_CAMERA_REQUEST && resultCode == Activity.RESULT_OK) {
                        Uri selectedImage = imageUri;
                        getActivity().getContentResolver().notifyChange(selectedImage, null);
                        ContentResolver contentResolver = getActivity().getContentResolver();
                        Bitmap bitmap;
                        try {
                            bitmap = android.provider.MediaStore.Images.Media
                                    .getBitmap(contentResolver, selectedImage);

                            imageDialog(bitmap);
                        } catch (Exception e) {

                            Log.e("Camera", e.toString());
                        }

                    }
    }
    }}

I hope it's working for you.
Ashutosh Srivastava
  • 597
  • 1
  • 9
  • 13