java 摄像机请求权限出现错误

kxkpmulp  于 2023-03-21  发布在  Java
关注(0)|答案(4)|浏览(143)

我是新的Android和我试图建立一个手电筒应用程序.我试图请求使用相机的权限,但它带来无法解决CAMERA_REQUEST.任何解释和解决方案.找到下面的代码.

mButtonEnabled = findViewById(R.id.buttonEnable);
    mButtonEnabled.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            ActivityCompat.requestPermissions(MainActivity.this,
                    new String[] {Manifest.permission.CAMERA}, CAMERA_REQUEST);
        }
    });
}
mnemlml8

mnemlml81#

我假设你得到的错误是一个编译器错误,因为没有声明CAMERA_REQUEST。这只是一个数字,你定义为你正在发出的请求的id。理想情况下,你应该将其定义为示例变量。
此id将通过方法调用作为requestCode传递回给您,以帮助您识别响应的请求。它还将分别给予权限,因为您可以一次请求多个:

private final int REQUEST_CAMERA_ID = 123456;

public void setupUI() {

    mButtonEnabled = findViewById(R.id.buttonEnable);
    mButtonEnabled.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            ActivityCompat.requestPermissions(MainActivity.this,
                    new String[] {Manifest.permission.CAMERA}, REQUEST_CAMERA_ID);
        }
    });
}

public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {

    switch (requestCode) {
        case REQUEST_CAMERA_ID:
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                // Permission was granted
            } else {
                    // Permission was denied
            }
        break;
    }
}
aiqt4smr

aiqt4smr2#

您可以使用任何数字作为CAMERA_REQUEST。Android没有此属性,它只是您下一步处理响应的标识符。

ugmeyewa

ugmeyewa3#

下面,我写了代码授予相机的运行时权限,有一个数组字符串,你可以给予多个请求授予,因为这是在运行时需要.
公共类MainActivity扩展了AppCompatActivity {私有静态最终int许可请求代码= 200;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    if (checkPermission()) {
    //main logic or main code

   // . write your main code to execute, It will execute if the permission is already given.

    } else {
        requestPermission();
    }
}

private boolean checkPermission() {
    if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA)
            != PackageManager.PERMISSION_GRANTED) {
        // Permission is not granted
        return false;
    }
    return true;
}

private void requestPermission() {

    ActivityCompat.requestPermissions(this,
            new String[]{Manifest.permission.CAMERA},
            PERMISSION_REQUEST_CODE);
}

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

                // main logic
            } else {
                Toast.makeText(getApplicationContext(), "Permission Denied", Toast.LENGTH_SHORT).show();
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                    if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA)
                            != PackageManager.PERMISSION_GRANTED) {
                        showMessageOKCancel("You need to allow access permissions",
                                new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {
                                        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                                            requestPermission();
                                        }
                                    }
                                });
                    }
                }
            }
            break;
    }
}

private void showMessageOKCancel(String message, DialogInterface.OnClickListener okListener) {
    new AlertDialog.Builder(MainActivity.this)
            .setMessage(message)
            .setPositiveButton("OK", okListener)
            .setNegativeButton("Cancel", null)
            .create()
            .show();
}

}
希望对你有帮助

kzipqqlq

kzipqqlq4#

App/manifest/AndroidManifest.xml
在文件中编写以下代码

<uses-permission android:name="android.permission.CAMERA" />

在www.example.com中添加以下代码mainActivity.java

import android.Manifest;

希望对你有帮助

相关问题