我已经尝试制作一个简单的手电筒应用程序,但它要么崩溃,要么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>
答案 0 :(得分:1)
由于您使用的是Android Marshmallow(6.0),因此您可以使用不需要相机权限的新Torch API,并使手电筒应用开发者的API更加明显和简单。
查看您的堆栈跟踪,似乎相机正在报告error code 2,即CAMERA_ERROR_EVICTED。 “由于优先级较高的用户使用,相机已断开连接。”
也许您的设备上还有另一个使用相机的应用。我首先看一下火炬API,然后看看其他应用正在使用相机。
答案 1 :(得分:0)
从您的代码中,我可以说您仍然使用旧的相机API。如果您获得了许可但闪存不起作用,可能是因为该设备无法使用旧的api。尝试使用camera 2 api。
对于使用旧相机api的简单工作应用,您可以向SimpleFlashLight学习。
对于相机2,请查看Camera2
- 已编辑 -
用于处理api&lt;之间的api范围。 21和api&gt; = 21,您可以通过检查设备构建版本来使用相机api(相机和相机2):
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
//Use camera2
} else {
//Use camera
}
更好的选择是创建自己的api,连接两个摄像头api。