android - 从 Activity 返回时没有当前的 openGL 上下文?

标签 android opengl-es

我正在编写代码,允许用户浏览其 SD 卡上的文件以查找图像并使用 openGL ES 2.0 加载它们。当我只使用 EditText 键入文件路径时,这工作正常,但现在我已经实现了一个文件浏览器,它使用文件路径的字符串进行完全相同的调用,我得到“在没有当前上下文的情况下调用 openGL API "在 LogCat 中。

我假设这与加载器 Activity 在 GLSurfaceView 之上有关,所以我设置该 Activity 在进行任何 openGL 调用之前终止,但没有骰子。

什么给了?

下面是一些代码 fragment :

当用户点击加载器中的文件时调用

    public void backOut(String filePath) {
        // inform the main Activity of the file to load...
        Intent i = new Intent();
        i.putExtra("filePath", filePath);

        setResult(Activity.RESULT_OK, i);
        // ... and end the load activity
        finish();
    }

在拥有 GLSurfaceView 的主 Activity 中

@Override 
public void onActivityResult(int requestCode, int resultCode, Intent data) {     
  super.onActivityResult(requestCode, resultCode, data); 
  switch(requestCode) { 
    case (1) : { 
      if (resultCode == Activity.RESULT_OK) { 
      String toLoad = data.getStringExtra("filePath");
      Log.v(TAG, toLoad);
      gl.informRendererLoadTexture(toLoad);
      } 
      break; 
    } 
  } 
}

在 GLSurfaceView 中

 public void informRendererLoadTexture(String filePath){
     _filePath = filePath;
     queueEvent(new Runnable(){
            public void run() {
                _renderer.loadGLTexture(_filePath);
            }});

最佳答案

来自 GLSurfaceView 文档:

“在某些情况下,EGL 渲染上下文会丢失。这通常发生在设备从休眠后唤醒时。当 EGL 上下文丢失时,与该上下文关联的所有 OpenGL 资源(例如纹理)将被自动删除。为了保持正确渲染,渲染器必须重新创建它仍然需要的任何丢失的资源。onSurfaceCreated(GL10, EGLConfig) 方法是执行此操作的便捷位置。”

您需要了解丢失的 OpenGL 上下文,然后重新获取上下文并重新加载所有 OpenGL 资源。当您显示全屏文件浏览器时,您的上下文似乎丢失了。

您可以查看 Replica Island 的检测和处理丢失上下文的示例代码:http://code.google.com/p/replicaisland/

关于android - 从 Activity 返回时没有当前的 openGL 上下文?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3579775/

相关文章:

c - Linux opengl es 2.0 来自 windows

java - 不同用户的单独 Activity

java - 如何将 textView 与 Fragments Android/Java 一起使用?

安卓屏幕录像

iphone - 对于具有不同上下文的多个线程,OpenGL 线程安全吗?

java - 为 ByteBuffer 分配创建一个辅助函数 android OpenGL ES

android - 如何修复慢速 Android 模拟器?

java - 使用 android fragment backstack 的最佳方法是什么?

android - 在 React Native Android 中使用 Auth0-lock 构建失败

c - 在 OpenGL ES 1.1 与 ES 2.0 中使用顶点缓冲区对象进行绘图