手电筒应用程序异常:"无法连接到摄像头服务"

时间:2016-01-19 18:05:38

标签: android camera flashlight

我是Android应用程序开发的新手,我试图构建一个基本的Flashlight应用程序,但似乎大多数库已被弃用。我尝试过做Camera.Parameters camera;但仍然无法正常工作。日志显示异常:Caused by: java.lang.RuntimeException: Fail to connect to camera service

AndroidManifest.xml

<uses-permission android:name="android.permission.CAMERA"/>
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus"/>



MainActivity.java

package com.thenewboston.flashlight;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.pm.PackageManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
import android.hardware.Camera.Parameters;

public class MainActivity extends AppCompatActivity {

private android.hardware.Camera camera = null;
Parameters params;
Boolean isOn;
ImageButton imageButton;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    imageButton = (ImageButton) findViewById(R.id.imageButton);



    Boolean isCameraFlash = getApplication().getPackageManager()
            .hasSystemFeature(PackageManager.FEATURE_CAMERA);
    if(!isCameraFlash)
        showNoCameraAlert();
    else{
        camera = android.hardware.Camera.open();
        params = camera.getParameters();
    }


}

private void showNoCameraAlert() {
    new AlertDialog.Builder(this)
            .setTitle("Error")
            .setMessage("Sorry, your device doesn't support flash light!")
            .setNeutralButton("OK", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                   finish();
                }
            })
            .show();
}

public void myButton(View view){
    if (isOn){
        setFlashOff();
    }
    else {
        setFlashOn();
    }
}

private void setFlashOn() {
    params = camera.getParameters();
    params.setFlashMode(Parameters.FLASH_MODE_TORCH);
    camera.setParameters(params);
    camera.startPreview();
    isOn = true;
}

private void setFlashOff() {


    params.setFlashMode(Parameters.FLASH_MODE_OFF);
    camera.setParameters(params);
    camera.stopPreview();
    isOn = false;

}

@Override
protected void onStop() {
    super.onStop();
    if(camera != null){
        camera.release();
        camera = null;
    }
}
}



activity_main.xml中

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.thenewboston.flashlight.MainActivity"
android:background="#000000">


<ImageButton
    android:layout_width="150dp"
    android:layout_height="150dp"
    android:id="@+id/imageButton"
    android:layout_centerVertical="true"
    android:layout_centerHorizontal="true"
    android:src="@drawable/mytoggle"
    android:scaleType="fitCenter"
    android:background="@null"
    android:onClick="myButton" />
</RelativeLayout>

我知道,我在代码中使用了过时的内容,但这可能是因为我没有看到有人为Marshmallow构建手电筒应用。如果任何人都可以建议新的代码,它将是有用的或更正我的代码(如上所述)将是感恩的。

0 个答案:

没有答案