Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I'm having trouble with the "Drawing Images" section on the libgdx tutorial. I set up the documents completely and I typed the code as follows:

public class Game implements ApplicationListener {
        public static final String LOG = Game.class.getSimpleName();
        private FPSLogger fpsLogger;
        private SpriteBatch batch;
        private Texture texture;
        private Sprite sprite;
        private TextureRegion region;

        //removed irrelevant code for this question...

        @Override
        public void render() {
                texture = new Texture(Gdx.files.internal("android.png"));
                region = new TextureRegion(texture, 20, 20, 50, 50);
                sprite = new Sprite(texture, 20, 20, 50, 50);
                sprite.setPosition(10, 10);
                sprite.setRotation(45);

                Gdx.gl.glClearColor(0f, 1f, 0f, 1f);
                Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
                batch.begin();
                batch.draw(texture,10,10);
                batch.draw(region,10,10);
                sprite.draw(batch);
                batch.end();

                // output the current FPS
                fpsLogger.log();
        }
}

I went through the tutorial on the website but when I run the code I get errors:

Exception in thread "LWJGL Application"
com.badlogic.gdx.utils.GdxRuntimeException: Couldn't load file: android.png at
com.badlogic.gdx.graphics.Pixmap.<init>(Pixmap.java:137)  at
com.badlogic.gdx.graphics.glutils.FileTextureData.prepare(FileTextureData.java:55) at
com.badlogic.gdx.graphics.Texture.load(Texture.java:175)  at
com.badlogic.gdx.graphics.Texture.create(Texture.java:159)    at
com.badlogic.gdx.graphics.Texture.<init>(Texture.java:133)    at
com.badlogic.gdx.graphics.Texture.<init>(Texture.java:122)    at
com.game.Game.render(Game.java:46)    at
com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop (LwjglApplication.java:163) at
com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:113)
Caused by: com.badlogic.gdx.utils.GdxRuntimeException: File not found: android.png (Internal)     at
com.badlogic.gdx.files.FileHandle.read(FileHandle.java:108)   at
com.badlogic.gdx.files.FileHandle.length(FileHandle.java:364)     at
com.badlogic.gdx.files.FileHandle.readBytes(FileHandle.java:156)  at
com.badlogic.gdx.graphics.Pixmap.<init>(Pixmap.java:134)  ... 8 more

I set the android.png in my assests folder in my android project linking it to the desktop one, I don't understand what I'm doing wrong. What is making these errors?

share|improve this question
    
You should clean up the code you post to just the relevant code. It's not too bad in this situation, since the program is pretty small. But it's a bad habit to get into. –  Byte56 Mar 26 '12 at 16:11
    
Yea, but how would I determine which is important and which isn't? When there is more complicated code wouldn't there be more to explain? –  Blue Mar 26 '12 at 16:43
    
If you don't know what the important part of the code is, you need to understand the code better. Check out the changes I made as an example of how you might cull your code. Like I said it's not too big of an issue this time. Just wanted to let you know. –  Byte56 Mar 26 '12 at 16:52

5 Answers 5

up vote 5 down vote accepted

The relevant part of that stack trace appears to be the message about not being able to find the android.png file. Since you assert the file does exist on-disk, the problem is likely an issue with the working directory of your application at the time you attempt to load the file and the file's location relative to that working directory.

I'm not entirely sure if this is true of Java applications, but the working directory is generally going to default to that of the application itself or the working directory of the shell that invoked the application (if launching it from a console). So, if your image is in an "assets" subfolder relative to the application, you'd have to attempt to load "assets/android.png" and not just "android.png".

Also note that because the image appears in an assets subfolder in your project structure, that does not necessarily mean it will be deployed with the final built application unless you've configured your IDE/toolchain (which you don't specify) to do so.

In a pinch, you may be able to use the getAbsoluteFile method of the File object to print the absolute path of the image at runtime to the console or in your debugger. That may help you diagnose where your program expects the image to be versus where it actually is.

share|improve this answer
    
This should be of some help: code.google.com/p/libgdx/wiki/FileModule –  Byte56 Mar 26 '12 at 17:08
    
I found what I need. –  David Dimalanta Jun 18 '13 at 5:01

The real problem to the code you posted above is these three lines:

texture = new Texture(Gdx.files.internal("android.png"));
region = new TextureRegion(texture, 20, 20, 50, 50);
sprite = new Sprite(texture, 20, 20, 50, 50);

You are instantiating these things in the render loop which is a really bad idea. @Override the create method and create them in there.

Here is a good reference: http://code.google.com/p/libgdx/wiki/ApplicationLifecycle

share|improve this answer
1  
Even though it's not an answer to the question at hand, +1 for pointing out the texture creation before that became his next question =) –  Patrick Hughes Mar 27 '12 at 0:20

I encountered the same problem. (I am using eclipse indigo on ubuntu 10.04). The libgdx project setup tool creates android and desktop projects The asset folder in desktop project is a link to android project assets folder When you add a new file to the assets folder ,it takes some time for eclipse to register that a new file is added,as such FileNotFoundError is thrown. Manualy refreshing the linked folder from eclipse resolved the problem for me.

Hope this helps others.

share|improve this answer
    
Thanks gt5050! And welcome to GDSE. –  Byte56 May 28 '12 at 15:22
    
This worked for me too. Really strange error. –  boolean Apr 22 '13 at 3:21

I had a similar problem and I decided to try taking assets/filename.png and shortening it to just filename.png and that fixed my loading issue. I had to manually refresh as well. Thanks for the tip David Dimalanta. :)

"Sometimes, you don't need to write "assets/android.png". "android.png" is fine but make sure that the name of the file and the file extention type (i.e. PNG, JPEG, TXT) are matched and correct." - That's the line that did it for me right there.

share|improve this answer

Sometimes, you don't need to write "assets/android.png". "android.png" is fine but make sure that the name of the file and the file extention type (i.e. PNG, JPEG, TXT) are matched and correct. Also, it is better to put it only at the create() method (show() method if you're implementing Screen) even the sprite and region if you're initializing it to prevent from getting a Nullpointer Exception error message.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.