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.
- 12-08 12:41:42.649: W/CameraSwitch(3004): open main camera
- 12-08 12:41:42.659: W/CameraSwitch(3004): no file - can't switch camera
- 12-08 12:41:43.239: D/AndroidRuntime(3004): Shutting down VM
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 :)