2014-01-07 13 views
0

我正在開發一個使用Flex和Flash Builder的Android應用程序。
我已經使用下面的一段代碼來使用URLLoader和FileStream下載視頻。Flex Mobile Missing Downloaded File

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測試,它顯示下載成功,但該文件無法在dirctory發現:
var文件:文件= File.applicationStorageDirectory.resolvePath(「文件:/// MNT/SD卡/ MyVideos「);

回答

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凍結。

+0

哇。謝謝。但是,有沒有辦法將下載的MP4存儲到SD卡? –

+1

是的,它是可能的,你需要改變應用程序的XML描述符文件,如<使用許可權的android:NAME =「android.permission.WRITE_EXTERNAL_STORAGE」 />之後,你可以寫入SD卡的詳細信息的http://理查德 - 赫克.blogspot.in/2011/01/how-to-write-file-to-android-filesystem.html –

+0

作爲一種魅力。非常感謝。 –