Java 无法使用 LibGDX Gdx.files.internal 加载 .png 文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18284322/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 00:03:19  来源:igfitidea点击:

Trouble loading .png file using LibGDX Gdx.files.internal

javalibgdx

提问by KaptnKrunch

I'm trying to load a Texture to libGDX and I'm getting a file not found exception.

我正在尝试将纹理加载到 libGDX,但出现文件未找到异常。

Here's the code that's trying to load the .png file.

这是尝试加载 .png 文件的代码。

//Textures
private Texture tiles;
private TextureRegion grassImage;
private TextureRegion dirtImage;
private TextureRegion stoneImage;

//Entities
private Texture entities;
private TextureRegion playerImage;

public WorldRenderer(World world, boolean debug) {
    this.world = world;
    this.camera = new OrthographicCamera(CAMERA_WIDTH, CAMERA_HEIGHT);
    this.camera.position.set(CAMERA_WIDTH/2f, CAMERA_HEIGHT/2f, 0);
    this.camera.update();
    this.debug = debug;
    spriteBatch = new SpriteBatch();
    loadTextures();
}

public void loadTextures() {
    tiles = new Texture(Gdx.files.internal("tiles.png"));
    grassImage = new TextureRegion(tiles, 0, 0, 32, 32);
    dirtImage = new TextureRegion(tiles, 0, 64, 32, 32);
    stoneImage = new TextureRegion(tiles, 64, 0, 32, 32);

    entities = new Texture(Gdx.files.internal("entities.png"));
    playerImage = new TextureRegion(entities, 0, 0, 32, 32);
}

public void render() {
    spriteBatch.begin();
    drawGrass();
    drawDirt();
    drawStone();
    drawPlayer();
    spriteBatch.end();
    if (debug) {
        drawDebug();
    }
}

Here's the error message:

这是错误消息:

Exception in thread "LWJGL Application" com.badlogic.gdx.utils.GdxRuntimeException: Couldn't load file: tiles.png
    at com.badlogic.gdx.graphics.Pixmap.<init>(Pixmap.java:140)
    at com.badlogic.gdx.graphics.glutils.FileTextureData.prepare(FileTextureData.java:64)
    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.mr.zen.level.WorldRenderer.loadTextures(WorldRenderer.java:62)
    at com.mr.zen.level.WorldRenderer.<init>(WorldRenderer.java:58)
    at com.mr.zen.screens.GameScreen.show(GameScreen.java:29)
    at com.badlogic.gdx.Game.setScreen(Game.java:62)
    at com.mr.zen.Zen.create(Zen.java:12)
    at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:136)
    at com.badlogic.gdx.backends.lwjgl.LwjglApplication.run(LwjglApplication.java:114)
Caused by: com.badlogic.gdx.utils.GdxRuntimeException: File not found: tiles.png (Internal)
    at com.badlogic.gdx.files.FileHandle.read(FileHandle.java:132)
    at com.badlogic.gdx.files.FileHandle.length(FileHandle.java:586)
    at com.badlogic.gdx.files.FileHandle.readBytes(FileHandle.java:220)
    at com.badlogic.gdx.graphics.Pixmap.<init>(Pixmap.java:137)
    ... 12 more

The problem exists in the loadTextures() method. Can't find the file

问题存在于 loadTextures() 方法中。找不到文件

 tiles = new Texture(Gdx.files.internal("tiles.png"));

I've made sure to put the .png files in the assets folder of the android project files. I have no idea what's causing this; I've gotten this to work on other projects but this time around something went awry. Thanks for any help.

我确保将 .png 文件放在 android 项目文件的资产文件夹中。我不知道是什么原因造成的;我已经将它用于其他项目,但这次出了点问题。谢谢你的帮助。

采纳答案by yuric

Solving the "Couldn't load file" issue by cleaning the project (Eclipse)

通过清理项目解决“无法加载文件”问题(Eclipse)

(For this solution is expected that the paths to your image files are correct.)

(对于此解决方案,您的图像文件的路径应该是正确的。)

Sometimes I have this Couldn't load fileproblem when I add new images to my project. I don't know why, but my Desktop version of my LibGDX game doesn't update the folder Mygame-desktop/bin/datawith my new images (which were added to Mygame-android/assets/data) when the project is built. (That is, every time I save a file, Eclipse builds the project, cause I set the option Project > Build Automatically.)

