gpt4 book ai didi

android - 将相机与安卓一起使用

转载 作者:行者123 更新时间:2023-11-29 01:07:41 25 4
gpt4 key购买 nike

我正在尝试通过我的应用程序使用相机,但是当我按下按钮时应用程序崩溃并且在 Android 监视器中出现以下消息:

java.lang.SecurityException: Permission Denial: starting Intent { 
act=android.media.action.IMAGE_CAPTURE
cmp=com.android.camera2/com.android.camera.CaptureActivity } from
ProcessRecord{bd3e6b7 4753:com.demonsystem.trackingticket/u0a94} (pid=4753,
uid=10094) with revoked permission android.permission.CAMERA

这是我主要 Activity 中的代码:

btnCamara.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent i = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(i,0); <-- here shows the error
ocultar();
}
});

这些是权限:

private void checkCameraPermission() {
int permissionCheck = ContextCompat.checkSelfPermission(
this, Manifest.permission.CAMERA);
if (permissionCheck != PackageManager.PERMISSION_GRANTED) {
Log.i("Mensaje", "No se tiene permiso para la camara!.");
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA}, 225);
} else {
Log.i("Mensaje", "Tienes permiso para usar la camara.");
}
}

这些是我在 list 中写的用途:

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

enter image description here

最佳答案

btnCamara.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick (View view){
if (hasPermissionUseCamera()) {
Intent i = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(i, 0);
ocultar();
}
}
}

public boolean hasPermissionUseCamera() {
if (Build.VERSION.SDK_INT >= 23) {
if (ActivityCompat.checkSelfPermission(getContext(), Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
return true;
} else {
requestPermissions(new String[]{Manifest.permission.CAMERA}, 0);
return false;
}
} else { //permission is automatically granted on sdk<23 upon installation
return true;
}
}

@RequiresApi(api = Build.VERSION_CODES.M)
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {

switch (requestCode) {
case 0: {
if (getActivity().checkSelfPermission(Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED) {
Intent i = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(i, 0);
ocultar();
} else {
Toast.makeText(getActivity(), "Access Denied", Toast.LENGTH_SHORT).show();
}
break;
}
}
}

关于android - 将相机与安卓一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45749641/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com