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>
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 fromcom.commonsware.cwac.camera.CameraFragment
, which itself inherits fromandroid.app.Fragment
. Yourcameramain
inherits fromFragmentActivity
, which does not work withandroid.app.Fragment
, but ratherandroid.support.v4.app.Fragment
. That does not work.The simplest solution is to have
cameramain
inherit fromActivity
, and use native API Level 11+ fragments.