2015-05-18 48 views
1

我知道有很多與這個問題有關的問題,但很少看起來像openGL。從OpenGL資產文件夾中加載紋理

我試圖加載從資產之間存在一定的PNG文件夾導入位圖,但由於某些原因返回的位圖是空這又拋出這裏一個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異常,所以我猜還有另一個我應該做的程序。

P.S.我可以從res/raw文件夾加載它們,但是我不能有子目錄來組織我的資產。

+0

哪部分失敗? 'AssetManager'上的'open()'方法,或'decodeStream()'?您應該能夠通過在調試器中逐句通知它。 –

+0

我只是把呼叫分開,而且肯定是失敗的open()調用。字符串路徑假定Android將從資產文件夾開始查找。 – Rebirth

回答

1

好的,我剛剛意識到我的資產文件夾出於某種原因在res文件夾下。我只是把它放在主文件夾的應用程序/主/資產,它的工作。 *臉紅

相關問題