5

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.

hata
  • 11,633
  • 6
  • 46
  • 69

2 Answers2

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
0

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.

Nanoc
  • 2,381
  • 1
  • 20
  • 35