2017-01-28 17 views
0

ローカルホストサーバから.mp3ファイルをダウンロードしたいが、私がダウンロードしているディレクトリは唯一の問題だと思う。コードにエラーはありませんが、if(file.Exists())は常にfalseを返しています。ファイルが適切にダウンロードされていないようです。ファイルのロードサーバから.mp3ファイルをダウンロードしてMediaPlayerにロード

if (isConnectedToInternet()) 
     { 
      using (var client = new WebClient()) 
      { 
       int numberFile = 1; 
       ProgressDialog pd = new ProgressDialog(Activity); 
       pd.SetCancelable(true); 
       pd.SetMessage("Pleasy wait for files to be downloaded... 0/16"); 
       pd.Show(); 
       client.DownloadFileCompleted += (o, s) => { 
        Toast.MakeText(Activity, "Download file completed.", ToastLength.Long).Show(); 
       }; 
       try 
       { 


         client.DownloadFileCompleted += (o, s) => { 
          if (numberFile == 1) 
          { 
           pd.Cancel(); 
          } 
         }; 

         string appDataDir = System.Environment.GetFolderPath(System.Environment.SpecialFolder.ApplicationData); 
         string filePath = soundListViewAdapter.GetItemAtPosition(e.Position).path1; 
         if (!Directory.Exists(appDataDir)) 
         { 
          Directory.CreateDirectory(appDataDir); 
         } 

         //I have to do .Remove(0,1) because filePath starts with the '/' 

         string path = Path.Combine(appDataDir, filePath.Remove(0, 1)); 

         Toast.MakeText(Activity, path, ToastLength.Long).Show(); 
         System.Uri url = new System.Uri(server + "rpad/api" + soundListViewAdapter.GetItemAtPosition(e.Position).path1); 
         client.DownloadFileAsync(url, path); 


       } 
       catch 
       { 
        Toast.MakeText(Activity, "Files are not downloaded", ToastLength.Long); 
       } 

      } 
     } 
     else 
     { 
      Toast.MakeText(Activity, "No connection", ToastLength.Long).Show(); 
     } 

ファイルをダウンロードする(file.Existsは())は常にfalseを返している場合

m1 = new MediaPlayer(); 

      string appDataDir = System.Environment.GetFolderPath(System.Environment.SpecialFolder.ApplicationData); 
      string filePath = prefs.GetString("path1", "empty"); 
      string path = Path.Combine(appDataDir, filePath.Remove(0, 1)); 
      Java.IO.File file = new Java.IO.File(path); 
      if (file.Exists()) 
      { 
       FileInputStream fileStream = new FileInputStream(file); 
       m1.SetDataSource(fileStream.FD); 
       m1.Prepare(); 
       m1.Start(); 
      } 
+0

'Directory.CreateDirectory(appDataDir);'。ディレクトリの作成に失敗する可能性があるため、戻り値を確認してください。その場合、そのように言っているユーザーにトーストを表示してください。そして戻る。既存のディレクトリ以外にファイルをダウンロードするのは意味をなさないので、コードを続行しないでください。 – greenapps

答えて

0

コードがエラーを与えたがでていないが、それをファイルが適切にダウンロードされていないようです。

Java.IO.File file = new Java.IO.File(path);は、Fileインスタンスのみを作成しています。ファイルはデバイス上に作成されていません。あなたはこのファイルを作成するためにFile.CreateNewFileを呼び出し、その前に、すべての親フォルダがDirectory.CreateDirectoryを使用して作成されていることを確認する必要があります。

string appDataDir = System.Environment.GetFolderPath(System.Environment.SpecialFolder.ApplicationData); 
string filePath = "Test/empty/abc.txt"; 
string parentPath = Path.Combine(appDataDir, "Test/empty"); 
string path = Path.Combine(appDataDir, filePath); 
Java.IO.File file = new Java.IO.File(path); 
Directory.CreateDirectory(parentPath);//make sure the parent directory is created 
file.CreateNewFile();//create the file 
if (file.Exists()) 
{ 
    ... 
} 
関連する問題