Ravindra Gupta
Ravindra Gupta

Reputation: 588

Camera.open() is not working android

I am trying to write a custom camera application in android. For that I need to open the camera application.

for that i am tring following code..

Camera camera = Camera.open();

but is showing error like

method open undefined for type Camera

i did as suggested here http://developer.android.com/reference/android/hardware/Camera.html#open(int)

any suggestion..

Thanks, Ravindra Gupta

Upvotes: 7

Views: 25023

Answers (8)

bufferum
bufferum

Reputation: 1

Waged right!!! A permission request is required. And I found the right code. he works. I advise you to look at this article to initially connect the camera: https://habr.com/ru/post/112272/

enter image description here

if(ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
        ActivityCompat.requestPermissions(MainActivity.this, new String[] {Manifest.permission.CAMERA}, 15); }

Upvotes: 0

Ramapati Maurya
Ramapati Maurya

Reputation: 664

I have faced a lot of issues while using integrating camera native/camera2 api. The code was bulky. To avoid complexity and compatibility issues google provide new CameraX api in new android jetpack library. See the google provided documentation https://developer.android.com/training/camerax. There is also a Kotlin based library i found on github https://github.com/robertlevonyan/CameraXDemo. You can get more clearity with less code.

Upvotes: 0

Waged
Waged

Reputation: 420

I have Faced the same problem till i reached that older versions of android will work properly until Android Marshmallow so it needs a runtime permission in order to proceed and show the camera ... you can read about it in this link https://developer.android.com/training/permissions/requesting.html

for me i used a 3rd party library to do all this stuff for me from this link and all resolved .. https://android-arsenal.com/details/1/2804 Hope it helps

Upvotes: 0

Pritam Kamble
Pritam Kamble

Reputation: 1

Please create a variable like this:

android.hardware.Camera camera ;

and then try open method :

camera = camera.open();

// this is working on my android studio

Upvotes: 0

AtomicStrongForce
AtomicStrongForce

Reputation: 721

If none of the above work: check to see if you are requesting camera permission manually. Newer Android permissions (API > 23) are set at runtime, not install time. See: https://developer.android.com/training/permissions/requesting.html

Upvotes: 1

Sanket990
Sanket990

Reputation: 665

I think you have not added the camera permission. See below - you need to add this in your manifest;

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

Upvotes: 3

Kanwaljit Singh
Kanwaljit Singh

Reputation: 4387

Check your imports. I had a similar problem and the Camera object Eclipse chose for me was: import android.graphics.Camera; instead it should be: import android.hardware.Camera;

Upvotes: 1

Md Abdul Gafur
Md Abdul Gafur

Reputation: 6201

You most likely imported the wrong camera class at the top of your source file, which is android.graphics.Camera.

You need android.hardware.Camera instead.

Thanks

Upvotes: 24

Related Questions