Вопрос

I am developing an Android application using Flex and Flash Builder.
I have used the following piece of code to download a video using URLLoader and 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 :
var file:File=File.applicationStorageDirectory.resolvePath("file:///mnt/sdcard/MyVideos");

Это было полезно?

Решение

Use File.applicationStorageDirectory.resolvePath("MyVideos/video_file.mp4"); instead of File.applicationStorageDirectory.resolvePath("file:///mnt/sdcard/MyVideos"); Due security violation issue so developer only can access ApplicationStorageDirectory without any security risk.

Also give filename MyVideos/video_file.mp4 instead of folder only 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.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top