I am making a custom camera application. Which requires surface view and opens the camera inside the surface view.
But when running the same application on marshmallow I am using Runtime permission
to grant permission for camera. After I allow the dialog disappears but black screen shows up and camera doesn't get open.
I have tried also by calling the activity again after allowing but its not working.
THANX in advance.
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
if (preview) {
camera.stopFaceDetection();
camera.stopPreview();
preview = false;
}
if (camera != null) {
preview = true;
// Parameters
Parameters parameters = camera.getParameters();
parameters.setFocusMode(Parameters.FOCUS_MODE_CONTINUOUS_PICTURE);
List<Camera.Size> sizeList = camera.getParameters().getSupportedPreviewSizes();
bestSize = sizeList.get(0);
for (int i = 1; i < sizeList.size(); i++) {
if ((sizeList.get(i).width * sizeList.get(i).height) >
(bestSize.width * bestSize.height)) {
bestSize = sizeList.get(i);
}
}
parameters.setPictureSize(bestSize.width, bestSize.height);
camera.setParameters(parameters);
camera.startPreview();
camera.startFaceDetection();
camera.setFaceDetectionListener(faceDetectionListener);
}
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
customsurfaceview();
}
public void customsurfaceview() {
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
Log.e("PLAYGROUND", "Permission is not granted, requesting");
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA}, 123);
}
Log.e("Entered Surace Created", "----------------");
}
public void SetPreview_onSurface() {
if (camera == null)
return;
camera = Camera.open();
try {
camera.setPreviewDisplay(surfaceView.getHolder());
} catch (IOException e) {
e.printStackTrace();
Log.e("printStackTrace", "--" + e.getMessage());
}
camera_button = (ImageView) findViewById(R.id.camera_button);
camera_button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (capture_chek == 0) {
camera.cancelAutoFocus();
camera.takePicture(shutterCallback, null, pictureCallback);
capture_chek = 1;
}
}
});
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
if (camera != null) {
camera.release();
camera = null;
preview = false;
}
}
This question might be too old, but to help other people who'll encounter the same problem as I did. My fix was...
In OnCreate() I removed setContentView(R.layout...) then I will only call initializeUiAndCamera() in OnCreate() and in onRequestPermissionsResult() if the permissions are granted.
Inside initializeUiAndCamera() here I called setContentView(R.layout...) and inflated all the views I needed.
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
if (ContextCompat.checkSelfPermission(this,
Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED)
ActivityCompat.requestPermissions(this,
arrayOf(Manifest.permission.CAMERA), REQUEST_CAMERA)
else
initializeUiAndCamera()
}
private fun initializeUiAndCamera() {
setContentView(R.layout.activity_qr_scanner)
surfaceView = findViewById(R.id.surfaceView)
//...
}
override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<String>, grantResults: IntArray) {
when (requestCode) {
REQUEST_CAMERA -> {
if (grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED)
initializeUiAndCamera()
else
Toast.makeText(applicationContext, "Camera Permission denied", Toast.LENGTH_SHORT).show()
}
}
}
TLDR; Only inflate your views when the permissions are granted.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With