在LibGDX中保存和检索图像文件 [英] Save and retrieve an image file in LibGDX

查看:88
本文介绍了在LibGDX中保存和检索图像文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在LibGDX中保存和检索图像文件.我想将图像文件保存在AndroidApplication类的本地存储中,然后在我的Core项目中检索它.

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 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()获取文件以检索

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


如果要在保存之前修改图像,则应查看 查看全文

登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