Android:无法启动活动/无法连接到相机服务
我想构建一个应用程序,可以打开和关闭设备的相机闪光灯。在代码中,它没有显示任何错误,但在我的设备上启动应用程序时,它开始崩溃。 报告错误显示一些Unable to start activity
和类似Fail to connect Camera Services
。 因为我是android开发新手,并没有足够的知识。 我已经看到了几个有关这个问题,但无法找到一些有用的信息。所有的答案都大致建议在我已经做过的清单文件中添加使用权限。Android:无法启动活动/无法连接到相机服务
内容清单文件:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.joshiyogesh.flashlight">
<uses-permission android:name="android.permission.CAMERA"/>
<uses-feature android:name="android.hardware.camera"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
主要活动的Java的内容:
package com.example.joshiyogesh.flashlight;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.hardware.Camera;
import android.hardware.Camera.Parameters;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
Button btn;
android.hardware.Camera camera;
Camera.Parameters parameters;
boolean isFlash = false;
boolean isOn = false;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = (Button)findViewById(R.id.button2);
if(getApplicationContext().getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH))
{
camera = Camera.open();
parameters = camera.getParameters();
isFlash = true;
}
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(isFlash){
if(!isOn){
btn.setText("Off");
btn.setBackgroundColor(Color.GREEN);
parameters.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
camera.setParameters(parameters);
camera.startPreview();
isOn = true;
}
else{
btn.setText("ON");
btn.setBackgroundColor(Color.RED);
parameters.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
camera.setParameters(parameters);
camera.stopPreview();
isOn = false;
}
}
else{
Toast.makeText(MainActivity.this,"Camera Not detecting",Toast.LENGTH_LONG).show();
}
}
});
}
@Override
protected void onStop() {
super.onStop();
if(camera!=null){
camera.release();
camera = null;
}
}
}
我张贴的代码打开摄像头,请根据您的需要进行更改。
试试这个:
static Camera camera = null;
,并声明如下:
try{
if(clickOn == true) {
clickOn = false;
camera = Camera.open();
Parameters parameters = camera.getParameters();
parameters.setFlashMode(Parameters.FLASH_MODE_TORCH);
camera.setParameters(parameters);
camera.startPreview();
remoteViews.setViewVisibility(R.id.button1, View.GONE);
remoteViews.setViewVisibility(R.id.button2, View.VISIBLE);
localAppWidgetManager.updateAppWidget(componentName, remoteViews);
} else {
clickOn = true;
camera.stopPreview();
camera.release();
camera = null;
remoteViews.setViewVisibility(R.id.button1, View.VISIBLE);
remoteViews.setViewVisibility(R.id.button2, View.GONE);
localAppWidgetManager.updateAppWidget(componentName, remoteViews);
}
} catch(Exception e) {
Log.e("Error", ""+e);
}
使用相机后,不要忘记下面的语句来释放它:
camera.release();
而且它可能是您的应用程序在运行时未获得打开相机的权限。因为从android 6.0开始,有必要拥有运行时权限来执行特定的任务。
所以,如果你使用的是Android 6.0(棉花糖)或以上,请检查权限按照此启用与否:
摄像机的权限可能会被禁用,应该从应用程序设置中启用。设置 - >应用程序 - > [您的应用程序] - >权限。
这就是为我工作。希望这有助于你太:)
编辑 并请使用e.printstacktrace()
在catch
块得到错误的logcat。
我觉得释放相机时出现问题。由于您已在onStop
中编写release
method
,因此只有当应用程序关闭时,您的相机才会获得发行版。当您关闭按钮click method
中的闪光灯时,请尝试使用release
相机。
我做到了,但..无法解决 –
我得到了我错在哪里。上面写的代码是正确的。唯一的问题,我使用棉花糖,我们必须通过设备获得相机许可。
你需要发表一个答案作为图像? –
张贴您的主要文件 –
请确保您正确地编写了它http://*.com/a/26842715/5156075 –