I want to pin down the path to save the image in the .. \ DCIM .
import flash.display.MovieClip;
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.*;
save_mc.addEventListener (MouseEvent.CLICK, ImageSave);
var transparent:Boolean = true;
var fillColor:Number = 0x00FFFFFF;
function ImageSave (image:MouseEvent):void
{
newwind.visible = true;
newwind.play();
var myBitmapData:BitmapData = new BitmapData(flashmo_pic.getChildAt(0).width,flashmo_pic.getChildAt(0).height,transparent, fillColor);
myBitmapData.draw (flashmo_pic.getChildAt(0));
var jpgEncoder:JPGEncoder = new JPGEncoder();
var imgByteData:ByteArray = jpgEncoder.encode(myBitmapData);
file = new FileReference();
var fileReference:FileReference=new FileReference();
file.save (imgByteData, "box.jpg");
}
On mobile, you can't use FileReference
as far as I know. Instead you use the File
and FileStream
classes.
It sounds though, like you want to save a photo in the Camera Roll?
There is a built in class in AIR to do so called CameraRoll
if(CameraRoll.supportsBrowseForImage){
var myBitmapData:BitmapData = new BitmapData(flashmo_pic.getChildAt(0).width,flashmo_pic.getChildAt(0).height,transparent, fillColor);
myBitmapData.draw (flashmo_pic.getChildAt(0));
var roll:CameraRoll = new CameraRoll();
roll.addBitmapData(myBitmapData);
}
If you don't want to use the CameraRoll, then the equivalent of FileReference on AIR is the following:
First, you need a File object. This can be an existing file or one that doesn't exist yet:
var saveFile:File = File.applicationStorageDirectory.resolvePath("box.jpg");
//sadly, the sdcard path varies per Android manufacturer.
//you could walk through the directory tree to find the DCIM folder by looping through File.getRootDirectories() array (see example farther down)
Then, if you want to prompt the user where to save (it will use the name and location of the file as the starting point), you can do this:
//listen for when the user is done selecting a location
saveFile.addEventListener(Event.SELECT, onSaveSelect);
//open the save dialog
saveFile.browseForSave("Save Your File");
//save the file
function onSaveSelect(e:Event):void {
var imgByteData:ByteArray; //populate this like are doing in your quesiton
var file:File = e.target as File;
var stream:FileStream = new FileStream();
stream.open(file, FileMode.WRITE);
stream.writeBytes(imgByteData);
stream.close();
}
If you don't want to bother asking the user, but just want to go ahead and save the file without them being involved, you can just do this without calling the browseForSave function.
var stream:FileStream = new FileStream();
stream.open(saveFile, FileMode.WRITE);
stream.writeBytes(imgByteData);
stream.close();
You could walk the directory tree to try and find the DCIM folder (as it's not always in a consistent spot on Android devices)
Something like this: (untested)
var saveFile:File = checkForDCIM(File.getRootDirectories());
if(saveFile){
saveFile.resolvePath("box.jpg"); //saveFile is a folder, so make a reference to a jpg file
//save the file using the method described above
}
//this function will recursively go through all directories looking for one called "DCIM"
function checkForDCIM(fileArray:Array):File {
var f:File;
for (var i:int = 0; i < fileArray.length; i++) {
if (File(fileArray[i]).isDirectory) {
if (File(fileArray[i]).name == "DCIM") return fileArray[i];
f = checkForDCIM(File(fileArray[i]).getDirectoryListing());
if (f) return f;
}
}
return null;
}
You also don't need to prompt the user the save the file with AIR, you can actually just save it directly with no interaction from the user at all.