Reputation:
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.
Upvotes: 5
Views: 2996
Reputation: 13581
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
Upvotes: 3