Android 是否将图像保存在SD卡上的手机中?

Android 是否将图像保存在SD卡上的手机中?,android,actionscript-3,air,flash-cs6,Android,Actionscript 3,Air,Flash Cs6,我想确定保存图像的路径 .. \ DCIM 据我所知,在手机上,你不能使用FileReference。而是使用文件和文件流类 听起来好像你想把照片保存在相机里 有一个内置的类在空中做所谓的 如果不想使用CameraRoll,则与“空中文件引用”等效的方法如下: 首先,您需要一个文件对象。这可以是现有文件,也可以是尚不存在的文件: var saveFile:File = File.applicationStorageDirectory.resolvePath("box.jpg"); //sad

我想确定保存图像的路径 .. \ DCIM


据我所知,在手机上,你不能使用
FileReference
。而是使用
文件
文件流

听起来好像你想把照片保存在相机里

有一个内置的类在空中做所谓的


如果不想使用CameraRoll,则与“空中文件引用”等效的方法如下:

首先,您需要一个文件对象。这可以是现有文件,也可以是尚不存在的文件:

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());
如果(保存文件){
saveFile.resolvePath(“box.jpg”);//saveFile是一个文件夹,因此请引用jpg文件
//使用上述方法保存文件
}
//此函数将递归地遍历所有目录,查找名为“DCIM”的目录
函数checkForDCIM(fileArray:Array):文件{
var f:文件;
for(变量i:int=0;i

您也不需要提示用户使用AIR保存文件,实际上您可以直接保存文件,而不需要用户进行任何交互。

请更新到问题,以包括当前问题。在移动设备上,您对可以访问哪些文件目录有许多限制。您也不能在手机上使用传统的
文件引用。请看我的答案:当您按下按钮保存图像时,程序停止工作?我的答案中的代码会使应用程序崩溃吗?你是这么说的吗?那么这对你有用吗?或者相机不是你想要的吗?我已经为你添加了一些其他的选择。我不明白你在问我什么。
//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();
}
    var stream:FileStream = new FileStream();
    stream.open(saveFile, FileMode.WRITE);
    stream.writeBytes(imgByteData);
    stream.close();
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;
}