PDA

View Full Version : Problems Storing a File Obtained through browseForOpen in Adobe AIR



bobTheBuilder
October 6th, 2008, 09:30 PM
I am new to the forums here and fairly new to flash, but I could really use some help. I am building an AIR application that needs to allow a user to choose a file while offline, store the file, and record the path to the stored file in a SQLite DB.

I have the SQLite DB working, and I can move the chosen file to the image repository I created in the applicationStorageDirectory.

My problem is that browseForOpen returns Void and file processing is done asynchronously by an event handler. I believe I need to be able to return the path to the image chosen by the user so that I can then store that value in the database so I can retrieve it later.

Does anybody know how to get the filename back to the calling function? Alternatively, is there another paradigm I could follow for getting the file?

I've been trying to find a way past this since yesterday. Your help would be greatly appreciated.

bobTheBuilder
October 6th, 2008, 09:36 PM
I did consider extending the File class to send a custom event containing the desired path to the event handler, but I can't figure out how to extend File while retaining access to the methods and properties of File.

bobTheBuilder
October 6th, 2008, 09:56 PM
Basically need to return "target.nativePath" to the function that calls "captureImage()" or predetermine what the filepath will be without allowing it to be overwritten if captureImage is called multiple times.

Again, really appreciate any help that you might be able to offer. Thanks all.

Code is below:

public function captureImage():void
{
var fileToOpen:File = File.desktopDirectory;
var imgFilter:FileFilter = new FileFilter("Image", "*.jpeg;*.pjpg;*.jpg;*.pjpeg;*.png;*.gif");

try
{
fileToOpen.addEventListener(Event.SELECT, fileSelected);
fileToOpen.browseForOpen("Choose a file...", [imgFilter]);
}
catch (error:Error)
{
trace("Failed:", error.message);
}
trace(fileToOpen.nativePath);
}

private function fileSelected(event:Event):void
{
var original:File = event.target as File;
var extension:String = original.nativePath.substr(original.nativePath.las tIndexOf("."));
var target:File = File.applicationStorageDirectory.resolvePath("image_vault/" + Math.round(Math.random() * 100000).toString() + extension);
var targetParent:File = target.parent;
targetParent.createDirectory();
original.copyTo(target, true);
trace("hit");
trace(target.nativePath);

}

bobTheBuilder
October 7th, 2008, 12:18 PM
For anyone else who might be experiencing the same problem....

Ultimately I was able to build a custom even and dispatch it on copying the file. I used a parameter on the custom event to pass the path back to main portion of the program.