2016-06-11 60 views
0

目前,我試圖下載我在亞馬遜S3上的存儲桶中的文件。我正在使用這段代碼進行調試,因爲當我試圖從文件中讀取文件時,總是會找不到EN0ENT /文件。從Android的亞馬遜S3下載對象使文件無法讀取

 String str_FilePathInDevice = "/sdcard/" + "/" 
       + "RestoreFolderName" + "/" + "filname.extention"; 

     File file = new File(str_FilePathInDevice); 

     String str_Path = file.getPath().replace(file.getName(), ""); 
     File filedir = new File(str_Path); 

     try { 
      filedir.mkdirs(); 
      file.createNewFile(); 
     } catch (Exception ex1) { 
     } 

     System.out.println(file.toString()); 
     System.out.println(file.canRead()); 
     System.out.println(file.length()); 

     TransferObserver observer2 = transferUtility.download(
       "arabianbucket",  /* The bucket to upload to */ 
       "demo.txt",  /* The key for the uploaded object */ 
       file  /* The file where the data to upload exists*/ 
     ); 

     System.out.println(file.toString()); 
     System.out.println(file.canRead()); 
     System.out.println(file.length()); 

file.canread()最終在transferutility.download鏈接後從true到false翻轉。我不確定爲什麼會這樣。有誰知道如何成功讀取亞馬遜S3中的對象?在我的清單中,我已經翻轉了權限:

<uses-permission android:name="android.permission.INTERNET" /> 
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 

謝謝!

+0

可能想與此用戶聊天 - http://stackoverflow.com/questions/37768060/android-downloading-from-s3-aws –

+0

你正在創建的文件的路徑是什麼?你應該使用'getExternalStorage()'來獲得你想要的文件夾 –

+0

@ cricket_007我試過getExternalStorage,但我似乎仍然無法打開文件。 –

回答

0

TransferUtility提供異步傳輸。在您的代碼transferUtility.download中啓動下載並下載發生在後臺線程中,以免阻塞主線程。在傳輸完成之前,我不推薦任何文件操作。您可以將偵聽器附加到觀察者,並監聽狀態和進度更改。有關更多信息,請參閱Store and Retrieve Files with Amazon S3