Camera.open()为什么这一行代码会使我的android应用崩溃?

时间:2019-01-11 19:35:39

标签: java android camera

我不是经验丰富的程序员。我刚刚学习了一些基本的Java,并且正在考虑创建和android应用。实际上是一个火炬应用。因此,我遵循了youtube教程并编写了此代码,但无法在手机中打开。我有一个小米android oreo版本8手机。

有人可以回答我吗?预先感谢。

所以这行使我的应用崩溃 camera = Camera.open();

它甚至无法在我的手机中打开,只是显示白屏然后关闭该应用程序,但在Android Studio模拟器中可以正常工作。

我的代码。

SDK版本:

android {
compileSdkVersion 28

    minSdkVersion 16
    targetSdkVersion 28
    versionCode 1
    versionName "1.0"
    testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
}
}

清单xml文件;

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

JAVA代码:

public class MainActivity extends AppCompatActivity {

ImageButton imageButton;
Camera camera;
Camera.Parameters parameters;
boolean isFlash = false;
boolean isOn = false;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    imageButton = (ImageButton) findViewById(R.id.imageButton);

    if(getApplicationContext().getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH)){
        camera = Camera.open();
        parameters = camera.getParameters();
        isFlash = true;
    }

    imageButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if(isFlash == false){
                if(!isOn){
                    imageButton.setImageResource(R.drawable.on);
                    parameters.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
                    camera.setParameters(parameters);
                    camera.startPreview();
                    isOn = true;
                }else {
                    imageButton.setImageResource(R.drawable.off);
                    parameters.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
                    camera.setParameters(parameters);
                    camera.stopPreview();
                    isOn = false;
                }
            }else{
                AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
                builder.setTitle("Error...");
                builder.setMessage("flash is not available");
                builder.setPositiveButton("ok", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                        finish();
                    }
                });
                AlertDialog alertDialog = builder.create();
                alertDialog.show();
            }
        }
    });



    // end onCreate method
}

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

1 个答案:

答案 0 :(得分:1)

从您的targetSDK(28)和您的清单权限来看,我很直觉,您没有使用runtime permissions

请将此代码添加到您的代码中(基本上是调用摄像头的运行时权限),以代替摄像头打开逻辑:

if (ContextCompat.checkSelfPermission(getContext(), Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
    if (ActivityCompat.shouldShowRequestPermissionRationale((Activity)
            getContext(), Manifest.permission.CAMERA)) {
    } else {
        ActivityCompat.requestPermissions((Activity) getContext(), 
                new String[]{Manifest.permission.CAMERA},
                42);
   }
}

42只是我选择的一个随机数,应该是权限请求代码。

@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
    switch (requestCode) {
        case 42:
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                Toast.makeText(getApplicationContext(), "Permission Granted", Toast.LENGTH_SHORT).show();

                camera = Camera.open();
                parameters = camera.getParameters();
                isFlash = true;
            } else {
                // failure logic
                }
            }
            break;
    }
}

onRequestPermissionsResult是调用的函数,您应该在其中处理授予/拒绝权限的逻辑