1

I searched for similar questions like this and followed the approaches in the answers but none of them worked for me. I know such questions have been asked previously. I am testing my app on Android 6.0 API 23. The permission dialog opens when i click on Allow the permission is granted but onRequestPermissionsResult is never called. None of the toasts appears on the screen whether i click on Allow or Deny. I know i am missing something but I can't figure it out.

public class MainActivity extends Activity {
private static final String TAG = "MainActivity";
Camera camera;
Preview preview;
ImageButton buttonClick;
ProgressBar progressBar;
public static Bitmap bitmap;
@Override
public void onCreate(Bundle savedInstanceState) {
    Log.e(TAG,"onCreate");
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setContentView(R.layout.activity_main);
    progressBar = (ProgressBar) findViewById(R.id.progressbar);
    buttonClick = (ImageButton) findViewById(R.id.imageButton);
    progressBar.setVisibility(View.GONE);

    int currentapiVersion = android.os.Build.VERSION.SDK_INT;
    //// if API >= 23 equest permisions
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && (ContextCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.CAMERA)
            != PackageManager.PERMISSION_GRANTED)) {
        ActivityCompat.requestPermissions(MainActivity.this,new String[]{"android.permission.CAMERA" }, 0);
    }else{
        moveOn();
    }

    buttonClick.setOnClickListener( new OnClickListener() {
        public void onClick(View v) {
            progressBar.setVisibility(View.VISIBLE);
            AudioManager mgr = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
            mgr.setStreamMute(AudioManager.STREAM_SYSTEM, true);
            preview.camera.takePicture(null, rawCallback, jpegCallback);
        }
    });
}

private void moveOn() {
    preview = new Preview(this);
    ((FrameLayout) findViewById(R.id.preview)).addView(preview);
}

@Override
protected void onPause() {
    super.onPause();
    finish();
}



PictureCallback rawCallback = new PictureCallback() {
    public void onPictureTaken(byte[] data, Camera camera) {
        Log.e(TAG,"onPictureTaken in rawCallback ");
    }
};

PictureCallback jpegCallback = new PictureCallback() {
    public void onPictureTaken(byte[] data, Camera camera) {
        bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
        Log.e(TAG,"onPictureTaken in jpegCallback ");
        camera.stopPreview();
        //finish();
        Intent k = new Intent(MainActivity.this, ImageSet.class);
        startActivity(k);
        progressBar.setVisibility(View.GONE);
    }
};

//This method will be called when the user will tap on allow or deny
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    //Checking the request code of our request
    if(requestCode == 0){

        //If permission is granted
        if(grantResults.length >0 && grantResults[0] == PackageManager.PERMISSION_GRANTED){

            //Displaying a toast
            Toast.makeText(this,"Permission granted to use Camera",Toast.LENGTH_LONG).show();
        }else{
            //Displaying another toast if permission is not granted
            Toast.makeText(this,"Oops you just denied the permission",Toast.LENGTH_LONG).show();
        }
    }
}
Floern
  • 33,559
  • 24
  • 104
  • 119
Yousaf Iqbal
  • 85
  • 2
  • 10

1 Answers1

3
@Override
protected void onPause() {
    super.onPause();
    finish();
}

When the request dialog shows, this method will callback because of your activity lost the focus, and you finish your activity. onRequestPermissionsResult will not be calling back.

I dont know why you finish your activity in this method, so the solution is delete the finish() sentance.

Fndroid
  • 465
  • 5
  • 9