5

I want to open a camera of a device when we click on a button in our app. Please help me out.

Sanat Pandey
  • 2,599
  • 5
  • 28
  • 40
  • If you want to use the picture taken in your app use the code provided here [tutorial](http://achorniy.wordpress.com/2010/04/26/howto-launch-android-camera-using-intents/) – pumpkee Apr 26 '11 at 09:36

4 Answers4

18

Inside Button's onClick,

 Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
 startActivityForResult(intent, 0); 

And add Camers Uses Permission in your manifest file.

 <uses-permission android:name="android.permission.CAMERA"></uses-permission>

See the additional discussion here Android camera intent

Community
  • 1
  • 1
Kartik Domadiya
  • 29,868
  • 19
  • 93
  • 104
  • @Kartik using your answer other camera applications are also being in the choice to use do u able to rectify that only default system camera app will open. – PiyushMishra Jan 30 '12 at 11:16
  • i got error Can't connect to camera through this what the solution of it ?? – Nirav Mehta Jun 11 '14 at 08:43
  • You don't need to add `android.permission.CAMERA` to AndroidManifest.xml because you don't use the device cameras, but only run _another application_ called Camera which uses the cameras. – Maksim Dmitriev Jul 08 '14 at 14:05
2

Use this

BtnSelectImage.setOnClickListener(new Button.OnClickListener() 
{ 
    @Override
    public void onClick(View v)
    {
        startCamera();
        Intent intent=new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        intent.putExtra(MediaStore.EXTRA_OUTPUT,
        MediaStore.Images.Media.EXTERNAL_CONTENT_URI.getPath());
        startActivityForResult(intent, 1);
    }
});
TheWanderer
  • 16,775
  • 6
  • 49
  • 63
Guru
  • 186
  • 14
0
#initialize in main activity 
    path = Environment.getExternalStorageDirectory()
            + "/images/make_machine_example.jpg"; #
    ImageView image=(ImageView)findViewById(R.id.image);
 //--------------------------------------------------||

 public void FromCamera(View) {

    Log.i("camera", "startCameraActivity()");
    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);
    startActivityForResult(intent, 1);

}

public void FromCard() {
    Intent i = new Intent(Intent.ACTION_PICK,
            android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
    startActivityForResult(i, 2);
}

 protected void onActivityResult(int requestCode, int resultCode, Intent data) {

    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == 2 && resultCode == RESULT_OK
            && null != data) {

        Uri selectedImage = data.getData();
        String[] filePathColumn = { MediaStore.Images.Media.DATA };

        Cursor cursor = getContentResolver().query(selectedImage,
                filePathColumn, null, null, null);
        cursor.moveToFirst();

        int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
        String picturePath = cursor.getString(columnIndex);
        cursor.close();

        bitmap = BitmapFactory.decodeFile(picturePath);
        image.setImageBitmap(bitmap);

        if (bitmap != null) {
            ImageView rotate = (ImageView) findViewById(R.id.rotate);

        }

    } else {

        Log.i("SonaSys", "resultCode: " + resultCode);
        switch (resultCode) {
        case 0:
            Log.i("SonaSys", "User cancelled");
            break;
        case -1:
            onPhotoTaken();
            break;

        }

    }

}

protected void onPhotoTaken() {
    // Log message
    Log.i("SonaSys", "onPhotoTaken");
    taken = true;
    imgCapFlag = true;
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inSampleSize = 4;
    bitmap = BitmapFactory.decodeFile(path, options);
    image.setImageBitmap(bitmap);


}
0

First you need to filter the system application and then you can check for the camera activity, i just answer the similar question here.

Community
  • 1
  • 1
PiyushMishra
  • 5,743
  • 6
  • 37
  • 57