Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8936521
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T10:13:54+00:00 2026-06-15T10:13:54+00:00

I need to capture video from camera and save it to memory card, I’m

  • 0

I need to capture video from camera and save it to memory card, I’m using google tutorial and I still gets an error when calling

mRecorder.setCamera(mCamera); // 4.b.1 step from tutorial

I have try to put it in try and see the exception

try {
    mRecorder.setCamera(mCamera);
} catch(Exception e) {
    Log.d("MyVideoRecord", "Error setting the camera: " + e.getMessage());
    System.out.println("CameraPreview::record() - setCamera() thrown an exception");
    System.out.println("Exception: "+e.getMessage());
    System.out.println("Exception: "+e.getLocalizedMessage());
    System.out.println("Exception: "+e.toString());
    e.printStackTrace();

    return false;
}

LogCat output

D/MyVideoRecord(2681): Error setting the camera: null
I/System.out(2681): CameraPreview::record() - setCamera() thrown an exception
I/System.out(2681): Exception: null
I/System.out(2681): Exception: null
I/System.out(2681): Exception: java.lang.NullPointerException
W/System.err(2681): java.lang.NullPointerException
W/System.err(2681):     at com.example.myvideorecord.CameraPreview.record(CameraPreview.java:101)
W/System.err(2681):     at com.example.myvideorecord.MainActivity.onOptionsItemSelected(MainActivity.java:104)
W/System.err(2681):     at android.app.Activity.onMenuItemSelected(Activity.java:2502)
W/System.err(2681):     at com.android.internal.policy.impl.PhoneWindow.onMenuItemSelected(PhoneWindow.java:969)
W/System.err(2681):     at com.android.internal.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:735)
W/System.err(2681):     at com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:149)
W/System.err(2681):     at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:874)
W/System.err(2681):     at com.android.internal.view.menu.IconMenuView.invokeItem(IconMenuView.java:468)
W/System.err(2681):     at com.android.internal.view.menu.IconMenuItemView.performClick(IconMenuItemView.java:126)
W/System.err(2681):     at android.view.View$PerformClick.run(View.java:14263)
W/System.err(2681):     at android.os.Handler.handleCallback(Handler.java:605)
W/System.err(2681):     at android.os.Handler.dispatchMessage(Handler.java:92)
W/System.err(2681):     at android.os.Looper.loop(Looper.java:137)
W/System.err(2681):     at android.app.ActivityThread.main(ActivityThread.java:4441)
W/System.err(2681):     at java.lang.reflect.Method.invokeNative(Native Method)
W/System.err(2681):     at java.lang.reflect.Method.invoke(Method.java:511)
W/System.err(2681):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
W/System.err(2681):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
W/System.err(2681):     at dalvik.system.NativeStart.main(Native Method)
D/dalvikvm(2681): Debugger has detached; object registry had 425 entries

It looks like mCamera pointer is null from the log, but I dont think thats the case as I have unlocked mCamera right before without error.
I have looked in documentation but there isn’t any word about setCamera() returning an error or throwing an exception. I have set CAMERA, WRITE_EXTERNAL_STORAGE and RECORD_AUDIO permissions in manifest. I don’t know what may be causing the problem or how to trace it. Could anyone please help me?

Thank you very much ! 🙂


Here is all my code:

CameraPreview.java

I have implemented record() (that’s wher error occurs) and stopRecord() methods. Rest of file is from google tutorial.

package com.example.myvideorecord;

import java.io.File;
import java.io.IOException;

import android.content.Context;
import android.hardware.Camera;
import android.media.CamcorderProfile;
import android.media.MediaRecorder;
import android.net.Uri;
import android.os.Environment;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

