Android:无法在 Camera.open() 处连接到相机服务;
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12705226/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android: Fail to connect to camera service at Camera.open();
提问by Archie.bpgc
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
谢谢你
回答by Archie.bpgc
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.
在我的代码中。因此它在我第一次启动应用程序时有效。当我重新启动它时,相机服务不可用。
回答by Royston Pinto
You will need to add the following
您将需要添加以下内容
private Preview mPreview; // Global variable
mPreview = new Preview(this); // onCreate()
setContentView(mPreview); // onCreate()
Hope this helps!
希望这可以帮助!
回答by Peter Miller
You imported the wrong camera class at the top of your source file (I sink that), which is android.graphics.Camera
.
您在源文件的顶部导入了错误的相机类(我认为是),即android.graphics.Camera
.
You need android.hardware.Camera
instead.
你需要android.hardware.Camera
代替。
After:
后:
myCamera = Camera.open(); - start working.
回答by Aamir Shah
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.
imageView 是您可能想要设置捕获图像的视图。