SurfaceTexture.updateTexImage() fails because of ivalid context
<p>I have an application, which captures video from camera.
I'm isung GLSurfaceView for showing preview and MediaCodec for encoding.
GlSurfaceView has it's own GLThread with EGLContext, and i'm creating another thread for MediaCodec and initialising another EGLContext in that thread. And it's working, i can record videos.</p>

<p>Problem comes when i'm trying to minimise and then restore the application. Call to SurfaceTexture.updateTexImage() causes IllegalStateException and logcat shows following message:</p>

<pre><code>02-19 18:19:58.400 8528-15845 E/GLConsumer:[unnamed-8528-0] checkAndUpdateEglState: invalid current EGLContext 
02-19 18:19:58.400 8528-15845 E/_GLViewRender: error updating text image 
02-19 18:19:58.400 8528-15845 E/_GLViewRender: java.lang.IllegalStateException: Unable to update texture contents (see logcat for details)
02-19 18:19:58.400 8528-15845 E/_GLViewRender:     at android.graphics.SurfaceTexture.nativeUpdateTexImage(Native Method)
02-19 18:19:58.400 8528-15845 E/_GLViewRender:     at android.graphics.SurfaceTexture.updateTexImage(SurfaceTexture.java:240)
02-19 18:19:58.400 8528-15845 E/_GLViewRender:     at ui.GLSurfaceViewRender.onDrawFrame(GLSurfaceViewRender.java:105)
02-19 18:19:58.400 8528-15845 E/_GLViewRender:     at android.opengl.GLSurfaceView$GLThread.guardedRun(GLSurfaceView.java:1583)
02-19 18:19:58.400 8528-15845 E/_GLViewRender:     at android.opengl.GLSurfaceView$GLThread.run(GLSurfaceView.java:1286)
</code></pre>

<p>What i'm doing when app minimises: </p>

<ol>
<li><p>Stopping camera preview:</p>

<pre><code>if (isPreviewOn &amp;&amp; mCamera != null) {
    isPreviewOn = false;
    mCamera.stopPreview();
}
</code></pre></li>
<li><p>Releasing camera:</p>

<pre><code>try {
    mCamera.setPreviewTexture(null);
} catch (IOException e) {
    e.printStackTrace();
}
mCamera.release();
mCamera = null;
</code></pre></li>
<li><p>Calling </p>

<pre><code>GLSurfaceView.onPause()
</code></pre></li>
</ol>

<p>What i'm doing when app restores: </p>

<ol>
<li><p>Calling </p>

<pre><code>GLSurfaceView.onPause()
</code></pre></li>
<li><p>Opening camera, setting preview surface texture, and starting preview</p>

<pre><code>if (mCamera == null) {
    mCamera = Camera.open(CAMERA_ID);
    params = mCamera.getParameters();
}

try {
    mCamera.setPreviewTexture(renderer.getSurfaceTexture());
} catch (IOException e) {
    Log.e(TAG, "error setting ST to preview", e);
    return;
}

if (!isPreviewOn &amp;&amp; mCamera != null) {
    isPreviewOn = true;
    mCamera.startPreview();
}
</code></pre></li>
</ol>

<p>And when updateTextImage is called next time, this error happens.</p>

<p>Any ideas why?</p>