dooplaye
dooplaye

Reputation: 1029

Showing download progress

I did read similar questions ,but nothing works for me . The problem is method getProgress() returns very big value (such as 16300 ).What did i do wrong?

see my code

 public void download()
{
    RandomAccessFile file = null;
    InputStream inputStream = null;
    HttpURLConnection connection = null;
    try
    {
        connection = (HttpURLConnection)url.openConnection();
        connection.setRequestMethod("GET");
        connection.setDoOutput(true);
        connection.connect();
        String connectLength = connection.getHeaderField("Content-Length");

        if(connectLength == null || Integer.parseInt(connectLength) < 1 || connectLength.equals(""))
            error();

        size = Long.parseLong(connectLength);
        file = new RandomAccessFile(filePath,"rw");
        inputStream = new BufferedInputStream(url.openStream());
        int read ;

        while((read = inputStream.read(buffer)) > 0 && status == States.DOWNLOADING)
        {
            file.write(buffer,0,read);
            downloadedS += read;
        }
    }
}
 public float getProgress()
    { 
        return ((float)downloadedS/size)*100;
    }

Upvotes: 0

Views: 161

Answers (1)

resource8218
resource8218

Reputation: 1485

AsyncTask enables proper and easy use of the UI thread. This class allows to perform background operations and publish results on the UI thread without having to manipulate threads and/or handlers. AsyncTask

Upvotes: 1

Related Questions