如何在android中获得相机的权限。(特别是棉花糖)
Posted
技术标签:
【中文标题】如何在android中获得相机的权限。(特别是棉花糖)【英文标题】:How get permission for camera in android.(Specifically Marshmallow) 【发布时间】:2016-11-27 20:59:02 【问题描述】:嘿,我正在 android studio 中设计一个应用程序。在其中我需要相机的许可。我在 AndroidManifest.xml 文件中包含了<uses-permission android:name="android.permission.CAMERA" />
。它在除 Marshmallow 之外的所有版本的 android 中都能正常工作。我如何默认获得相机权限?如果不可能,我该如何向用户询问?
【问题讨论】:
参考此链接:github.com/sagarjogadia28/PermissionSample/blob/master/app/src/… 试试这个可能有用***.com/a/41221852/5488468 【参考方案1】:首先检查用户是否已授予权限:
if (ContextCompat.checkSelfPermission(context, Manifest.permission.CAMERA)
== PackageManager.PERMISSION_DENIED)
然后,您可以使用它向用户请求:
ActivityCompat.requestPermissions(activity, new String[] Manifest.permission.CAMERA, requestCode);
而在 Marshmallow 中,它会出现在一个对话框中
【讨论】:
非常感谢... :-) 它现在要求许可。 不客气。请,如果答案对您有帮助,请检查作为解决方案;) 在Manifest.permission.CAMERA
我收到CAMERA
的红色文本
使用android.Manifest.permission.CAMERA【参考方案2】:
您可以尝试以下代码在棉花糖中请求相机权限。首先检查用户是否授予权限。如果用户没有授予权限,则请求摄像头权限:
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);
请求权限后,将显示对话框询问权限,其中包含allow
和deny
选项。点击action后,我们可以通过以下方法获取请求的结果:
@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();
【讨论】:
【参考方案3】:这对我有用,来源是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.
【讨论】:
【参考方案4】:check using this
if (ContextCompat.checkSelfPermission(this, android.Manifest.permission.CAMERA)
== PackageManager.PERMISSION_DENIED)
和
【讨论】:
【参考方案5】:请求权限 在下面的代码中,我们会请求摄像头权限:
在java中
EasyPermissions 是一个封装库,用于在面向 Android M 或更高版本时简化基本系统权限逻辑。
安装 EasyPermissions 通过将以下依赖项添加到您的 build.gradle 文件来安装:
dependencies
// 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 );
【讨论】:
【参考方案6】: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)
【讨论】:
【参考方案7】:我尝试添加以下代码:
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();
这对我有用:)
【讨论】:
【参考方案8】: private const val CAMERA_PERMISSION_REQUEST_CODE = 2323
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
if (checkSelfPermission(Manifest.permission.CAMERA) !=
PackageManager.PERMISSION_GRANTED)
requestPermissions(arrayOf(Manifest.permission.CAMERA),
CAMERA_PERMISSION_REQUEST_CODE)
【讨论】:
以上是关于如何在android中获得相机的权限。(特别是棉花糖)的主要内容,如果未能解决你的问题,请参考以下文章
如何在Android中询问Camera的运行时权限,运行时存储权限