Save and retrieve an image file in LibGDX

允我心安 提交于 2019-12-13 16:07:56

问题


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.


回答1:


The file handling in Libgdx is well describe in the libGDX wiki.

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

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

if you want to modify the image before saving it you should take a look at Pixmap and PixmapIO classes




回答2:


I hope you dont want to store something on assets folder, because that cannot be done.

You can save it in the internal storage using basic java, take a look at this

Hope this helps.



来源:https://stackoverflow.com/questions/32816723/save-and-retrieve-an-image-file-in-libgdx

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!