简体   繁体   English

Flex Mobile缺少下载的文件

[英]Flex Mobile Missing Downloaded File

I am developing an Android application using Flex and Flash Builder. 我正在使用Flex和Flash Builder开发Android应用程序。
I have used the following piece of code to download a video using URLLoader and FileStream. 我使用以下代码使用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";
        }

When tested using Motorola Xoom, it shows download successful but the file can't be found in the dirctory : 使用Motorola Xoom进行测试时,它显示下载成功但文件无法在目录中找到:
var file:File=File.applicationStorageDirectory.resolvePath("file:///mnt/sdcard/MyVideos"); var file:File = File.applicationStorageDirectory.resolvePath(“file:/// mnt / sdcard / MyVideos”);

Use File.applicationStorageDirectory.resolvePath("MyVideos/video_file.mp4"); 使用File.applicationStorageDirectory.resolvePath("MyVideos/video_file.mp4"); instead of File.applicationStorageDirectory.resolvePath("file:///mnt/sdcard/MyVideos"); 而不是File.applicationStorageDirectory.resolvePath("file:///mnt/sdcard/MyVideos"); Due security violation issue so developer only can access ApplicationStorageDirectory without any security risk. 由于安全性违规问题,开发人员只能访问ApplicationStorageDirectory而不存在任何安全风险。

Also give filename MyVideos/video_file.mp4 instead of folder only MyVideos . 同时给出文件名MyVideos/video_file.mp4而不是仅文件夹MyVideos

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

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

Like, 喜欢,

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";
    }
}

When writing large files like Video/music file better use ASYNC mode of writing/reading so your application works without UI freeze. 在编写大型文件(如视频/音乐文件)时, better use ASYNC mode写入/读取better use ASYNC mode ,这样您的应用程序就可以在没有UI冻结的情

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM