嘿我正在android studio中设计一个应用程序。我需要相机的许可。我在AndroidManifest.xml文件中包含了<html lang=="en">
<meta name="viewport" content="width=device-width, initial-scale=1">
<head>
<meta charset="utf-8">
<title>Deut65</title>
<link href="site.css" rel="stylesheet">
</head>
<body>
<ul id="nav">
<img id="logo" src="logo.png"></img>
<li><a href="#home">Home</a></li>
<li><a href="#editor">Editor</a></li>
<li><a href="#about">About</a></li>
</ul>
<div id="image-div">
<img id="top-image"src="fotoDeut65.jpeg">
<button id="image-button">Button</button>
</div>
<style>
#image-div{
top: -50%;
left: -50%;
width: 100%;
height: 100%;
}
#top-image{
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
margin: auto;
min-width: 100%;
min-height: 100%;
width: 400px;
height: auto;
}
#image-button{
position: relative;
z-index: 1;
margin-left: auto;
margin-right: auto;
top: 191px;
left: 420px;
right: -420px;
bottom: -191px;
}
</style>
<!--until here-->
</body>
</html>
。它在除Marshmallow之外的所有Android版本中都能正常工作。如何默认获得相机权限?如果不可能,我该如何向用户询问?
答案 0 :(得分:44)
首先检查用户是否已授予权限:
if (ContextCompat.checkSelfPermission(context, Manifest.permission.CAMERA)
== PackageManager.PERMISSION_DENIED)
然后,您可以使用它来向用户请求:
ActivityCompat.requestPermissions(activity, new String[] {Manifest.permission.CAMERA}, requestCode);
在Marshmallow中,它会出现在对话框中
答案 1 :(得分:30)
您可以尝试使用以下代码在marshmallow中请求相机权限。
首先检查用户是否授予权限。如果用户没有被授予 权限然后请求相机许可。
private static final int MY_CAMERA_REQUEST_CODE = 100;
if (checkSelfPermission(Manifest.permission.CAMERA)
!= PackageManager.PERMISSION_GRANTED) {
requestPermissions(new String[]{Manifest.permission.CAMERA},
MY_CAMERA_REQUEST_CODE);
}
请求权限后,将显示警告以询问权限 包含允许和拒绝选项。点击动作后我们可以得到 使用以下方法的请求结果。
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == MY_CAMERA_REQUEST_CODE) {
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Toast.makeText(this, "camera permission granted", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(this, "camera permission denied", Toast.LENGTH_LONG).show();
}
}}//end onRequestPermissionsResult
答案 2 :(得分:3)
RxPermission是将代码排成1行的最佳库。
要获得摄像头许可,解决方法是这样的。
1)首先在您的manifest.xml
中添加此权限。
<uses-permission android:name="android.permission.CAMERA" />
2)然后询问运行时权限。
new RxPermissions(this)
.request(Manifest.permission.CAMERA) // ask single or multiple permission once
.subscribe(granted -> {
if (granted) {
// All requested permissions are granted
} else {
// At least one permission is denied
}
});
将此库添加到您的build.gradle
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
dependencies {
implementation 'com.github.tbruyelle:rxpermissions:0.10.1'
implementation 'com.jakewharton.rxbinding2:rxbinding:2.1.1'
}
这不容易吗?
答案 3 :(得分:2)
check using this
if (ContextCompat.checkSelfPermission(this, android.Manifest.permission.CAMERA)
== PackageManager.PERMISSION_DENIED)
和
答案 4 :(得分:1)
这对我有用,来源是here
int MY_PERMISSIONS_REQUEST_CAMERA=0;
// Here, this is the current activity
if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED)
{
if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.CAMERA))
{
}
else
{
ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.CAMERA}, MY_PERMISSIONS_REQUEST_CAMERA );
// MY_PERMISSIONS_REQUEST_READ_CONTACTS is an
// app-defined int constant. The callback method gets the
// result of the request.
}
}
答案 5 :(得分:1)
click here获取完整的源代码,并了解更多。
在获得许可之前,您可以检查api版本,
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
{
if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
}
else
{
ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, 401);
}
}
else
{
// if version is below m then write code here,
}
获取权限对话框的结果,
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == 401) {
if (grantResults.length == 0 || grantResults == null) {
} else if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
openGallery();
} else if (grantResults[0] == PackageManager.PERMISSION_DENIED) {
}
} else if (requestCode == 402) {
if (grantResults.length == 0 || grantResults == null) {
} else if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
} else if (grantResults[0] == PackageManager.PERMISSION_DENIED) {
}
}
}
答案 6 :(得分:1)
我尝试添加以下代码:
private static final int MY_CAMERA_REQUEST_CODE = 100;
@RequiresApi(api = Build.VERSION_CODES.M)
if (checkSelfPermission(Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
requestPermissions(new String[]{Manifest.permission.CAMERA}, MY_CAMERA_REQUEST_CODE);
}
关于 onCreate 函数和以下代码:
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == MY_CAMERA_REQUEST_CODE) {
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Toast.makeText(this, "camera permission granted", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(this, "camera permission denied", Toast.LENGTH_LONG).show();
}
}
}
这对我有用:)
答案 7 :(得分:0)
请求权限 在以下代码中,我们将请求相机许可:
在Java中
EasyPermissions是一个包装器库,用于简化针对Android M或更高版本的基本系统权限逻辑。
安装 通过将以下依赖项添加到build.gradle文件中来安装EasyPermissions:
依赖性{
// For developers using AndroidX in their applications
implementation 'pub.devrel:easypermissions:3.0.0'
// For developers using the Android Support Library
implementation 'pub.devrel:easypermissions:2.0.1'
}
private void askAboutCamera(){
EasyPermissions.requestPermissions(
this,
"A partir deste ponto a permissão de câmera é necessária.",
CAMERA_REQUEST_CODE,
Manifest.permission.CAMERA );
}