user5383483
user5383483

Reputation:

Save and retrieve an image file in LibGDX

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

Answers (2)

m.antkowicz
m.antkowicz

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

Nanoc
Nanoc

Reputation: 2381

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.

Upvotes: 0

Related Questions