2016-05-20 9 views
1
async void ImgDownload_Click (object sender, EventArgs e) 
    { 
     var webClient=new WebClient(); 
    var url=new Uri(stream_url_soundcloud); 
    byte[] bytes=null; 


    webClient.DownloadProgressChanged+= WebClient_DownloadProgressChanged; 
    dialog = new ProgressDialog (this); 
    dialog.SetProgressStyle (ProgressDialogStyle.Horizontal); 
    dialog.SetTitle("Downloading..."); 
    dialog.SetCancelable (false); 
    //dialog.SetButton("Cancel",); 
    dialog.SetCanceledOnTouchOutside (false); 
    dialog.Show(); 

    try 
    { 
     bytes= await webClient.DownloadDataTaskAsync(url); 
    } 
    catch(TaskCanceledException) 
    { 
     Toast.MakeText(this,"Task Canceled!",ToastLength.Long); 
     return; 
    } 
    catch(Exception a) 
    { 
     Toast.MakeText(this,a.InnerException.Message,ToastLength.Long); 
     dialog.Progress=0; 
     return; 
    } 

    Java.IO.File documentsPath= new Java.IO.File(Android.OS.Environment.GetExternalStoragePublicDirectory(Android.OS.Environment.DirectoryMusic),"MusicDownloaded"); 
    string localFilename = documentsPath + mListData[mPosition].track.title+".mp3"; 
    //string localPath=System.IO.Path.Combine(documentsPath,localFilename); 
    Java.IO.File localPath=new Java.IO.File(documentsPath,localFilename); 

    dialog.SetTitle("Download Complete"); 

    //Save the Mp3 using writeAsync 
    //FileStream fs=new FileStream(localPath,FileMode.OpenOrCreate); 
    OutputStream fs=new FileOutputStream(localPath); 
    await fs.WriteAsync (bytes, 0, bytes.Length); 

    fs.Close(); 


    dialog.Progress = 0; 
    } 

    void WebClient_DownloadProgressChanged (object sender, DownloadProgressChangedEventArgs e) 
    { 
     dialog.Progress = e.ProgressPercentage; 
     if (e.ProgressPercentage == 100) { 
      //dialog.Hide(); 
     } 
    } 


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

私はSamsung Galaxyでテストしていますが、FIle.WriteAllBytesでエラーが表示されます。 方向が見つかりません......xamarin androidでmp3ファイルをダウンロードするには?

フォルダーのデバイスはMyAudioです。 MyMusicではありません。 - mymusicをmydocumentに置き換えようとしました。エラーは表示されませんが、約3分待ってからデバイスのmydocumentに入ります。> mp3ファイルは表示されません。

答えて

2

DownloadFileTaskAsyncメソッドを利用して)これに似た何かを試してみてください:

 button.Click += async delegate 
     { 
      var destination = Path.Combine(
       System.Environment.GetFolderPath(
        System.Environment.SpecialFolder.ApplicationData), 
        "music.mp3"); 

      await new WebClient().DownloadFileTaskAsync(
       new Uri("http://www.xyzmusic.com/music.mp3"), 
       destination); 
     }; 

参照してください:https://stackoverflow.com/a/22420894/3891036

+0

ダウンロードの完了が、私は –

+0

を参照してください私のデバイス上でそれを見ることはできません。 - http://blog.cindypotvin.com/saving-data-to-a-file-あなたのアンドロイドアプリケーション/ - http://developer.android.com/reference/android/os/Environment.html#getExternalStorageDirectory() – Vaikesh

+0

ファイルは、ドキュメントの共通リポジトリとして機能するディレクトリに存在します。 – Vaikesh

1

は、パスに問題があるだろうか? 私のビット動作していること:

  public async Task<string> SaveFilePublicStorage(string filename, byte[] fileBody) 
     { 
      var documentsPath = Android.OS.Environment.ExternalStorageDirectory + "/download/"; 
      var filePath = Path.Combine(documentsPath, filename); 
      File.WriteAllBytes(filePath, fileBody); 
      return filePath; 
     } 
+0

ありがとうございました。 –

関連する問題