将图像保存在 sdcard 上的 phone 中?
Save the image in the phone on sdcard?
我想确定保存图像的路径
..\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");
}
据我所知,您不能在移动设备上使用 FileReference
。相反,您使用 File
和 FileStream
classes.
听起来好像您想将照片保存在相机胶卷中?
AIR 中有一个名为 CameraRoll
的内置 class
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);
}
如果您不想使用 CameraRoll,则 AIR 上的 FileReference 等效如下:
首先,您需要一个文件对象。这可以是现有文件或尚不存在的文件:
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)
然后,如果你想提示用户保存位置(它将以文件的名称和位置作为起点),你可以这样做:
//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();
}
如果您不想打扰用户,而只想在不让他们参与的情况下继续保存文件,您可以直接执行此操作而无需调用 browseForSave 函数。
var stream:FileStream = new FileStream();
stream.open(saveFile, FileMode.WRITE);
stream.writeBytes(imgByteData);
stream.close();
您可以遍历目录树尝试找到 DCIM 文件夹(因为它在 Android 设备上并不总是位于一致的位置)
像这样:(未经测试)
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;
}
您也不需要提示用户使用 AIR 保存文件,实际上您可以直接保存它,根本不需要用户交互。
我想确定保存图像的路径 ..\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");
}
据我所知,您不能在移动设备上使用 FileReference
。相反,您使用 File
和 FileStream
classes.
听起来好像您想将照片保存在相机胶卷中?
AIR 中有一个名为 CameraRoll
的内置 classif(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);
}
如果您不想使用 CameraRoll,则 AIR 上的 FileReference 等效如下:
首先,您需要一个文件对象。这可以是现有文件或尚不存在的文件:
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)
然后,如果你想提示用户保存位置(它将以文件的名称和位置作为起点),你可以这样做:
//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();
}
如果您不想打扰用户,而只想在不让他们参与的情况下继续保存文件,您可以直接执行此操作而无需调用 browseForSave 函数。
var stream:FileStream = new FileStream();
stream.open(saveFile, FileMode.WRITE);
stream.writeBytes(imgByteData);
stream.close();
您可以遍历目录树尝试找到 DCIM 文件夹(因为它在 Android 设备上并不总是位于一致的位置)
像这样:(未经测试)
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;
}
您也不需要提示用户使用 AIR 保存文件,实际上您可以直接保存它,根本不需要用户交互。