Home » Android » Camera.open() is not working android

Camera.open() is not working android

Posted by: admin June 15, 2020 Leave a comment

Questions:

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

How to&Answers:

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

Answer:

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" />

Answer:

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;

Answer:

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

Answer:

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

Answer:

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

Answer:

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.