my samsung a20 have triple camera. camera2 api not work there this will work for all the phone which have 2 camera on back or single camera.When I try to see picture or take picture with my app still in dev, the app crash with error, any solution please ?
java.lang.NullPointerException: Attempt to invoke virtual method 'int android.hardware.camera2.CameraCaptureSession.capture(android.hardware.camera2.CaptureRequest, android.hardware.camera2.CameraCaptureSession$CaptureCallback, android.os.Handler)' on a null object reference
--------- Stack trace ---------
com.app.appname.CameraActivity.lockFocus(CameraActivity.java:1749)com.app.appname.CameraActivity.onClick(CameraActivity.java:1191)android.view.View.performClick(View.java:7862)android.view.View.performClickInternal(View.java:7831)android.view.View.access$3600(View.java:879)android.view.View$PerformClick.run(View.java:29359)android.os.Handler.handleCallback(Handler.java:883)android.os.Handler.dispatchMessage(Handler.java:100)android.os.Looper.loop(Looper.java:237)android.app.ActivityThread.main(ActivityThread.java:8173)java.lang.reflect.Method.invoke(Native Method)com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:496)com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1100)
My gradle uses these settings :
android { buildToolsVersion '30.0.3' defaultConfig { applicationId 'com.app.appname' minSdkVersion 19 targetSdkVersion 30 versionCode 36 versionName '1.0' multiDexEnabled true } ... dependencies { classpath 'com.android.tools.build:gradle:4.1.0' classpath 'com.google.gms:google-services:4.3.4' } ...
The manifest has the following:
<uses-permission android:name="android.permission.CAMERA" /><uses-permission android:name="android.permission.FLASHLIGHT" />
on my CameraActivity :
import android.hardware.Camera;import android.hardware.Camera.PictureCallback;import android.hardware.Camera.ShutterCallback;import android.hardware.camera2.CameraAccessException;import android.hardware.camera2.CameraCaptureSession;import android.hardware.camera2.CameraCharacteristics;import android.hardware.camera2.CameraDevice;import android.hardware.camera2.CameraManager;import android.hardware.camera2.CameraMetadata;import android.hardware.camera2.CaptureRequest;import android.hardware.camera2.CaptureResult;import android.hardware.camera2.TotalCaptureResult;import android.hardware.camera2.params.StreamConfigurationMap;import android.media.ExifInterface;import android.media.Image;import android.media.ImageReader;...@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP) @SuppressLint("StringFormatInvalid") @Override public void onClick(View v) { switch (v.getId()) { case R.id.snap: cameraCapturedOrientation = detectOrientation; if (isCamera2Support) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { if (CameraCharacteristics.LENS_FACING_FRONT == mCameraLensFacingDirection) { captureStillPicture(); } else { lockFocus(); } } } else { try { camera.takePicture(shutterCallback, rawCallback, jpegCallback); } catch (NullPointerException e) { e.printStackTrace(); } catch (RuntimeException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } } break;... /** * Lock the focus as the first step for a still image capture. */ @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP) private void lockFocus() { try { // This is how to tell the camera to lock focus. mPreviewRequestBuilder.set(CaptureRequest.CONTROL_AF_TRIGGER, CameraMetadata.CONTROL_AF_TRIGGER_START); // Tell #mCaptureCallback to wait for the lock. mState = STATE_WAITING_LOCK; mCaptureSession.capture(mPreviewRequestBuilder.build(), mCaptureCallback, mBackgroundHandler); } catch (CameraAccessException e) { e.printStackTrace(); } } /** * Unlock the focus. This method should be called when still image capture sequence is * finished. */ @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP) private void unlockFocus() { try { // Reset the auto-focus trigger mPreviewRequestBuilder.set(CaptureRequest.CONTROL_AF_TRIGGER, CameraMetadata.CONTROL_AF_TRIGGER_CANCEL); mCaptureSession.capture(mPreviewRequestBuilder.build(), mCaptureCallback, mBackgroundHandler); // After this, the camera will go back to the normal state of preview. mState = STATE_PREVIEW; mCaptureSession.setRepeatingRequest(mPreviewRequest, mCaptureCallback, mBackgroundHandler); } catch (CameraAccessException e) { e.printStackTrace(); } }...