Surface View with a camera that does not work in Android API 23 (android 6+)

I want to create a background with a camera on SurfaceView, and I will be able to use the Android API below 23. Although he previously requested camera resolution in the API 23 application, the following error appeared on my Nexus 5 with Android 6:

java.lang.NullPointerException: attempt to call the void virtual method android.hardware.Camera.setPreviewDisplay (android.view.SurfaceHolder) 'on a link with a null object on com.package.name.CameraPreview.surfaceCreated (CameraPreview.java:31)

My code is:

import android.content.Context; import android.hardware.Camera; import android.util.Log; import android.view.SurfaceHolder; import android.view.SurfaceView; import java.io.IOException; /** A basic Camera preview class */ public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback { private SurfaceHolder mHolder; private Camera mCamera; public CameraPreview(Context context, Camera camera) { super(context); mCamera = camera;; // Install a SurfaceHolder.Callback so we get notified when the // underlying surface is created and destroyed. mHolder = getHolder(); mHolder.addCallback(this); // deprecated setting, but required on Android versions prior to 3.0 mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); } public void surfaceCreated(SurfaceHolder holder) { // The Surface has been created, now tell the activity_hologram where to draw the preview. try { mCamera.setPreviewDisplay(holder); mCamera.startPreview(); mCamera.setDisplayOrientation(90); } catch (IOException e) { Log.d("", "Error setting activity_hologram preview: " + e.getMessage()); } } public void surfaceDestroyed(SurfaceHolder holder) { // empty. Take care of releasing the Camera preview in your activity. } public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) { // If your preview can change or rotate, take care of those events here. // Make sure to stop the preview before resizing or reformatting it. if (mHolder.getSurface() == null){ // preview surface does not exist return; } // stop preview before making changes try { mCamera.stopPreview(); } catch (Exception e){ // ignore: tried to stop a non-existent preview } // set preview size and make any resize, rotate or // reformatting changes here // start preview with new settings try { mCamera.setPreviewDisplay(mHolder); mCamera.startPreview(); } catch (Exception e){ Log.d("", "Error starting activity_hologram preview: " + e.getMessage()); } } } 

The error goes to mCamera.setPreviewDisplay (holder);

My main activity code:

 public class MainActivity extends AppCompatActivity { private static Context mContext; private Camera mCamera; private CameraPreview mPreview; private FrameLayout layout; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mContext = getApplicationContext(); layout = (FrameLayout) findViewById(R.id.camera_preview); mCamera = getCameraInstance(); mPreview = new CameraPreview(this, mCamera); layout.addView(mPreview); } /** A safe way to get an instance of the Camera object. */ public static Camera getCameraInstance(){ Camera c = null; try { c = Camera.open(); // attempt to get a Camera instance } catch (Exception e){ // Camera is not available (in use or does not exist) } return c; // returns null if activity_hologram is unavailable } } 

My manifest received:

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

Any ideas to solve this problem on Android 6?

Thanks for your time, greetings.

+7
java android android-studio camera
source share
4 answers

I noted a problematic place in the comment:

 public static Camera getCameraInstance(){ Camera c = null; try { c = Camera.open(); // attempt to get a Camera instance } catch (Exception e){ // Camera is not available (in use or does not exist) // >>> HERE is your problem! Do not swallow exceptions silently! } return c; // returns null if activity_hologram is unavailable } 

It seems that getCameraInstance returns null. This may have two reasons:

  • Camera.Open() returns null
  • There is an exception that you will not be aware of, because you simply silently swallow it and continue.

Another point:

This class is deprecated at API level 21.
We recommend using the new android.hardware.camera2 API for new applications.

From Android Developer Site

+4
source share

Set up @Fildor's answer, which pointed me in the right direction. The camera object is NULL. In API 23 and above, go to application information → permissions and enable the camera resolution to successfully obtain the camera object.

+4
source share

Solve a problem Open Application Manager. Select the Application tab and go to Permissions and enable the required permission. The application will work fine after that.

To fix the problem in Runtime: - check the link
https://developer.android.com/training/permissions/requesting.html

+1
source share

This api still works on newer Android versions tested in version 6.0.1, only the problem is to add camera resolution and ask the user to provide it explicitly.

0
source share

All Articles