有时,当我将新图像添加到我的项目时,我会遇到无法加载文件的问题。我不知道为什么,但是我的 LibGDX 游戏的桌面版本在项目构建时没有使用我的新图像(已添加到Mygame-android/assets/data)更新文件夹Mygame-desktop/bin/data. (也就是说,每次我保存文件时,Eclipse 都会构建项目,因为我设置了选项Project > Build Automatically。)

It should work, cause in LibGDX the Desktop and HTML projects have a link to the Android project assets folder. So, I was not suppose to need to copy manually my new images to Mygame-desktop/bin/data.

它应该可以工作,因为在 LibGDX 中,桌面和 HTML 项目有一个指向 Android 项目资产文件夹的链接。因此,我不认为需要将我的新图像手动复制到Mygame-desktop/bin/data

The solution, on Eclipse, is to clean (menu Project > Clean...) all LibGDX projects you use for your game, then the files in Mygame-android/assets/datawill be copied to Mygame-desktop/bin/dataproperly.

该解决方案,Eclipse的,是干净的(菜单项目>清洁...),你用你的游戏中所有LibGDX项目,然后将文件在Mygame-的Android /资产/数据将被复制到Mygame桌面/斌/数据正确.

回答by aug13

Make a data folder in the assets folder and instead of using

在资产文件夹中创建一个数据文件夹,而不是使用

tiles = new Texture(Gdx.files.internal("tiles.png"));

try this:

尝试这个:

tiles = new Texture("data/tiles.png");

回答by Vikas Sardana

create new folder datain the assets folder of the android project and then access the file using Gdx.files.internal("data/tiles.png")

在android项目的assets文件夹中新建文件夹data,然后使用Gdx.files.internal("data/tiles.png")访问该文件

tiles = new Texture(Gdx.files.internal("data/tiles.png"));

tiles = new Texture(Gdx.files.internal("data/tiles.png"));

回答by Milixyron

If you're using Gradle, click the left mouse button in package explorer on Aassets -> Gradle -> Refresh All.

如果您使用的是 Gradle,请在包资源管理器中单击鼠标左键 Aassets -> Gradle -> Refresh All.

That's it =)

就是这样=)

回答by Alexander Ermakov

Click the left mouse button in Package Explorer on Refresh or just F5.

在 Package Explorer 中的 Refresh 或 F5 上单击鼠标左键。

This solved my problem.

这解决了我的问题。

回答by Dewey Banks

I have this issue working in android studio 2 using the current libgdx version. When I save as a png in psp - cs6 and/or cs15 - it can't read it. I can't recall the exact error message, something like io can't read stream. So I tried saving it again in corel paint. Same problem. Then with the simple paint utility packaged with windows. No good. Finally I downloaded paint.net. Now my workflow just involves an extra step. Any png generated with psp, etc., pretty much any png libgdx grumps about, I load into paint.net, hit "save" and I'm good to go.

我在使用当前 libgdx 版本的 android studio 2 中遇到了这个问题。当我在 psp - cs6 和/或 cs15 中保存为 png 时 - 它无法读取它。我不记得确切的错误消息,比如 io 无法读取流。所以我尝试在 Corel Paint 中再次保存它。同样的问题。然后使用 Windows 打包的简单绘图实用程序。不好。最后我下载了paint.net。现在我的工作流程只涉及一个额外的步骤。任何用 psp 等生成的 png,几乎所有 png libgdx 都发脾气,我加载到paint.net,点击“保存”,我很高兴。

Not really a solution per se, but gets the job done.

本身并不是真正的解决方案,但可以完成工作。

Looks like you're good but I hope this helps someone out there. Marc

看起来你很好,但我希望这可以帮助那里的人。马克

回答by S. Entsov

Maybe your PNG profile is not supported by LibGDX.

也许 LibGDX 不支持您的 PNG 配置文件。

I set it to sRGB-elle-V2-srgbtrc.icc(with Krita) and now it works.

我将它设置为sRGB-elle-V2-srgbtrc.icc(使用 Krita),现在它可以工作了。

In Krita go to:

在 Krita 中访问:

Image > Properties > Profile

图像 > 属性 > 配置文件