Libgdx: File not found Error "Internal" when opening external file

Here's a good one.

I am trying to open a file using external.

FileHandle dirHandel = Gdx.files.external("MyApps/skanectModel.g3db");
boolean isDir = Gdx.files.external("MyApps/skanectModel.g3db").exists();

      

I know the file is there, and the bool tells me that it actually is.

The main problem is that I am getting this error.

Caused by: com.badlogic.gdx.utils.GdxRuntimeException: Error reading file: MyApps/skanectModel.png (Internal)

      

I don't know why to do this, I am not looking for an internal file.

Out of curiosity, I created a file in my assets folder just like this assets/MyApps/skanectModel.png

one and the code works fine, also the app can be played on my android.

But ... I only need to be able to read external ones. I am creating a simple 3D view for some friends and I will send them 3D models from time to time.

Here is a copy of the code:

package com.mygdx.game;

import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.assets.loaders.ModelLoader;
import com.badlogic.gdx.Files;
import com.badlogic.gdx.files.FileHandle;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.PerspectiveCamera;
import com.badlogic.gdx.graphics.g3d.Environment;
import com.badlogic.gdx.graphics.g3d.Model;

import com.badlogic.gdx.graphics.g3d.ModelBatch;
import com.badlogic.gdx.graphics.g3d.ModelInstance;
import com.badlogic.gdx.graphics.g3d.attributes.ColorAttribute;
import com.badlogic.gdx.graphics.g3d.environment.DirectionalLight;
import com.badlogic.gdx.graphics.g3d.loader.G3dModelLoader;
import com.badlogic.gdx.graphics.g3d.loader.ObjLoader;
import com.badlogic.gdx.graphics.g3d.utils.CameraInputController;
import com.badlogic.gdx.math.Vector3;
import com.badlogic.gdx.utils.UBJsonReader;

public class MyGdxGame implements ApplicationListener {
    public Environment environment;
    public PerspectiveCamera cam;
    public CameraInputController camController;
    public ModelBatch modelBatch;
    public Model model;
    public ModelInstance instance;

@Override
public void create() {
    environment = new Environment();
    environment.set(new ColorAttribute(ColorAttribute.AmbientLight, 1.4f, 1.4f, 1.4f, 5f));
    environment.add(new DirectionalLight().set(0.8f, 0.8f, 0.8f, -1f, -0.8f, -0.2f));

    cam = new PerspectiveCamera(75,Gdx.graphics.getWidth(),Gdx.graphics.getHeight());
    cam.position.set(50f, 1f, 50f);
    cam.lookAt(0,0,0);
    cam.near = 0.1f;
    cam.far = 500f;
    cam.update();

    modelBatch = new ModelBatch();
//      ModelLoader<?> loader = new ObjLoader();
    UBJsonReader jsonReader = new UBJsonReader();
    G3dModelLoader modelLoader = new G3dModelLoader(jsonReader);

//      model = modelLoader.loadModel(Gdx.files.getFileHandle("root/MyApps/skanectModel.g3db",Files.FileType.Absolute));
//      model = modelLoader.loadModel(Gdx.files.getFileHandle("skanectModel.g3db",Files.FileType.External));

    FileHandle dirHandel = Gdx.files.external("MyApps/skanectModel.g3db");
    boolean isDir = Gdx.files.external("MyApps/skanectModel.g3db").exists();
    Gdx.app.log("is it", String.valueOf(isDir));
//      for (FileHandle entry: dirHandel.list()){
//          Gdx.app.log("Path",entry.toString());
//      }
    model = modelLoader.loadModel(dirHandel);
//      model = modelLoader.loadModel(Gdx.files.getFileHandle("skanectModel.g3db",Files.FileType.External));
    instance = new ModelInstance(model);
    instance.transform.rotate(90f,0f,0,-90f);


    camController = new CameraInputController(cam);
    Gdx.input.setInputProcessor(camController);
}

@Override
public void render() {
    camController.update();

    Gdx.gl.glViewport(0,0,Gdx.graphics.getWidth(),Gdx.graphics.getHeight());
    Gdx.gl.glClearColor(.5f,1,1,1);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT | GL20.GL_DEPTH_BUFFER_BIT);

    modelBatch.begin(cam);
    modelBatch.render(instance, environment);
    modelBatch.end();
}

@Override
public void dispose() {
    modelBatch.dispose();
    model.dispose();
}

@Override
    public void resize(int width, int height) {
}

@Override
    public void pause() {
}

@Override
    public void resume() {
}
}

      

Please, help...!!!

EDIT: Here is the complete error log:

06-20 10:00:47.918    3205-3221/com.mygdx.game.android E/AndroidRuntime﹕    FATAL EXCEPTION: GLThread 39207
Process: com.mygdx.game.android, PID: 3205
com.badlogic.gdx.utils.GdxRuntimeException: Couldn't load file:MyApps/skanectModel.png
at com.badlogic.gdx.graphics.Pixmap.<init>(Pixmap.java:140)
at com.badlogic.gdx.graphics.TextureData$Factory.loadFromFile(TextureData.java:98)  at com.badlogic.gdx.graphics.Texture.<init>(Texture.java:100) 
at com.badlogic.gdx.graphics.Texture.<init>(Texture.java:92)
at com.badlogic.gdx.graphics.g3d.utils.TextureProvider$FileTextureProvider.load(TextureProvider.java:34) 
at com.badlogic.gdx.graphics.g3d.Model.convertMaterial(Model.java:290)
at com.badlogic.gdx.graphics.g3d.Model.loadMaterials(Model.java:266)
at com.badlogic.gdx.graphics.g3d.Model.load(Model.java:107)
at com.badlogic.gdx.graphics.g3d.Model.<init>(Model.java:102)
at com.badlogic.gdx.assets.loaders.ModelLoader.loadModel(ModelLoader.java:54)
at com.badlogic.gdx.assets.loaders.ModelLoader.loadModel(ModelLoader.java:69)
at com.mygdx.game.MyGdxGame.create(MyGdxGame.java:65)
at com.badlogic.gdx.backends.android.AndroidGraphics.onSurfaceChanged(AndroidGraphics.java:241)
at android.opengl.GLSurfaceView$GLThread.guardedRun(GLSurfaceView.java:1520)
at android.opengl.GLSurfaceView$GLThread.run(GLSurfaceView.java:1248)
Caused by: com.badlogic.gdx.utils.GdxRuntimeException: Error reading file: MyApps/skanectModel.png (Internal)
at com.badlogic.gdx.backends.android.AndroidFileHandle.read(AndroidFileHandle.java:77)
at com.badlogic.gdx.files.FileHandle.readBytes(FileHandle.java:222)
at com.badlogic.gdx.graphics.Pixmap.<init>(Pixmap.java:137)
at com.badlogic.gdx.graphics.TextureData$Factory.loadFromFile(TextureData.java:98)
at com.badlogic.gdx.graphics.Texture.<init>(Texture.java:100)
at com.badlogic.gdx.graphics.Texture.<init>(Texture.java:92)
at com.badlogic.gdx.graphics.g3d.utils.TextureProvider$FileTextureProvider.load(TextureProvider.java:34)
at com.badlogic.gdx.graphics.g3d.Model.convertMaterial(Model.java:290)
at com.badlogic.gdx.graphics.g3d.Model.loadMaterials(Model.java:266)
at com.badlogic.gdx.graphics.g3d.Model.load(Model.java:107)
at com.badlogic.gdx.graphics.g3d.Model.<init>(Model.java:102)
at com.badlogic.gdx.assets.loaders.ModelLoader.loadModel(ModelLoader.java:54)
at com.badlogic.gdx.assets.loaders.ModelLoader.loadModel(ModelLoader.java:69)
at com.mygdx.game.MyGdxGame.create(MyGdxGame.java:65)
at com.badlogic.gdx.backends.android.AndroidGraphics.onSurfaceChanged(AndroidGraphics.java:241)
at android.opengl.GLSurfaceView$GLThread.guardedRun(GLSurfaceView.java:1520)
at android.opengl.GLSurfaceView$GLThread.run(GLSurfaceView.java:1248)
Caused by: java.io.FileNotFoundException: MyApps/skanectModel.png
at android.content.res.AssetManager.openAsset(Native Method)
at android.content.res.AssetManager.open(AssetManager.java:324)
at android.content.res.AssetManager.open(AssetManager.java:298)
at com.badlogic.gdx.backends.android.AndroidFileHandle.read(AndroidFileHandle.java:75)
at com.badlogic.gdx.files.FileHandle.readBytes(FileHandle.java:222)
at com.badlogic.gdx.graphics.Pixmap.<init>(Pixmap.java:137)
at com.badlogic.gdx.graphics.TextureData$Factory.loadFromFile(TextureData.java:98)
at com.badlogic.gdx.graphics.Texture.<init>(Texture.java:100)
at com.badlogic.gdx.graphics.Texture.<init>(Texture.java:92)
at com.badlogic.gdx.graphics.g3d.utils.TextureProvider$FileTextureProvider.load(TextureProvider.java:34)
at com.badlogic.gdx.graphics.g3d.Model.convertMaterial(Model.java:290)
at com.badlogic.gdx.graphics.g3d.Model.loadMaterials(Model.java:266)
at com.badlogic.gdx.graphics.g3d.Model.load(Model.java:107)
at com.badlogic.gdx.graphics.g3d.Model.<init>(Model.java:102)
at com.badlogic.gdx.assets.loaders.ModelLoader.loadModel(ModelLoader.java:54)
at com.badlogic.gdx.assets.loaders.ModelLoader.loadModel(ModelLoader.java:69)
at com.mygdx.game.MyGdxGame.create(MyGdxGame.java:65)
at com.badlogic.gdx.backends.android.AndroidGraphics.onSurfaceChanged(AndroidGraphi cs.java:241)
at android.opengl.GLSurfaceView$GLThread.guardedRun(GLSurfaceView.java:1520)
at android.opengl.GLSurfaceView$GLThread.run(GLSurfaceView.java:1248)

      

