0

I am new to android stuff and want to work with camera stuff so I am trying to hands on with CWAC camera library.I am using simple camerafragment which would show fullscreen camera preview.But camera preview is invisible.Can anyone suggest what should I do to rectify it.Here is my code:

FullCameraFragment.java

package com.mission.fourinc.cwac;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.commonsware.cwac.camera.CameraFragment;
import com.commonsware.cwac.camera.CameraView;
import com.commonsware.cwac.camera.SimpleCameraHost;

/**
 * Created by sanjaytalreja on 4/7/15.
 */
public class FullCameraFragment extends CameraFragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View cameraView=super.onCreateView(inflater, container, savedInstanceState);
        View rootView = inflater.inflate(R.layout.fragment_cameramain, container, false);
        ((ViewGroup)rootView.findViewById(R.id.camera)).addView(cameraView);
        return rootView;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setHost(new SimpleCameraHost(getActivity()));
    }
}

cameramain.java

    package com.mission.fourinc.cwac;

    import android.support.v4.app.FragmentActivity;
    import android.support.v7.app.ActionBarActivity;
    import android.support.v7.app.ActionBar;
    import android.support.v4.app.Fragment;
    import android.os.Bundle;
    import android.view.LayoutInflater;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.view.View;
    import android.view.ViewGroup;
    import android.os.Build;

    import com.commonsware.cwac.camera.CameraFragment;


    public class cameramain extends FragmentActivity {

        FullCameraFragment fcm=null;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_cameramain);
            fcm=(FullCameraFragment)getFragmentManager().findFragmentById(R.id.camera_preview);

            }
        }

fragment_cameramain.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"

    tools:context=".cameramain$FullCameraFragment">

    <FrameLayout
        android:id="@+id/camera"
        android:layout_width="match_parent"
        android:layout_height="0dip"
        android:layout_weight="1"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="New Button"
        android:id="@+id/button"
        android:layout_below="@+id/camera"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginTop="180dp" />

</RelativeLayout>

activity_cameramain.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:id="@+id/container"
    android:layout_width="match_parent" android:layout_height="match_parent"
    tools:context=".cameramain" tools:ignore="MergeRootFrame"
    >
<fragment
        android:id="@+id/camera_preview"
        android:name="com.mission.fourinc.cwac.FullCameraFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
    </FrameLayout>
Sanju Talreja
  • 39
  • 2
  • 7

1 Answers1

0

Among other possible problems, you are mixing native API Level 11 fragments with Android Support package backported fragments. That will not work.

Specifically, FullCameraFragment inherits from com.commonsware.cwac.camera.CameraFragment, which itself inherits from android.app.Fragment. Your cameramain inherits from FragmentActivity, which does not work with android.app.Fragment, but rather android.support.v4.app.Fragment. That does not work.

The simplest solution is to have cameramain inherit from Activity, and use native API Level 11+ fragments.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Thank you for the reply,as per your solution I changed that inheritance thing.Now I extends Activity rather than FragmentActivity but still camera preview is invisible! – Sanju Talreja Apr 08 '15 at 02:08
  • @SanjuTalreja: You have a zero-height preview. `android:layout_weight` does not work for children of `RelativeLayout`. – CommonsWare Apr 08 '15 at 10:38
  • Thank You.Its working fine now:)A big heart for your beautiful library:) – Sanju Talreja Apr 08 '15 at 14:08