Stencyl 3.4.0 is now out. Get it now!

Best way to save a separate file?

svintaj

  • *
  • Posts: 278
Hello! :)

I need to be able save several images from within a painting app that I'm working on. I will also need to be able to load those images and delete them from within the app. I need this to work solid on both iOS and Android. ( I also would like to be able to first check if there is enough disk space on the device to save an image, before trying to save it, to prevent crashing. )

What is the best way to do this from Stencyl?

-> I have tried the "Attribute Saving"-extension, it works ok, but it does not seem to keep the files separated, so for each image I save it takes much longer time to save the next one. So all images seems to shear the same data. And I need to have it separated in some way. ( Also I'm clueless about how to see if there is enough disk space on the device, before saving an file. )

Please help with some ideas, thanks in advance!

svintaj

  • *
  • Posts: 278
Ok, so Yoplalala was trying to help me from another thread, writing this:
Quote
Today at 06:08:53 am
Quote
Hi svintaj ! Sorry I didn't test how to do it. I'm trying Stencyl 3.5 and for now windows and android compiling doesn't work ... so quite difficult to test. Maybe I'll try on some old version.

(http://stackoverflow.com/questions/22630008/save-bitmapdata-bytearray-as-a-png-file)
Maybe this could help.
(image is an attribute of type image ;) )
// Saving the BitmapData
var b:ByteArray = image.encode("png", 1);
var fo:FileOutput = sys.io.File.write("test.png", true);
fo.writeString(b.toString());
fo.close();

Thanks Yoplalala, I'm new to this but I tried to put that in a Code Block, but when trying to compile I get those two errors:
'Type not found : ByteArray' and 'Type not found : FileOutput'?

yoplalala

  • *
  • Posts: 1376
This should compile. Try on desktop  so you can see where the file is saved :) ( because I don't know :P)

Code: [Select]
var b:openfl.utils.ByteArray = _image.encode(_image.rect,"png");
var fo:sys.io.FileOutput = sys.io.File.write("test.png", true);
fo.writeString(b.toString());
fo.close();

svintaj

  • *
  • Posts: 278
Thanks!  :) 

So I'm trying to learn this and I manage to save and load a text file on Windows, by modifying the code to this:
Code: [Select]
// Saving
var fo:sys.io.FileOutput = sys.io.File.write("test.txt", false);
fo.writeString("Just a Test...");
fo.close();

// Loading
var fi:haxe.io.Input = sys.io.File.read("test.txt", false);
_TestString = fi.readLine();
fi.close();
trace(_TestString);
(the file where created in the same folder as the exe file.)

While this code works nice on Windows,  the same code crashes on Android?
I also have added the  'WRITE_EXTERNAL_STORAGE permission' to the Androidmanifest, still crashing on Android...

Does anyone have any idea why?


svintaj

  • *
  • Posts: 278
I just understod that I have to save and load using the dir: "/data/data/com.your.packagename/" on Android.
So I got it working a bit on Android now, no crash. Yay.   :)
Code: [Select]
sys.io.File.read("/data/data/com.your.packagename/test.txt", false);I have read that the dir should be "/Documents/" on iOS, and I'll try that later but does anyone know if it's right?

yoplalala

  • *
  • Posts: 1376
There 's a data extension called external data extension , you can use it for text (even for images maybe .........  sorry didn't think about it :(  ).
You don't need to know to where it saves. There was some code to do it, but I don't remember where.

yoplalala

  • *
  • Posts: 1376

svintaj

  • *
  • Posts: 278
Yes I know about that extension but I got to be able to dele files as well. However I got my own code working now.  8) Just need to test on iOS as well. Thanks for your help Yoplalala!