Abhishek Madhusudhan
Abhishek Madhusudhan

Reputation: 95

GdxRuntimeException: Error reading file

I am learning and programming a game for android using libgdx and i have got stuck on this error for quite a long period of time. I have enclosed the following content

  1. The code.
  2. The command i used to access the asset.
  3. Screenshot that contains the error when i debug the code on android.
  4. Screenshot of my Package Explorer.
  5. All the possible combinations i tried to get the code working.

1.The Code

package com.me.mygdxgame;

import com.badlogic.gdx.Gdx;  
import com.badlogic.gdx.assets.AssetErrorListener;  
import com.badlogic.gdx.assets.AssetManager;  
import com.badlogic.gdx.graphics.Texture;  
import com.badlogic.gdx.graphics.Texture.TextureFilter;    
import com.badlogic.gdx.graphics.g2d.TextureAtlas;  
import com.badlogic.gdx.utils.Disposable;

public class Gameassetloader implements Disposable, AssetErrorListener {

public static final String TAG = Gameassetloader.class.getName();
public static Gameassetloader instance = new Gameassetloader();
private AssetManager assetManager;

/** Load the appropriate texture for the respective entities **/
public AssetCharacter boy;
public AssetRock rock;
public AssetShield shield;

/** singleton: prevent instantiation from other classes **/

private Gameassetloader(){}
public void init(AssetManager assetManager)
 {
    this.assetManager = assetManager;

    //set asset manager error handler
    assetManager.setErrorListener(this);

    //load texture atlas
    assetManager.load(Gameconstants.TEXTURE_ATLAS_OBJECTS,TextureAtlas.class);

    //start loading assets and wait until finished
    assetManager.finishLoading();

    Gdx.app.debug(TAG, "# of assets loaded:" + assetManager.getAssetNames().size);

    for (String a : assetManager.getAssetNames())
        Gdx.app.debug(TAG, "asset:" + a);

    TextureAtlas atlas = assetManager.get(Gameconstants.TEXTURE_ATLAS_OBJECTS);

    //enable texture filtering for pixel smoothing
    for(Texture t : atlas.getTextures())
        t.setFilter(TextureFilter.Linear,TextureFilter.Linear);

    //create game resource objects

    boy = new AssetCharacter(atlas);
    rock = new AssetRock(atlas);
    shield = new AssetShield(atlas);
}

public void dispose() { assetManager.dispose(); }


 @Override
 public void error(String fileName, Class type, Throwable throwable) 
  {
    Gdx.app.error(TAG, "Couldn't load asset' " + fileName + "'", (Exception)throwable); 
  }

}  

2.The Command

 public static final String TEXTURE_ATLAS_OBJECTS ="gdxgame-android/assets/packed/packed.png";

3.Screenshot of Package Explorer http://i1294.photobucket.com/albums/b608/Abhishek_M369/PackageExplorer_zpsd2589ee2.jpg

4.Screenshot of Error log (Android DDMS) http://i1294.photobucket.com/albums/b608/Abhishek_M369/ErrorLog_zps8a4a68d9.jpg

5.All the possible combinations i tried

 "/gdxgame-android/assets/packed/packed.png";
 "gdxgame-android/assets/packed/packed.png";
 "/assets/packed/packed.png";
 "assets/packed/packed.png";
 "/packed/packed.png";
 "packed/packed.png";
 "/packed.png";
 "packed.png";

 public static FileHandle location = Gdx.files.internal("assets/packed.png");
 public static final TEXTURE_ATLAS_OBJECT = location.toString();

 //this caused shutdown of emulator
 public static FileHandle location = Gdx.files.internal("assets/packed.png");
 public static final TEXTURE_ATLAS_OBJECT = location.readString();

 // I even tried setting the build path of the asset folder as source folder
 // Also tried placing the image in the data folder.
 // Tried using the absolute path too , i.e Gdx.files.absolute("the absolute");
 // Tried passing the absolute path directly as string

Nothing seems to work for me.

Upvotes: 0

Views: 1666

Answers (1)

Abhishek Madhusudhan
Abhishek Madhusudhan

Reputation: 95

The problem was very simple, the problem lied in the different versions of libgdx and the documentation. The answer explained below will solely confirm to libgdx version 0.9.8 only.

(Note: The usage of Texturepacker GUI was used to pack textures && not the method from the libgdx library)

First the "assetManager" had to be supplied with a file that contains the coordinates of the images which was a mistake here as i was supplying the packed image.

The GL20 should be able to parse NPOT images but it was unable to do so and the reason remains unknown so i had to pack the texture to a POT which was accomplished by selecting the POT options in the GUI. After doing this i was able to load the newly POT image easily with the following code

/**Mention only the folder/file under the asset dir**/

public class Gameconstants { public static final String location = "packed/packed.txt" }

/**access the same using the following command**/

private AssetManager assetManager;
assetManager.load(Gameconstants.location,Texture.class);

This Answer may not be very convincing but it surely solved my problem.

Thank you to all who helped :)

Upvotes: 1

Related Questions