2

I am developing a camera app and I will use front and back camera. I am using TextureView like this:

<TextureView
        android:id="@+id/textureView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="center"
        />

and code behind:

renderer = new CameraRenderer(this, isFrontCamera);
textureView.setSurfaceTextureListener(renderer);

CameraRenderer class implemet SurfaceTextureListener

public class CameraRenderer implements Runnable, TextureView.SurfaceTextureListener{...

and I set camera in onSurfaceTextureAvailable method like this:

    public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
            if (renderThread != null && renderThread.isAlive()) {
                renderThread.interrupt();
            }
            renderThread = new Thread(this);

            surfaceTexture = surface;
            gwidth = width;
            gheight = height;

            // Open camera
            int cameraId;
            if(isFrontCamera)
                cameraId = getFrontCamera();
            else
                cameraId = getBackCamera();

            camera = Camera.open(cameraId);

renderThread.start();
    }

Problem:

When I want to change camera and click button I am creating a new CameraRenderer instance and set to textureView

textureView.setSurfaceTextureListener(renderer);

but onSurfaceTextureDestroyed method in CameraRenderer class never called, also onSurfaceTextureAvailable method is not called too.

I can call onSurfaceTextureDestroyed method myself but I can't set second camera preview to textureView because onSurfaceTextureAvailable method is not called second time.

Here is my onSurfaceTextureDestroyed method:

    @Override
public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
    if (camera != null) {
        camera.stopPreview();
        camera.release();
    }
    if (renderThread != null && renderThread.isAlive()) {
        renderThread.interrupt();
    }
    CameraFilter.release();

    return true;
}

Last note: I tried set listener as null but not working.

So How can I re-set textureView Listener. Thanks in advance!

Savas Adar
  • 4,083
  • 3
  • 46
  • 54
  • there is a working example [here](https://stackoverflow.com/questions/11539139/example-of-camera-preview-using-surfacetexture-in-android) – RonTLV Jul 27 '17 at 13:29
  • 1
    Thanks but my app already working, I am in trouble with changing camera. – Savas Adar Jul 27 '17 at 14:03

0 Answers0