//http://developer.android.com/guide/topics/media/camera.html#capture-video
public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback {
        private SurfaceHolder mHolder;
        private Camera mCamera;
        private boolean recording;
        private MediaRecorder mRecorder;

        public CameraPreview(Context context, Camera camera) {
            super(context);
            System.out.println("CameraPreview() - Started");
            mCamera = camera;
            recording = false;

            // 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);

            System.out.println("CameraPreview() - Ended");
        }

        public void surfaceCreated(SurfaceHolder holder) {
            System.out.println("CameraPreview::surfaceCreated() - Started");
            // 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("MyVideoRecord", "Error setting camera preview: " + e.getMessage());
            }

            System.out.println("CameraPreview::surfaceCreated() - Ended");
        }

        public void surfaceDestroyed(SurfaceHolder holder) {
            System.out.println("CameraPreview::surfaceDestroyed()");
            // empty. Take care of releasing the Camera preview in your activity.
        }

        public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
            System.out.println("CameraPreview::surfaceChanged() - Started");

            // 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("MyVideoRecord", "Error starting camera preview: " + e.getMessage());
            }

            System.out.println("CameraPreview::surfaceChanged() - Ended");
        }


        public boolean record()
        {
            // if already recording, return
            if( recording ) return false;

            // We are recording
            recording = true;
            // log start of the method
            System.out.println("CameraPreview::record() - Method start");

            //mCamera.stopPreview();
            mCamera.unlock();


            // 
            // !!! THIS IS WHERE ERROR OCCURS !!!
            //
            //
            try {
                mRecorder.setCamera(mCamera);
            } catch(Exception e) {
                Log.d("MyVideoRecord", "Error setting the camera: " + e.getMessage());
                System.out.println("CameraPreview::record() - setCamera() thrown an exception");
                System.out.println("Exception: "+e.getMessage());
                System.out.println("Exception: "+e.getLocalizedMessage());
                System.out.println("Exception: "+e.toString());
                e.printStackTrace();

                return false;
            }

            // Set media recorder properties            
            mRecorder.setAudioSamplingRate(MediaRecorder.AudioSource.CAMCORDER);
            mRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
            mRecorder.setProfile( CamcorderProfile.get( CamcorderProfile.QUALITY_LOW ) );
            //mRecorder.setOutputFile(getOutputMediaFile(MEDIA_TYPE_VIDEO).toString());
            mRecorder.setOutputFile("/sdcard/MVR_video");

            // probably not needed
            //mRecorder.setPreviewDisplay());

            // Prepare media recorder
            try {
                mRecorder.prepare();
            } catch (Exception e) {
                Log.d("MyVideoRecord", "Error preparing media recorder: " + e.getMessage());
                System.out.println("CameraPreview::record() - prepare() thrown an exception");

                return false;
            }

            mRecorder.start();

            System.out.println("CameraPreview::record() - Method returning TRUE");
            return true;
        }

        public void stopRecord() {
            System.out.println("CameraPreview::stopRecord() - Method start");

            if(recording)
            {
                mRecorder.stop();
                mRecorder.release();
                mCamera.lock();
            }
        }    
    }

MainActivity.java

package com.example.myvideorecord;

import android.app.Activity;
import android.content.SharedPreferences;
import android.hardware.Camera;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.FrameLayout;

// http://developer.android.com/reference/android/app/Activity.html
public class MainActivity extends Activity {
    private Camera mCamera;
    private CameraPreview mPreview;
    private Camera.Parameters parameters;
    private FrameLayout preview;
    private SharedPreferences preferences;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.camera_preview);               

        // Create an instance of Camera
        mCamera = getCameraInstance();

        // Get parameters of camera (not neccessary now)
        parameters = getCameraParameters(mCamera);
        parameters.getFocusMode();

        // get preview reference
        preview = (FrameLayout) findViewById(R.id.camera_preview);

        mPreview = new CameraPreview(this, mCamera);

        preferences = getPreferences(MODE_PRIVATE);
    }

    @Override
    public void onStart() {
        super.onStart();

        // Create our Preview view and set it as the content of our activity.        

        System.out.println("MainActivity::onCreate() - adding view to preview");
        preview.addView(mPreview);
        System.out.println("MainActivity::onCreate() - view added");
    }
    @Override
    public void onStop() {
        super.onStop();     
        System.out.println("MainActivity::onStop() - Method start");

        // here shoud go something like
        // preview.stop
        preview.removeView(mPreview);
        mPreview.stopRecord();
    }

    @Override
    public void onPause() {
        super.onPause();

        // Save all needed information
        //preferences.edit();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        System.out.println("MainActivity::onDestroy() - Method start");

        mCamera.stopPreview();
        releaseCameraInstance(mCamera);
    } 



    /*
     * 
     *  MENU 
     * 
     * */
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        System.out.println("MainActivity::onCreateOptionsMenu() - Method start");

        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        System.out.println("MainActivity::onOptionsItemSelected() - Method start");

        // Handle item selection
        switch (item.getItemId()) {
            case R.id.menu_settings:
                return true;
            case R.id.menu_quit:
                this.finish();
                return true;
            case R.id.menu_record:
                mPreview.record();
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }



    /*
     * 
     *  Camera handling
     * 
     * */
    /** 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)
            System.out.println("getCameraInstance() - excepction");
        }

        System.out.println("getCameraInstance() - returning camera");
        return c; // returns null if camera is unavailable
    }

    public static Camera.Parameters getCameraParameters(Camera c)
    {
        System.out.println("getCameraParameters() - getting parameters of camera");
        Camera.Parameters par;
        par = c.getParameters();

        System.out.println("getCameraParameters() - returning parameters of camera");
        return par;
    }

    /* release camera object and log it */
    public static void releaseCameraInstance(Camera c){
        System.out.println("releaseCameraInstance() - camera released");
        c.release();
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T10:13:57+00:00Added an answer on June 15, 2026 at 10:13 am

    Your mRecorder is null as you never initialize it. Initialize this in the constructor along with the camera and other things 😉

    mRecorder = new MediaRecorder();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using the AVFoundation classes to capture the live video stream from the camera
Basically I need to capture the video from videocamera do some processing on frames
I am trying to capture video from a camera. i have gotten the captureOutput:didOutputSampleBuffer:
I'm using JMF to capture video stream (webcam) on my Java project. The camera
I am using AVFoundation to capture CMSampleBufferRef from the camera and then convert it
I need to capture audio from a microphone using a flash object in a
I’m developing an application, which allows to record video from web camera. (using visual
I am guessing I need 2 things: A library to capture video from a
I wrote a C# code that captures video from a video capture card, and
I need to capture right mouse click event inside a iframe in asp.net using

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.