javaandroidcwac-camera

Android CWAC-Camera issue on new CameraFragment().takePicture();


I am trying to use CWAC-Camera library for a project that require the addition of some features to a camera... add some buttons on the UI etc...

Actually I am testing whit that library... The problem is that I am able to see the preview and rotate the camera without issues. but... I am unable to take a picture whitout error....

My code so far..

 @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Button photo = (Button)findViewById(R.id.foto);


    photo.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            new CameraFragment().takePicture();
        }
    });

}

Just that on the Main_activity class..

Traceback...

                E/AndroidRuntime(20708): Process: pandicam.camerafromlibrary, PID: 20708
            E/AndroidRuntime(20708): java.lang.NullPointerException: Attempt to invoke virtual method 'void com.commonsware.cwac.camera.CameraView.takePicture(boolean, boolean)' on a null object reference
            E/AndroidRuntime(20708):        at com.commonsware.cwac.camera.CameraFragment.takePicture(CameraFragment.java:151)
            E/AndroidRuntime(20708):        at com.commonsware.cwac.camera.CameraFragment.takePicture(CameraFragment.java:137)
            E/AndroidRuntime(20708):        at pandicam.camerafromlibrary.MainActivity$1.onClick(MainActivity.java:30)
            E/AndroidRuntime(20708):        at android.view.View.performClick(View.java:4756)
            E/AndroidRuntime(20708):        at android.view.View$PerformClick.run(View.java:19748)
            E/AndroidRuntime(20708):        at android.os.Handler.handleCallback(Handler.java:739)
            E/AndroidRuntime(20708):        at android.os.Handler.dispatchMessage(Handler.java:95)
            E/AndroidRuntime(20708):        at android.os.Looper.loop(Looper.java:135)
            E/AndroidRuntime(20708):        at android.app.ActivityThread.main(ActivityThread.java:5256)
            E/AndroidRuntime(20708):        at java.lang.reflect.Method.invoke(Native Method)
            E/AndroidRuntime(20708):        at java.lang.reflect.Method.invoke(Method.java:372)
            E/AndroidRuntime(20708):        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:898)
            E/AndroidRuntime(20708):        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:693)
            E/AudioTrack(  518): AudioTrack::set : Exit
            W/ActivityManager(  518):   Force finishing activity pandicam.camerafromlibrary/.MainActivity

Library Documentation for simple use.

Step #1: Install the JARs or AARs as described above.

Step #2: Add a CameraFragment to your UI. You have two versions of CameraFragment to choose from:

com.commonsware.cwac.camera.CameraFragment for use with native API Level 11+ fragments

com.commonsware.cwac.camera.acl.CameraFragment for use with the Android Support package's backport of fragments and ActionBarSherlock, supporting API Level 9 and 10

(note: if you choose the latter, your project will also need to have the ActionBarSherlock library project)

The CameraFragment is responsible for rendering your preview, so you need to size and position it as desired.

Step #3: Call takePicture() on the CameraFragment when you want to take a picture, which will be stored in the default digital photos directory (e.g., DCIM) on external storage as Photo_yyyyMMdd_HHmmss.jpg, where yyyyMMdd_HHmmss is replaced by the current date and time. Note that takePicture() can throw an IllegalStateException if you call it before the preview is ready or if you call it while auto-focus is occurring.

I really apretiate any help on this. Regards


Solution

  • I have just rebuilded everything and make a new project whit the guide of the example app that the library has on github and now is working.. thanks all that have readed my question