java - 从 OpenGL 的 Assets 文件夹加载纹理

标签 java android opengl-es

我知道有很多与这个问题相关的问题,但似乎很少有关于 openGL 的。

我正在尝试将一些 PNG 文件从 assets 文件夹加载到位图中,但由于某种原因,返回的位图为 null,这又会在此处抛出 NullPointerException:

GLUtils.texImage2D(GLES20.GL_TEXTURE_2D, 0, bitmap, 0);

我用来从 Assets 文件夹加载图像的代码:

public static Bitmap getBitmapFromAsset(AssetManager mgr, String path)
{
    InputStream is = null;
    Bitmap bitmap = null;
    try {
        is = mgr.open(path);
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inScaled = false;
        bitmap = BitmapFactory.decodeStream(is, null, options);
    }
    catch (final IOException e)
    {
        bitmap = null;
        Log.e(TAG, "FAILED TO get getBitmapFromAsset: " + e.getMessage());
    }
    finally
    {
       if (is != null)
       {
            try 
            {
                 is.close();
            } 
            catch (IOException ignored) 
            {
            }
        }
    }

    return bitmap;
}

我已经尝试了几种不同的方法,例如没有 BitmapFactory.Options,但无论我得到 NullPointerException 是什么,所以我猜我应该做另一个过程。

附言我可以从 res/raw 文件夹加载它们,但我没有子目录来组织我的资源。

最佳答案

好的,我刚刚意识到我的 assets 文件夹出于某种原因在 res 文件夹下。我只是把它放在主文件夹 app/main/assets 下,它正在工作。 *脸红

关于java - 从 OpenGL 的 Assets 文件夹加载纹理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30307515/

相关文章:

java - 尽管我们可以继承它们,为什么我们转向访问父类(super class)成员?

android - 带有新行的 AlertDialog 消息。 (\n 不起作用。)

opengl-es - 移动设备上 WebGL 中的 OES_texture_float

android - 使用 OpenGL 的 2D 示例

java - 如何即时更新ListView中的列表项?

java - regex - Java 正则表达式匹配方括号

android - 如何恢复CountDownTimer?

android - 访问 xml 中的整数资源

iphone - 在 OpenGL ES 中,绘制一个大对象还是绘制多个小对象更快?

java - 使用 Criteria 根据时间段从数据库获取对象?