【问题标题】:Flex Mobile Missing Downloaded FileFlex Mobile 缺少下载的文件
【发布时间】:2014-01-24 19:18:15
【问题描述】:

我正在使用 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";
        }

使用 Motorola Xoom 测试时,显示下载成功,但在目录中找不到文件:
var file:File=File.applicationStorageDirectory.resolvePath("file:///mnt/ sdcard/MyVideos");

【问题讨论】:

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


    【解决方案1】:

    使用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 冻结的情况下工作。

    【讨论】:

    • 哇。谢谢。但是,有没有办法将下载的 mp4 存储到 SD 卡中?
    • 是的,您可能需要更改应用程序 XML 描述符文件,如 之后您可以写入 SD 卡更多详细信息richard-heck.blogspot.in/2011/01/…
    • 作为一个魅力。非常感谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多