【问题标题】:Save and retrieve an image file in LibGDX在 LibGDX 中保存和检索图像文件
【发布时间】:2015-09-28 06:15:35
【问题描述】:

如何在 LibGDX 中保存和检索图像文件。我想将图像文件保存在 AndroidApplication 类的本地存储中,并在我的核心项目中检索它。

【问题讨论】:

  • 您的意思是从 LibGDX 中的项目资产中加载图像?
  • 不,我想从外部存储中获取图像并保存在本地存储中@Nanoc
  • 您是否阅读了有关文件管理的 libgdx 文档?这很简单。

标签: java android libgdx


【解决方案1】:

Libgdx 中的文件处理在libGDX wiki 中有很好的描述。

简而言之:您正在使用 FileHandle 对象打开文件,该对象可以通过调用其中之一来检索

    Gdx.files.external("path.txt"); //files on SD card [Android]
    Gdx.files.absolute("path.txt"); //absolute path to file
    Gdx.files.internal("path.txt"); //asset directory
    Gdx.files.local("path.txt"); //local storage - only here you can write safely!

然后从文件创建纹理看起来像

    Texture tex = new Texture( Gdx.files.internal("path.jpg") );

那么你应该做的是使用external()获取一个文件来检索FileHandle,然后用它做任何你想做的事情并使用local()。 FileHandle 有方法 readBytes()writeBytes 允许您打开/保存数据

    FileHandle from = Gdx.files.external("image.jpg");
    byte[] data = from.readBytes();

    ...

    FileHandle to = Gdx.files.local("image.jpg");
    to.writeBytes(data);

如果您想在保存之前修改图像,您应该查看PixmapPixmapIO

【讨论】:

    【解决方案2】:

    我希望你不想在资产文件夹中存储一些东西,因为那是不可能的。

    你可以使用基本的java将它保存在内部存储中,看看this

    希望这会有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-09-06
      • 1970-01-01
      • 1970-01-01
      • 2015-12-12
      • 2012-03-14
      • 2022-07-05
      • 2015-03-07
      相关资源
      最近更新 更多