权限未在android中显示(相机权限)没有错误
Posted
技术标签:
【中文标题】权限未在android中显示(相机权限)没有错误【英文标题】:Permission not showing in android (Camera Permission ) With no Errors 【发布时间】:2021-05-03 10:41:38 【问题描述】:我是一名学习 android 开发的初学者,我开始学习权限,但它不起作用,也没有出现在模拟器中,当我转到应用程序的设置时,它显示没有为此应用程序请求任何权限任何人都可以请帮帮我
活动:
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.core.app.ActivityCompat;
import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.provider.Settings;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;
import com.google.android.material.snackbar.BaseTransientBottomBar;
import com.google.android.material.snackbar.Snackbar;
public class MainActivity extends AppCompatActivity
private static final String TAG = "MainActivity";
private Button btnOPenCamera;
private ImageView imgPhotoFromCamera;
private ConstraintLayout parent;
private static final int PERMISSION_REQUEST_CODE = 909;
private static final int OPEN_CAMERA_INTENT = 808;
private static final int OPEN_SETTINGS_INTENT = 707;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnOPenCamera = findViewById(R.id.btnOPenCamera);
imgPhotoFromCamera = findViewById(R.id.imgPhotoFromCamera);
parent = findViewById(R.id.parent);
btnOPenCamera.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
handlePermession();
);
private void handlePermession()
if (ActivityCompat.checkSelfPermission(this , Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED)
Log.d(TAG, "handlePermession: it is working");
openCamera();
else
if (ActivityCompat.shouldShowRequestPermissionRationale(this , Manifest.permission.CAMERA))
showSnackBar();
else
Log.d(TAG, "handlePermession: it is working in the request");
ActivityCompat.requestPermissions(this , new String[] Manifest.permission.CAMERA , PERMISSION_REQUEST_CODE);
private void openCamera()
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent , OPEN_CAMERA_INTENT);
private void showSnackBar()
Snackbar.make(parent , "This app need your permission to the camera" , BaseTransientBottomBar.LENGTH_INDEFINITE)
.setAction("Allow", new View.OnClickListener()
@Override
public void onClick(View v)
Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
intent.setData(Uri.parse("package:" + getPackageName()));
startActivityForResult(intent , OPEN_SETTINGS_INTENT );
).show();
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data)
super.onActivityResult(requestCode, resultCode, data);
switch(requestCode)
case OPEN_CAMERA_INTENT:
if (resultCode == PackageManager.PERMISSION_GRANTED && data != null)
Bundle bundle = data.getExtras();
if (null != bundle)
Bitmap bitmap =(Bitmap) bundle.get("data");
imgPhotoFromCamera.setImageBitmap(bitmap);
break;
case OPEN_SETTINGS_INTENT:
handlePermession();
break;
default:
break;
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults)
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch(requestCode)
case PERMISSION_REQUEST_CODE:
if(grantResults.length>0 && grantResults[0] == PackageManager.PERMISSION_GRANTED)
openCamera();
else
Toast.makeText(this, "Permission Denied", Toast.LENGTH_SHORT).show();
logcat 中没有错误,这让我很困惑,谢谢
【问题讨论】:
你在你的 androidMenifest.xml 文件中添加了摄像头权限吗? 【参考方案1】:您的代码似乎对我有用。唯一不同的是我在 AndroidManifest.xml 文件中添加了摄像头权限
<manifest ...>
<uses-permission android:name="android.permission.CAMERA" />
...
<application ...>
...
</application>
</manifest>
【讨论】:
以上是关于权限未在android中显示(相机权限)没有错误的主要内容,如果未能解决你的问题,请参考以下文章
onRequestPermissionsResult 未在 android 活动中回调