Flex Mobile缺少下载的文件

时间:2014-01-07 07:15:49

标签: android flex actionscript flash-builder flex-mobile

我正在使用Flex和Flash Builder开发Android应用程序 我使用以下代码使用URLLoader和FileStream下载视频。

public function download():void{
                var loader:URLLoader = new URLLoader();
                loader.dataFormat = URLLoaderDataFormat.BINARY;
                loader.addEventListener(IOErrorEvent.IO_ERROR,function(e:IOErrorEvent):void{
                    progressLabel.text = "Loader IO Error";
                });
                loader.addEventListener(Event.COMPLETE,downloadComplete);
                loader.load(new URLRequest("[MY URL GOES HERE]"));
                progressLabel.text = "Downloading...";
            }
private function downloadComplete(event:Event):void{
                try{
                    var file:File=File.applicationStorageDirectory.resolvePath("file:///mnt/sdcard/MyVideos");

                var ba:ByteArray  = event.target.data as ByteArray;
                var fileStream:FileStream = new FileStream();
                fileStream.addEventListener(IOErrorEvent.IO_ERROR,function(e:IOErrorEvent):void{
                    progressLabel.text = "File IO Error";
                });
                fileStream.open(file, FileMode.WRITE);
                fileStream.writeBytes(ba);
                fileStream.addEventListener(Event.COMPLETE, fileClosed); 
                fileStream.close(); 
                progressLabel.text = "Download Sucessful";
            }
            catch(eeee){
                progressLabel.text = "Error";
            }
        }
        private function fileClosed(event:Event):void {
            openLabel.text = "File Closed";
        }

使用摩托罗拉Xoom进行测试时,显示下载成功但文件无法在目录中找到:
var file:File = File.applicationStorageDirectory.resolvePath(“file:/// mnt / sdcard / MyVideos”);

1 个答案:

答案 0 :(得分:2)

使用File.applicationStorageDirectory.resolvePath("MyVideos/video_file.mp4");代替File.applicationStorageDirectory.resolvePath("file:///mnt/sdcard/MyVideos"); 由于安全性违规问题,开发人员只能访问ApplicationStorageDirectory而不会有任何安全风险。

同时提供文件名MyVideos/video_file.mp4而不是仅文件夹MyVideos

var file:File=File.applicationStorageDirectory.resolvePath("MyVideos/video_file.mp4");

if(file.exists)
{
    trace("file exists");
}

像,

private function downloadComplete(event:Event):void
{
    try
      {
        var file:File=File.applicationStorageDirectory.resolvePath("MyVideos/video_file.mp4");

        var ba:ByteArray  = event.target.data as ByteArray;
        var fileStream:FileStream = new FileStream();
        fileStream.addEventListener(IOErrorEvent.IO_ERROR,function(e:IOErrorEvent):void{
            progressLabel.text = "File IO Error";
        });
        fileStream.open(file, FileMode.WRITE);
        fileStream.writeBytes(ba);
        fileStream.addEventListener(Event.COMPLETE, fileClosed); 
        fileStream.close(); 
        progressLabel.text = "Download Sucessful";
        trace(file.nativePath); //Where file actually stored
    }
    catch(eeee){
        progressLabel.text = "Error";
    }
}

在编写大型文件时,如写入/阅读的视频/音乐文件better use ASYNC mode,这样您的应用程序就可以在没有UI冻结的情况下工作。