2

I need to export an image from a libGDX game, and make it appear in the default Photos app on an iPad.

Currently, I do it like this:

Pixmap image = getScreenshot();

FileHandle file;
String filename = "diplom_" + game.player.getID() + ".png";

if(Gdx.files.isExternalStorageAvailable())
    file = Gdx.files.external(filename);
else
    file = Gdx.files.local(filename);

PixmapIO.writePNG(file, image);
pixmap.dispose();

But the screenshot doesn't appear anywhere. How can I make it appear in the Photos app?

Erlend D.
  • 3,013
  • 6
  • 37
  • 59

2 Answers2

3

What I am doing in such case is:

private NSData getImageAsNsData(Pixmap pixmap) {
    FileHandle file = Gdx.files.local("tmpImage.png");
    PixmapIO.writePNG(file, pixmap);
    NSData imageData = NSData.read(file.file());
    file.delete();
    return imageData;
}

public void sendToGallery(Pixmap pixmap) {
    NSData imageData = getImageAsNsData(pixmap);
    uiImage = new UIImage(imageData);
    uiImage.saveToPhotosAlbum(new VoidBlock2<UIImage, NSError>() {
        @Override
        public void invoke(UIImage uiImage, NSError nsError) {
            if (nsError!=null)
                Gdx.app.log("Error", "Unable to save: " + nsError.getLocalizedDescription());
        }
    });
}

I hope it helps you. :)

centy
  • 61
  • 3
0

At first try adding logger so you see where you add put the file.

if you put it in local:

Local files are stored relative to the application's root or working directory on desktops and relative to the internal (private) storage of the application on Android. Note that Local and internal are mostly the same on the desktop.

if external

External files paths are relative to the SD card root on Android and to the home directory of the current user on desktop systems.

refare to FileHandling libGDX wiki

So i guess you save it in local thats why you wont find it in iOS. Else you need to create the right path to the pictures folder. Else you just save it at the same folder where the apps get installed. But i think youll need to use the absolut verion of the filehandle. Else you can save something at an total different path.

In this case, “myfile.txt” needs to be in the users’ home directory (/home//myfile.txt on linux or \Users\\myfile.txt on Windows and MacOS) on desktop, and in the root of the SD card on Android.

FileHandle handle = Gdx.files.absolute("/some_dir/subdir/myfile.txt");
bemeyer
  • 6,154
  • 4
  • 36
  • 86
  • Thank you, I will try that tomorrow. Will the Photos app detect the image automatically once I place it in the right directory, or do I have to do some other magic to tell Photos about it? – Erlend D. Aug 29 '13 at 13:39
  • I am not sure but i think it will know it automatic. everything else would be strange – bemeyer Aug 29 '13 at 14:44