ProgressDialog不能很好地工作

时间:2017-08-07 06:35:45

标签: c# xamarin.android

你好我从服务器获取zip文件,我已经为它编写了代码。

我正在展示progressdialog

这是代码

public class DownloadImageFromUrl : AsyncTask<string, string, string>
{
    private ProgressDialog pd;
    private Context context;

    public DownloadImageFromUrl(Context context)
    {
        this.context = context;
    }

    protected override void OnPreExecute()
    {
        pd = new ProgressDialog(context);

        pd.SetMessage("Downloading file. Please wait...");

        pd.Indeterminate = false;

        pd.Max = 100;

        pd.SetProgressStyle(ProgressDialogStyle.Horizontal);

        pd.SetCancelable(true);

        pd.Show();

        base.OnPreExecute();

    }

    protected override void OnProgressUpdate(params string[] values)
    {
        base.OnProgressUpdate(values);

        pd.SetProgressNumberFormat(values[0]);
        Log.Verbose("values", "" + values[0]);
        pd.Progress = int.Parse(values[0]);

    }

    protected override void OnPostExecute(string result)
    {
        pd.Dismiss();
    }

    protected override string RunInBackground(params string[] @params)
    {

        var storagePath = Android.OS.Environment.GetExternalStoragePublicDirectory(Android.OS.Environment.DirectoryDownloads);
        string filePath = System.IO.Path.Combine(storagePath.AbsolutePath, "testfile.zip");
        int count;

        try
        {                    
            URL url = new URL("http://shadikidukan.co.in/abhi/AAA.zip");
            filename = Path.GetFileName(new Uri(url.ToString()).AbsolutePath);
            System.Console.WriteLine("Abhijir" + filename);
            URLConnection connection = url.OpenConnection();

            connection.Connect();

            int LengthOfFile = connection.ContentLength;//Here connection.ContentLength return -1
            Log.Verbose("length", ""+LengthOfFile);
            InputStream input = new BufferedInputStream(url.OpenStream(), 8192);

            OutputStream output = new FileOutputStream(filePath);

            byte[] data = new byte[1024];
            long total = 0;

                while ((count = input.Read(data)) != -1)

                {

                    total += count;
                    PublishProgress("" + (int)((total / 100) / LengthOfFile));
                    output.Write(data, 0, count);
                }

                output.Flush();

                output.Close();

                input.Close();

            }



            catch (Exception e)

            {

                System.Console.WriteLine("Abhijir"+e.Message);
            }
            return null;

        }

    }

connection.ContentLength返回-1

当文件下载时,它显示负值,进度不会递增..

在progressupdate方法中,它总是得到负值

protected override void OnProgressUpdate(params string[] values)
{
    base.OnProgressUpdate(values);

    pd.SetProgressNumberFormat(values[0]);
    Log.Verbose("values", "" + values[0]);//values[0] has negative values
    pd.Progress = int.Parse(values[0]);

}

如何克服该问题并在progressdialog中显示正确的值。

1 个答案:

答案 0 :(得分:1)

内容长度为-1表示标题丢失或应忽略。这通常意味着服务器正在使用chunked transfer encoding。根据设计,您无法知道您尝试下载的文件的总长度,因此无法显示进度指示器。

尝试在桌面浏览器中下载该文件,您将看到在传输结束之前总长度不会显示。