无法将文件保存到xamarin android上的设备

时间:2016-05-20 08:00:39

标签: xamarin xamarin.android

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 ();
            }
        }

enter image description here

下载图片成功,但我在设备上找到了它,但我看不到它。

在文档中获取emty和 下载历史记录也很容易。

我不明白。我想将它保存在设备上的照片中。 我使用三星Galaxy。

1 个答案:

答案 0 :(得分:1)

看看这个post

我相信你只能从外部存储公共目录中写入和读取。

祝你好运!