【发布时间】:2015-05-18 15:43:16
【问题描述】:
我知道有很多与这个问题有关的问题,但似乎很少有针对 openGL 的。
我正在尝试将资产文件夹中的一些 PNG 文件加载到位图中,但由于某种原因,返回的位图为 null,这又会在此处引发 NullPointerException:
GLUtils.texImage2D(GLES20.GL_TEXTURE_2D, 0, bitmap, 0);
我用来从资产文件夹加载图像的代码:
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 文件夹加载它们,但我不能有子目录来组织我的资产。
【问题讨论】:
-
哪一部分失败了?
AssetManager或decodeStream()上的open()方法?您应该能够通过在调试器中单步执行来判断。 -
我刚刚分离了调用,它肯定是 open() 调用失败了。字符串路径假定 Android 将从资产文件夹开始查找。