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.
Asked
Active
Viewed 2,761 times
5
-
You mean loading an image from project assets in LibGDX? – Nanoc Sep 28 '15 at 07:58
-
No, I want to get image from external storage and save in local storage @Nanoc – Sep 28 '15 at 08:57
-
Did you read the libgdx documentation on file management? It's pretty straight-forward. – Tenfour04 Sep 28 '15 at 13:07
2 Answers
3
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

m.antkowicz
- 13,268
- 18
- 37