Save and retrieve an image file in LibGDX(在 LibGDX 中保存和检索图像文件)
问题描述
如何在 LibGDX 中保存和检索图像文件.我想将图像文件保存在 AndroidApplication 类的本地存储中,并在我的核心项目中检索它.
How to Save and retrieve an image file in LibGDX. I want to save an image file in local storage in AndroidApplication class and retrieve it in my Core project.
推荐答案
Libgdx 中的文件处理在 libGDX 维基.
The file handling in Libgdx is well describe in the libGDX wiki.
简而言之:您正在使用 FileHandle 对象打开文件,该对象可以通过调用其中之一来检索
In a nutshell: you are opening the file using FileHandle object that can be retrieved by calling one of
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!
然后从文件创建纹理看起来像
Then creating texture from file looks like
Texture tex = new Texture( Gdx.files.internal("path.jpg") );
那么您应该做的是使用 external() 获取文件以检索 FileHandle,然后用它做任何你想做的事情,然后使用 local() 保存它.FileHandle 有方法 readBytes() 和 writeBytes 允许您打开/保存数据
Then what you should do would be to get a file using external() to retrieve FileHandle, then do whatever you want with it and just save it using local(). FileHandle has methods readBytes() and writeBytes that allows you to open/save data
FileHandle from = Gdx.files.external("image.jpg");
byte[] data = from.readBytes();
...
FileHandle to = Gdx.files.local("image.jpg");
to.writeBytes(data);
<小时>
如果您想在保存之前修改图像,您应该查看 Pixmap 和 PixmapIO 类
这篇关于在 LibGDX 中保存和检索图像文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在 LibGDX 中保存和检索图像文件


- 如何使 JFrame 背景和 JPanel 透明且仅显示图像 2022-01-01
- 如何指定 CORS 的响应标头? 2022-01-01
- Eclipse 的最佳 XML 编辑器 2022-01-01
- 获取数字的最后一位 2022-01-01
- 未找到/usr/local/lib 中的库 2022-01-01
- GC_FOR_ALLOC 是否更“严重"?在调查内存使用情况时? 2022-01-01
- 在 Java 中,如何将 String 转换为 char 或将 char 转换 2022-01-01
- 转换 ldap 日期 2022-01-01
- 将 Java Swing 桌面应用程序国际化的最佳实践是什么? 2022-01-01
- java.lang.IllegalStateException:Bean 名称“类别"的 BindingResult 和普通目标对象都不能用作请求属性 2022-01-01