Surfaceview is not creating immidiately after asking for runtime permission
Asked Answered
J

1

7

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.

Jackknife answered 21/6, 2017 at 10:49 Comment(4)
I think you will have to re-initialize the SurfaceView somehow after the permission is granted.Quietus
I am initializing the surfaceview after the permission is grantedJackknife
show how you do itQuietus
Okay i am updating my questionJackknife
S
8

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.

Strife answered 12/8, 2017 at 13:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.