4

I found the below snippet to save an image to a specific folder with Adobe AIR.

How to modify it that the "Save As" OS-window comes up first? (So users can choose where to save the image on the hard-drive)

var arrBytes:ByteArray = PNGEncoder.encode(bmd);
    var file:File = File.desktopDirectory.resolvePath(folderName + "/" + fileName + fileNum + ".png");
    var fileStream:FileStream = new FileStream();
    //
    fileStream.open(file, FileMode.WRITE);
    fileStream.writeBytes(arrBytes);
    fileStream.close();

Thanks.

Tom
  • 5,588
  • 20
  • 77
  • 129

1 Answers1

6

Use File.browseForSave:

import flash.filesystem.*;
import flash.events.Event;
import flash.utils.ByteArray;

var imgBytes:ByteArray = PNGEncoder.encode(bmd);
var docsDir:File = File.documentsDirectory;
try
{
    docsDir.browseForSave("Save As");
    docsDir.addEventListener(Event.SELECT, saveData);
}
catch (error:Error)
{
    trace("Failed:", error.message);
}

function saveData(event:Event):void 
{
    var newFile:File = event.target as File;
    if (!newFile.exists) // remove this 'if' if overwrite is OK.
    {
        var stream:FileStream = new FileStream();
        stream.open(newFile, FileMode.WRITE);
        stream.writeBytes(imgBytes);
        stream.close();
    } 
    else trace('Selected path already exists.');
}

The manual is always your friend :)

BTW, I see you're relatively new here - welcome to StackExchange! If you find my answer helpful, please be sure to select it as the answer.

N Rohler
  • 4,595
  • 24
  • 20
  • Selected. Thank you very much. Btw: Is there a "fastest way" to save an image to drive from AIR? By encoding a bitmap of 2800px x 2800px I always got a delay till it's saved - is it normal? – Tom Oct 11 '11 at 02:08
  • Glad it helped. The delay is with encoding (not file writing). See [here](http://stackoverflow.com/questions/2509554/fast-or-asynchronous-as3-jpeg-encoding) and the fastest of them all [here](http://www.blooddy.by/en/crypto/benchmark/). – N Rohler Oct 11 '11 at 02:17
  • @NRohler - That blooddy encoder looks really good! Unfortunately, I downloaded the swc and it doesn't seem to compile correctly. FDT can't parse it correctly either. Is there an older version anywhere that works okay? – jowie Dec 12 '12 at 14:54
  • @jowie I've never tried that SWC with an FDT project. You might want to try contacting Nick (or Powerflasher) directly. – N Rohler Dec 13 '12 at 15:30
  • @NRohler thanks but I've tried it in Flash directly also, and it won't compile when I try to access its commands. It sits alongside a few other swc files but is the only one that won't work. – jowie Dec 13 '12 at 19:59