我的手电筒应用程序的代码不起作用



我已经尝试制作一个简单的手电筒应用程序,但是它要么崩溃或LED永远不会打开。问题似乎是在要求获得权限,但是从设置中,我可以看到该应用程序已经具有它。对不起,编码非常困难。这是代码

import android.Manifest;
import android.content.DialogInterface;
import android.content.pm.PackageManager;
import android.hardware.Camera;
import android.hardware.Camera.Parameters;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.widget.Switch;
public class MainActivity extends AppCompatActivity {
private boolean hasFlash;
private Camera camera;
Camera.Parameters params;
private boolean isFlashOn;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

// SPERIMENTAL PERSMISSION ASKING COPIED FROM DEVELOPERS.ANDROID.COM
//(How the does it work)
    // Here, thisActivity is the current activity
    if (ContextCompat.checkSelfPermission(this,
            Manifest.permission.CAMERA)
            != PackageManager.PERMISSION_GRANTED) {
        // Should we show an explanation?
        if (ActivityCompat.shouldShowRequestPermissionRationale(this,
                Manifest.permission.CAMERA)) {
            // Show an expanation to the user *asynchronously* -- don't block
            // this thread waiting for the user's response! After the user
            // sees the explanation, try again to request the permission.
        } else {
            // No explanation needed, we can request the permission.
            ActivityCompat.requestPermissions(this,
                    new String[]{Manifest.permission.CAMERA},
                    500);
            // MY_PERMISSIONS_REQUEST_READ_CONTACTS is an
            // app-defined int constant. The callback method gets the
            // result of the request.
        }
    }



    //set max brightness
    WindowManager.LayoutParams layout = getWindow().getAttributes();
    layout.screenBrightness = 1;
    getWindow().setAttributes(layout);

}
private void getCamera() {
    if (camera == null) {
        try {
            camera = Camera.open();
            params = camera.getParameters();
        } catch (RuntimeException e) {
            Log.e("Failed to Open. Error: ", e.getMessage());
        }
    }

}
public void onFlash() {
    if (!isFlashOn) {
        if (camera == null || params == null) {
            return;
        }
    }
    camera = Camera.open();
    camera.startPreview();
    params = camera.getParameters();
    params.setFlashMode(Parameters.FLASH_MODE_TORCH);
    camera.setParameters(params);
    isFlashOn = true;
}
public void offFlash() {
    if (!isFlashOn) {
        if (camera == null || params == null) {
            return;
        }
    }
    camera = Camera.open();
    params = camera.getParameters();
    params.setFlashMode(Parameters.FLASH_MODE_OFF);
    camera.setParameters(params);
    camera.stopPreview();
    isFlashOn = false;
}

public void toggleFlash() {
    if (isFlashOn) {
        offFlash();
    } else {
        onFlash();
    }
}


public void checkFlash(View v){
    hasFlash = getApplicationContext().getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH);
    if(!hasFlash){
        //flash is not available
        //show a simple alert dialog
        AlertDialog alertDialog = new AlertDialog.Builder(MainActivity.this).create();
        alertDialog.setTitle("Error");
        alertDialog.setMessage("Couldn't connect to flash.");
        alertDialog.setButton(AlertDialog.BUTTON_NEUTRAL, "Ok",
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                });
        alertDialog.show();
        //toggle the switch to its original position
        Switch lightButton = (Switch) findViewById(R.id.lightButton);
        lightButton.toggle();
    }else{
        toggleFlash();
    }
}

@Override
protected void onPause() {
    super.onPause();
}
@Override
protected void onDestroy(){
    super.onDestroy();
}
@Override
protected void onRestart(){
    super.onRestart();
}
@Override
protected void onResume(){
    super.onResume();
    getCamera();
}
@Override
protected void onStop(){
    super.onStop();
    camera.release();
}
@Override
protected void onStart(){
    super.onStart();
}
}

stacktrace :(没什么重要的,当我击中主屏幕上的开关时,最后一行出现)

06-22 01:01:18.736 32422-32422/com.andsp.brightledflashlight I/art: Late-enabling -Xcheck:jni
06-22 01:01:18.760 32422-32428/com.andsp.brightledflashlight E/art: Failed sending reply to debugger: Broken pipe
06-22 01:01:18.760 32422-32428/com.andsp.brightledflashlight I/art: Debugger is no longer active
06-22 01:01:18.775 32422-32422/com.andsp.brightledflashlight I/ActivityThread: Switching default density from 480 to 400
06-22 01:01:18.781 32422-32422/com.andsp.brightledflashlight W/System: ClassLoader referenced unknown path: /data/app/com.andsp.brightledflashlight-2/lib/arm
06-22 01:01:19.653 32422-32422/com.andsp.brightledflashlight W/System: ClassLoader referenced unknown path: /data/app/com.andsp.brightledflashlight-2/lib/arm
06-22 01:01:19.965 32422-32422/com.andsp.brightledflashlight W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
06-22 01:01:20.187 32422-32583/com.andsp.brightledflashlight D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true
06-22 01:01:20.752 32422-32422/com.andsp.brightledflashlight I/Choreographer: Skipped 32 frames!  The application may be doing too much work on its main thread.
06-22 01:01:20.768 32422-32583/com.andsp.brightledflashlight I/Adreno-EGL: <qeglDrvAPI_eglInitialize:379>: QUALCOMM Build: 10/21/15, 369a2ea, I96aee987eb
06-22 01:01:20.769 32422-32583/com.andsp.brightledflashlight I/OpenGLRenderer: Initialized EGL, version 1.4
**06-22 01:02:01.634 32422-32422/com.andsp.brightledflashlight E/Camera: Error 2
06-22 01:02:04.092 32422-32422/com.andsp.brightledflashlight E/Camera: Error 2
06-22 01:02:05.242 32422-32422/com.andsp.brightledflashlight E/Camera: Error 2**

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"
    tools:context="com.andsp.brightledflashlight.MainActivity">

    <Switch
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/lightButton"
        android:layout_gravity="center"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true"
        android:onClick="checkFlash"/>

</RelativeLayout>

由于您使用的是Android Marshmallow(6.0),因此您可以使用不需要相机权限的新火炬API,并且使API更明显,更简单,对于手电筒应用程序开发人员。/p>

查看您的堆栈Trace,似乎相机正在报告错误代码2,该代码2是camera_error_eved。"由于较高的优先用户使用,相机被断开。"

也许您的设备上还有另一个使用相机的应用程序。我会先看一下Torch API,然后看看其他应用程序正在使用相机。

从您的代码中,我可以说您仍然使用旧相机API。如果您获得了权限但Flash不起作用,则可能是因为该设备无法与旧API一起使用。尝试使用摄像头2 API。

对于使用旧相机API的简单工作应用程序,您可以从SimpleFlashlight中学习。

相机2查看camera2

- 编辑 -

用于使用API和LT之间的API范围;21和API> = 21,您可以通过检查设备构建版本以下使用相机API(相机和相机2):

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
   //Use camera2
} else {
   //Use camera
}

更好的选择是制作自己的API,将两个相机API接口。

最新更新