Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Surfaceview is not creating immidiately after asking for runtime permission

I am stuck with this issue of Surfaceview and runtime perssion,

I have requirement of showing the camera preview using the Surfaceview at a main activity, So i am asking for the runtime permission when i call the code to show the camera preview.

But I am initializing the Surfaceview immediately after the permission is granted for camera, At that time the surfaceCreated() callback never calls and permission is also granted(I have checked from the Settings).

Once i kill the app and open again it works perfect after that.

Here is my initalising code,

in MainActivity,

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    initCameraPreview();
}

in initCameraPreview() method,

public void initCameraPreview(){
    StartCameraPreview preview = new StartCameraPreview(context, surfaceView);

    preview.onCreate();
}

I have created on class for SurfaceView, StartCameraPreview.java

public StartCameraPreview(Context context, SurfaceView surfaceView) {
    this.mContext = context;
    this.surfaceView = surfaceView;
}

public void onCreate() {
    if (checkCameraHardware()) {
        mCamera = getCameraInstance();

       // configure preview
        previewHolder = surfaceView.getHolder();
        previewHolder.addCallback(surfaceCallback);
        previewHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
    }
}

I cannot share much of the code, Sorry. So i have just added the initalization code. Did anyone face that issue or any one knows any solution for this?

Please help Thanks in adv.

like image 711
Hardik Chauhan Avatar asked Jun 21 '17 10:06

Hardik Chauhan


1 Answers

Try setting the visibility of the SurfaceView to INVISIBLE initially and set it to VISIBLE after the permission is granted . This way the drawing surface gets created after the permission result. Hope this helps.

like image 173
Prashanta Shrestha Avatar answered Nov 20 '22 01:11

Prashanta Shrestha