I'm trying to create a very simple app that simply shows what the camera is viewing on the screen. I created the app with an emulator(same specification as the phone I used) which shows the black and white squared background with the grey viewing box moving over it, so I'm pretty sure I've got all code and permissions correct.
When I try to run the App on my Wildfire Phone(Android Version: 2.2.1) It force closes before anything is shown and when I check the logs this error is shown.
I looked through some sites and it seems it might be a problem with the OS or the hardware. Has anyone found a work around?
I'm going to post my code just in case I've made a foolish mistake.
import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
public class InformationPassingTestActivity extends Activity {
Preview preview;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
preview = new Preview(this);
setContentView(preview);
}
}
my preview class that handles the camera
class Preview extends SurfaceView implements SurfaceHolder.Callback {
SurfaceHolder mHolder;
public Camera camera;
Preview(Context context) {
super(context);
mHolder = getHolder();
mHolder.addCallback(this);
mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}
public void surfaceCreated(SurfaceHolder holder) {
camera = Camera.open();
try {
camera.setPreviewDisplay(holder);
} catch (IOException e) {
e.printStackTrace();
}
}
public void surfaceDestroyed(SurfaceHolder holder) {
camera.stopPreview();
camera = null;
}
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
Camera.Parameters parameters = camera.getParameters();
parameters.setPreviewSize(width, height);
camera.setParameters(parameters);
camera.startPreview();
}
}
Manifest.xml
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />
<uses-sdk android:minSdkVersion="8" />
<application android:icon="@drawable/ic_launcher" android:label="@string/app_name" >
<activity android:label="@string/app_name" android:name=".InformationPassingTestActivity" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
Any help would be amazing :)
Your problem is probably from this line (or if it isn't yet it will be): parameters.setPreviewSize(width, height);
Actual cameras can't support every preview size. You need to choose a preview size that is close to the size of your surface and supported by the camera hardware. Here's a sample method to do that:
private Camera.Size getBestPreviewSize(int width, int height,
Camera.Parameters parameters) {
Camera.Size result=null;
for ( Camera.Size size : parameters.getSupportedPreviewSizes()) {
if (size.width<=width && size.height<=height) {
if (result==null) {
result=size;
}
else {
int resultArea=result.width*result.height;
int newArea=size.width*size.height;
if (newArea>resultArea) {
result=size;
}
}
}
}
return(result);
}
You may also have a problem calling Camera.open()
in surfaceCreated
without checking whether or not the camera has already been opened. Try moving that call to your onCreate
and onResume
methods and adding a call to Camera.release()
in your onPause
method so that you don't attempt to open the camera more than once.