libGdx无法在android应用程序数据目录中找到文件

时间:2013-07-07 15:25:25

标签: android file-io libgdx

我正在尝试从应用程序的数据目录中访问数据。我可以加载default.fnt文件,但它告诉我无法找到关联的default.png如何让系统识别文件?有没有我设置错误的东西?

异常

07-07 22:22:52.467: E/AndroidRuntime(10785): FATAL EXCEPTION: GLThread 240
07-07 22:22:52.467: E/AndroidRuntime(10785): com.badlogic.gdx.utils.GdxRuntimeException: Couldn't load file: /data/data/com.iliadonline.client/files/data/gfx/fonts/default.png
07-07 22:22:52.467: E/AndroidRuntime(10785):    at com.badlogic.gdx.graphics.Pixmap.<init>(Pixmap.java:140)
07-07 22:22:52.467: E/AndroidRuntime(10785):    at com.badlogic.gdx.graphics.glutils.FileTextureData.prepare(FileTextureData.java:64)
07-07 22:22:52.467: E/AndroidRuntime(10785):    at com.badlogic.gdx.graphics.Texture.load(Texture.java:175)
07-07 22:22:52.467: E/AndroidRuntime(10785):    at com.badlogic.gdx.graphics.Texture.create(Texture.java:159)
07-07 22:22:52.467: E/AndroidRuntime(10785):    at com.badlogic.gdx.graphics.Texture.<init>(Texture.java:133)
07-07 22:22:52.467: E/AndroidRuntime(10785):    at com.badlogic.gdx.graphics.Texture.<init>(Texture.java:126)
07-07 22:22:52.467: E/AndroidRuntime(10785):    at com.badlogic.gdx.graphics.g2d.BitmapFont.<init>(BitmapFont.java:125)
07-07 22:22:52.467: E/AndroidRuntime(10785):    at com.badlogic.gdx.graphics.g2d.BitmapFont.<init>(BitmapFont.java:99)
07-07 22:22:52.467: E/AndroidRuntime(10785):    at com.iliadonline.client.render.Render.loadFonts(Render.java:213)
07-07 22:22:52.467: E/AndroidRuntime(10785):    at com.iliadonline.client.render.Render.<init>(Render.java:71)
07-07 22:22:52.467: E/AndroidRuntime(10785):    at com.iliadonline.client.IliadClient.create(IliadClient.java:65)
07-07 22:22:52.467: E/AndroidRuntime(10785):    at com.badlogic.gdx.backends.android.AndroidGraphics.onSurfaceChanged(AndroidGraphics.java:322)
07-07 22:22:52.467: E/AndroidRuntime(10785):    at android.opengl.GLSurfaceView$GLThread.guardedRun(GLSurfaceView.java:1505)
07-07 22:22:52.467: E/AndroidRuntime(10785):    at android.opengl.GLSurfaceView$GLThread.run(GLSurfaceView.java:1240)
07-07 22:22:52.467: E/AndroidRuntime(10785): Caused by: com.badlogic.gdx.utils.GdxRuntimeException: Error reading file: /data/data/com.iliadonline.client/files/data/gfx/fonts/default.png (Internal)
07-07 22:22:52.467: E/AndroidRuntime(10785):    at com.badlogic.gdx.backends.android.AndroidFileHandle.read(AndroidFileHandle.java:74)
07-07 22:22:52.467: E/AndroidRuntime(10785):    at com.badlogic.gdx.files.FileHandle.readBytes(FileHandle.java:224)
07-07 22:22:52.467: E/AndroidRuntime(10785):    at com.badlogic.gdx.graphics.Pixmap.<init>(Pixmap.java:137)
07-07 22:22:52.467: E/AndroidRuntime(10785):    ... 13 more
07-07 22:22:52.467: E/AndroidRuntime(10785): Caused by: java.io.FileNotFoundException: /data/data/com.iliadonline.client/files/data/gfx/fonts/default.png
07-07 22:22:52.467: E/AndroidRuntime(10785):    at android.content.res.AssetManager.openAsset(Native Method)
07-07 22:22:52.467: E/AndroidRuntime(10785):    at android.content.res.AssetManager.open(AssetManager.java:315)
07-07 22:22:52.467: E/AndroidRuntime(10785):    at android.content.res.AssetManager.open(AssetManager.java:289)
07-07 22:22:52.467: E/AndroidRuntime(10785):    at com.badlogic.gdx.backends.android.AndroidFileHandle.read(AndroidFileHandle.java:72)
07-07 22:22:52.467: E/AndroidRuntime(10785):    ... 15 more

文件权限

我已检查过文件是否存在以及他们是否拥有合适的所有者/组,甚至将权限设置为777只是为了测试。

Shell Screen Shot

我已经看到了其他一些问题,其中问题出在项目中的 assets 文件夹中,但我没有使用assets文件夹。并且代码能够找到位于.png。

旁边的.fnt文件

字体定义标题

  

info face =“Droid Sans”size = 17 bold = 0 italic = 0 charset =“”unicode = 0   stretchH = 100 smooth = 1 aa = 1 padding = 0,0,0,0 spacing = 1,1 common   lineHeight = 20 base = 18 scaleW = 256 scaleH = 128 pages = 1 packed = 0 page id = 0   file =“default.png”chars count = 95


如果有帮助,这是我用来获取文件句柄的代码:

FileHandle dataDir = Gdx.files.local("data");

FileHandle gfxDir = dataDir.child("gfx");
if(!gfxDir.isDirectory())
{
    //mkdirs will make all directories, so this includes the "data" dir
    gfxDir.mkdirs();
}

FileHandle spritesDir = gfxDir.child("sprites");
if(!spritesDir.isDirectory())
{
    spritesDir.mkdirs();
}

FileHandle fontsDir = gfxDir.child("fonts");
if(!fontsDir.isDirectory())
{
    fontsDir.mkdirs();
}

P.S。字体文件和png在属于assets文件夹时工作。所以我假设它们设置得当。

1 个答案:

答案 0 :(得分:2)

您遇到来自FileNotFound的{​​{1}}个例外,但该文档明确指出android.content.res.AssetManager.open

但是,你不直接打电话......

啊,only works with "files bundled as assets"使用Gdx.files.internal加载字体纹理:

public BitmapFont (BitmapFontData data, TextureRegion region, boolean integer) {
        this.region = region == null ? new TextureRegion(new Texture(Gdx.files.internal(data.imagePath), false)) : region;
        ...

(看起来像Libgdx中的一个错误就是假设字体纹理文件是“内部的”,但我不确定它们应该如何解决...)

我认为您可以通过在loadFonts方法中调用Libgdx BitmapFont constructor来解决此问题。