25285

Trying to switch camera back to front but getting exception

Trying to switch camera back to front but getting exception. cant find the problem Please Check and help..

error:- 01-27 11:49:00.376: E/AndroidRuntime(30767): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.vdrecord/com.vdrecord.AndroidVideoCapture}: java.lang.NullPointerException

<strong>My Code:-</strong>

public class AndroidVideoCapture extends Activity{ private static final String TAG = "CameraRecorderActivity"; private Camera myCamera; private MyCameraSurfaceView myCameraSurfaceView; private MediaRecorder mediaRecorder; private static String val="yes"; private Button useOtherCamera; Button myButton; SurfaceHolder surfaceHolder; boolean recording; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); recording = false; setContentView(R.layout.activity_android_video_capture); useOtherCamera = (Button) findViewById(R.id.useOtherCamera); useOtherCamera.setOnClickListener( new View.OnClickListener() { @Override public void onClick(View v) { //Get Camera for preview myCamera = getCameraInstance(); } }); FrameLayout myCameraPreview = (FrameLayout)findViewById(R.id.videoview); myCameraPreview.addView(myCameraSurfaceView); myButton = (Button)findViewById(R.id.mybutton); myButton.setOnClickListener(myButtonOnClickListener); myButton.setOnClickListener(myButtonOnClickListener); if(myCamera == null){ System.out.println("PRINT TRACK--7 "); Toast.makeText(AndroidVideoCapture.this, "Fail to get Camera", Toast.LENGTH_LONG).show(); } myCameraSurfaceView = new MyCameraSurfaceView(this, myCamera); } Button.OnClickListener myButtonOnClickListener = new Button.OnClickListener(){ @Override public void onClick(View v) { // TODO Auto-generated method stub if(recording){ // stop recording and release camera mediaRecorder.stop(); // stop the recording releaseMediaRecorder(); // release the MediaRecorder object //Exit after saved finish(); }else{ //Release Camera before MediaRecorder start releaseCamera(); if(!prepareMediaRecorder()){ Toast.makeText(AndroidVideoCapture.this, "Fail in prepareMediaRecorder()!\n - Ended -", Toast.LENGTH_LONG).show(); finish(); } mediaRecorder.start(); recording = true; myButton.setText("STOP"); } }}; public int getFrontCameraId() { CameraInfo ci = new CameraInfo(); for (int i = 0 ; i < Camera.getNumberOfCameras(); i++) { Camera.getCameraInfo(i, ci); if ((val=="yes") && (ci.facing == CameraInfo.CAMERA_FACING_FRONT)) { val="no"; return i; } } return CameraInfo.CAMERA_FACING_BACK; // No front-facing camera found } public Camera getCameraInstance(){ // TODO Auto-generated method stub Camera c = null; try { int index = getFrontCameraId(); if (index != -1) { c = Camera.open(index); // c = Camera.open(); // attempt to get a Camera instance }else{ c = Camera.open(index); } } catch (Exception e){ Log.d(TAG, "Error setting camera preview: " + e.getMessage()); // Camera is not available (in use or does not exist) } return c; // returns null if camera is unavailable } private boolean prepareMediaRecorder(){ myCamera = getCameraInstance(); mediaRecorder = new MediaRecorder(); myCamera.unlock(); mediaRecorder.setCamera(myCamera); mediaRecorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER); mediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA); mediaRecorder.setProfile(CamcorderProfile.get(CamcorderProfile.QUALITY_LOW)); mediaRecorder.setOutputFile("/sdcard/myvideo.mp4"); mediaRecorder.setMaxDuration(60000); // Set max duration 60 sec. mediaRecorder.setMaxFileSize(5000000); // Set max file size 5M mediaRecorder.setPreviewDisplay(myCameraSurfaceView.getHolder().getSurface()); try { mediaRecorder.prepare(); } catch (IllegalStateException e) { releaseMediaRecorder(); Log.d(TAG, "Error setting camera preview: " + e.getMessage()); return false; } catch (IOException e) { releaseMediaRecorder(); Log.d(TAG, "Error setting camera preview: " + e.getMessage()); return false; } return true; } @Override protected void onPause() { super.onPause(); releaseMediaRecorder(); // if you are using MediaRecorder, release it first releaseCamera(); // release the camera immediately on pause event } private void releaseMediaRecorder(){ if (mediaRecorder != null) { mediaRecorder.reset(); // clear recorder configuration mediaRecorder.release(); // release the recorder object mediaRecorder = null; myCamera.lock(); // lock camera for later use } } private void releaseCamera(){ if (myCamera != null){ myCamera.release(); // release the camera for other applications myCamera = null; } } public class MyCameraSurfaceView extends SurfaceView implements SurfaceHolder.Callback{ private SurfaceHolder mHolder; private Camera mCamera; public MyCameraSurfaceView(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); } @Override public void surfaceChanged(SurfaceHolder holder, int format, int weight, int height) { // 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){ Log.d(TAG, "Error setting camera preview: " + e.getMessage()); // ignore: tried to stop a non-existent preview } // make any resize, rotate or reformatting changes here // start preview with new settings try { mCamera.setPreviewDisplay(mHolder); mCamera.startPreview(); } catch (Exception e){ Log.d(TAG, "Error setting camera preview: " + e.getMessage()); } } @Override public void surfaceCreated(SurfaceHolder holder) { // TODO Auto-generated method stub // The Surface has been created, now tell the camera where to draw the preview. try { mCamera.setPreviewDisplay(holder); mCamera.startPreview(); } catch (IOException e) { Log.d(TAG, "Error setting camera preview: " + e.getMessage()); } } @Override public void surfaceDestroyed(SurfaceHolder holder) { // TODO Auto-generated method stub } } } Error Log:- 01-27 11:49:00.376: E/AndroidRuntime(30767): FATAL EXCEPTION: main 01-27 11:49:00.376: E/AndroidRuntime(30767): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.vdrecord/com.vdrecord.AndroidVideoCapture}: java.lang.NullPointerException 01-27 11:49:00.376: E/AndroidRuntime(30767): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1968) 01-27 11:49:00.376: E/AndroidRuntime(30767): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1993) 01-27 11:49:00.376: E/AndroidRuntime(30767): at android.app.ActivityThread.access$600(ActivityThread.java:127) 01-27 11:49:00.376: E/AndroidRuntime(30767): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1159) 01-27 11:49:00.376: E/AndroidRuntime(30767): at android.os.Handler.dispatchMessage(Handler.java:99) 01-27 11:49:00.376: E/AndroidRuntime(30767): at android.os.Looper.loop(Looper.java:137) 01-27 11:49:00.376: E/AndroidRuntime(30767): at android.app.ActivityThread.main(ActivityThread.java:4507) 01-27 11:49:00.376: E/AndroidRuntime(30767): at java.lang.reflect.Method.invokeNative(Native Method) 01-27 11:49:00.376: E/AndroidRuntime(30767): at java.lang.reflect.Method.invoke(Method.java:511) 01-27 11:49:00.376: E/AndroidRuntime(30767): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:980) 01-27 11:49:00.376: E/AndroidRuntime(30767): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:747) 01-27 11:49:00.376: E/AndroidRuntime(30767): at dalvik.system.NativeStart.main(Native Method) 01-27 11:49:00.376: E/AndroidRuntime(30767): Caused by: java.lang.NullPointerException 01-27 11:49:00.376: E/AndroidRuntime(30767): at android.view.ViewGroup.addView(ViewGroup.java:3158) 01-27 11:49:00.376: E/AndroidRuntime(30767): at android.view.ViewGroup.addView(ViewGroup.java:3145) 01-27 11:49:00.376: E/AndroidRuntime(30767): at com.vdrecord.AndroidVideoCapture.onCreate(AndroidVideoCapture.java:51) 01-27 11:49:00.376: E/AndroidRuntime(30767): at android.app.Activity.performCreate(Activity.java:4465) 01-27 11:49:00.376: E/AndroidRuntime(30767): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1052) 01-27 11:49:00.376: E/AndroidRuntime(30767): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1932)

Answer1:

Stop the camera previous instance before switching to other :

if (camera != null) { camera.stopPreview(); camera.setPreviewCallback(null); camera.release(); camera = null; surfaceholder.removeCallback(CameraActivity.this); surfaceholder = null; }

And finish current camera activity, call start intent of same camera activity.

Answer2:

First, destroy your surface, release the camera and recreate surface manually. Like this:

surfaceDestroy(holder); surfaceCreated(holder); surfaceChanged(holder,format,width,height);

It solved my problem, it may help you.

Recommend

  • Listview setOnItemClickListener - Not working for custom listview but working into simple listview
  • Java thread for JDBC
  • Recycler View Inside Recycler View not Scrolling
  • Conversion of VB6 HTTP request to VB.Net 2.0
  • how can I key rotate for google cloud storage service account?
  • Distribute Range of Numbers between each threads
  • Whats the right place for testhelper-classes? (phpunit/best practise)
  • Using an enum contained in a Cloud Endpoint model on a Android client
  • Is WITH the replacement for a #TEMP table?
  • Table striping rows in CSS Grid
  • RabbitMQ java client stops consuming messages
  • Fragment gives me an error while inflating 1
  • Select inner HTML item in CSS
  • Cannot find “Grammar.txt” in python-sphinx
  • Is there a way to set up a fallback for the formAction attribute in HTML5?
  • How to upload specific List image using click on Upload button
  • C# Excel interop - how to test if interop object is still working and performing a task?
  • Correct implementation of List Iterator methods
  • HttpClient: disabling chunked encoding
  • chrome video src change not working
  • How can I run DataNucleus Bytecode Enhancer from SBT?
  • Why does java tzupdater add leap seconds?
  • How can I tell a form not to dispose a particular control when it closes?
  • Hibernate to update table schema
  • SonarQube: Cannot deactivate rule with missing quality profile
  • Jackson Parser: ignore deserializing for type mismatch
  • Change multiple background-images with jQuery
  • How to use an array of arrays with array_map(…) in PHP?
  • The plugin 'org.apache.maven.plugins:maven-jboss-as-plugin' does not exist or no valid ver
  • Why is an OPTIONS request sent to the server?
  • Spring security and special characters
  • How to set/get protobuf's extension field in Go?
  • Cassandra Data Model
  • Trying to switch camera back to front but getting exception
  • Importing jscolor library in angular 2
  • using conditional logic : check if record exists; if it does, update it, if not, create it
  • Free memory of cv::Mat loaded using FileStorage API
  • Angular 2 constructor injection vs direct access
  • -fvisibility=hidden not passed by compiler for Debug builds
  • Programmatically clearing map cache