简体   繁体   中英

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

I am a beginner learning android development and I started learning about permissions but it is not working nor appearing in the emulator and when I go to the settings of the apps it is showing that there are no permissions asked for this app can anyone help me, please

the Actvity:

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();
                }
        }
    }
}

and there are no errors in the logcat which is something confusing for me, Thanks

Your code seems to work for me. The only difference is that I have added the camera permission to the AndroidManifest.xml file

<manifest ...>

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

</manifest>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM