Libgdx:找不到文件错误'内部'打开外部文件时

时间:2015-06-20 12:54:43

标签: android libgdx

这对你来说很好。

我尝试使用外部打开文件。

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

我知道文件在那里,并且布尔告诉我,事实上它就在那里。

主要问题是我收到此错误。

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

我不知道为什么这样做,我不寻找内部文件。

出于好奇,我在我的资源文件夹中创建了文件,就像这个assets/MyApps/skanectModel.png一样,代码运行完美,也可以在我的android上播放应用程序。

但是......我需要能够只读外部。我正在为一些朋友构建一个简单的3D查看器,我会不时地向他们发送3D模型。

以下是代码的副本:

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() {
}
}

请帮助...... !!!

编辑: 这是完整的错误日志:

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 在MadEqua的快速建议之后,这就是我实施修复的方式。

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

2 个答案:

答案 0 :(得分:2)

仔细查看堆栈跟踪后,似乎模型文件本身正确加载。

加载模型的纹理时会出现问题:

  

在   com.badlogic.gdx.backends.android.AndroidFileHandle.read(AndroidFileHandle.java:75)   在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)   在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)   在com.badlogic.gdx.graphics.g3d.Model.convertMaterial(Model.java:290)   在com.badlogic.gdx.graphics.g3d.Model.loadMaterials(Model.java:266)

我相信libGDX正试图从错误的位置加载纹理文件(内部位置,解释你得到的错误)。

我从未使用过libDGX模型函数,但我认为您应该使用此方法:loadModel(FileHandle fileHandle, TextureProvider textureProvider)

并传入一个从外部文件夹加载Texture文件的TextureProvider。 (并确保纹理文件在那里)。

通过查看FileTextureProvider,应该很容易实现完全相同的自定义TextureProvider

答案 1 :(得分:1)

File handling Libgdx Wiki

来自他们的维基

  

外部文件路径相对于Android上的SD卡根目录以及桌面系统上当前用户的主目录。

  

本地文件相对于桌面上应用程序的根目录或工作目录以及相对于Android上应用程序的内部(私有)存储而存储。请注意,桌面上的本地和内部大致相同。

因此,如果您的文件位于项目根目录中,则应使用local而不是external。你能提供更多信息,比如测试平台。