****** EDIT ***** Following a quick recommendation from MadEqua, this is how I applied the fix.

FileHandle dirHandel = Gdx.files.external("MyApps/skanectModel.g3db");
myT = new TextureProvider() {
@Override
public Texture load(String fileName) {
Texture result = new Texture(Gdx.files.external(fileName));
result.setFilter(Texture.TextureFilter.Linear, Texture.TextureFilter.Linear);
result.setWrap(Texture.TextureWrap.Repeat, Texture.TextureWrap.Repeat);
return result;
  }
};
myT.load("Myapps/skanectModel.png");
model = modelLoader.loadModel(dirHandel,myT);

      

+3


source to share


2 answers


After looking closely at the stack trace, it appears that the model file itself is being loaded as expected.

The problem occurs when loading model textures:

at com.badlogic.gdx.backends.android.AndroidFileHandle.read (AndroidFileHandle.java:75) at com.badlogic.gdx.files.FileHandle.readBytes (FileHandle.java:222) at com.badlogic.gdx.graphics.Pixmap ... (Pixmap.java:137) at com.badlogic.gdx.graphics.TextureData $ Factory.loadFromFile (TextureData.java:98) at com.badlogic.gdx.graphics.Texture. (Texture.java:100) at com.badlogic.gdx.graphics.Texture. (Texture.java:92) at com.badlogic.gdx.graphics.g3d.utils.TextureProvider $ FileTextureProvider.load (TextureProvider.java:34) at com.badlogic.gdx.graphics.g3d.Model.convertMaterial (Model.java : 290) at com.badlogic.gdx.graphics.g3d.Model.loadMaterials (Model.java:266)

I believe libGDX is trying to load texture files from the wrong location (internal location, explaining the error it received).



I have never worked with libDGX model functions, but I think you should use this method: loadModel (FileHandle fileHandle, TextureProvider textureProvider)

and pass to TextureProvider

, which loads the texture file from an external folder. (and make sure the texture file is there).

It should be easy to implement a custom TextureProvider

one that does exactly that by looking at FileTextureProvider .

+2


source


Libgdx Wiki File Handling

From their wiki

External file paths refer to the root of the SD card on Android, and to the current user's home directory on desktop systems.



and

Local files are stored relative to the application root or working directory on desktops and relative to the internal (private) storage of the Android application. Note that local and internal are basically the same on the desktop.

So, if your file is at the root of the project, you should use local, not external. Can you provide more information such as a test platform.

+1


source







All Articles