1

I know there are many questions relating to this question, but few appear to be for openGL.

I'm trying to load some PNG files from the assets folder into a Bitmap, but for some reason the returned Bitmap is null which in turn throws a NullPointerException here:

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

The code I am using to load the image from the assets folder:

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;
}

I've tried it a few different ways, e.g without the BitmapFactory.Options, but no matter what I am getting the NullPointerException, so I'm guessing there's another procedure I should be doing.

P.S. I can load them from the res/raw folder, but I can't have subdirectories to organise my assets.

2
  • Which part is failing? The open() method on the AssetManager, or the decodeStream()? You should be able to tell by stepping through it in a debugger. Commented May 18, 2015 at 15:50
  • I just separated the calls and it's definitely the open() call that's failing. String path assumes Android will start looking from the assets folder. Commented May 18, 2015 at 15:57

1 Answer 1

1

OK, I just realised that my assets folder was under the res folder for some reason. I just put it under the main folder app/main/assets and it's working. *blushes

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.