android 中未显示权限(相机权限)且无错误

Permission not showing in android (Camera Permission ) With no Errors

我是学习 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 中没有错误,这让我感到困惑,谢谢

你的代码似乎对我有用。唯一不同的是我在AndroidManifest.xml文件

中添加了相机权限
<manifest ...>

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

</manifest>