Reputation: 24012
There are many questions similar to this, but none helped me:
my manifest file is:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.android.cameraapi"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="10" />
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".CameraAPIActivity"
android:label="@string/app_name"
android:screenOrientation="landscape" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
my Activity class is:
public class CameraAPIActivity extends Activity {
private Camera myCamera = null;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
if (checkCameraHardware(this)) {
try {
myCamera = Camera.open();
} catch (Exception e) {
//Here i get the Exception: Failed to connect to camera service
}
}
}
private boolean checkCameraHardware(Context context) {
if (context.getPackageManager().hasSystemFeature(
PackageManager.FEATURE_CAMERA)) {
// this device has a camera
return true;
} else {
// no camera on this device
return false;
}
}
i get the Exception at the line
myCamera = Camera.open();
Thank You
Upvotes: 5
Views: 33668
Reputation: 1
You imported the wrong camera class at the top of your source file (I sink that), which is android.graphics.Camera
.
You need android.hardware.Camera
instead.
After:
myCamera = Camera.open(); - start working.
Upvotes: 0
Reputation: 6716
You will need to add the following
private Preview mPreview; // Global variable
mPreview = new Preview(this); // onCreate()
setContentView(mPreview); // onCreate()
Hope this helps!
Upvotes: 2
Reputation: 4493
If you want to take a pic from camera use
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_REQUEST);`
also add this function
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAMERA_REQUEST) {
photo = (Bitmap) data.getExtras().get("data");
imageView.setImageBitmap(photo);
}
}
imageView is the View where you may want to set that captured image.
Upvotes: -4
Reputation: 24012
Forgot to add
myCamera.release();
in my code. hence it works for the first time i launch the application. When i re-launch it the Camera service is not available.
Upvotes: 14