开发者

how to display that how much data we have downloaded

开发者 https://www.devze.com 2023-03-09 18:36 出处:网络
I anm new to android.I am making an application in which i want to display the size of file that is downloaded from server.

I anm new to android.I am making an application in which i want to display the size of file that is downloaded from server. Is there any开发者_高级运维 way to display that how much we have downloaded? Plz

if anyone can help out.

Thanks


I hope you are storing the the data whcih you get from server in a file in any particular location. So create a file passinb the absolute path of the location. use length() method to get the file size. In this way you can get how much data you have downloaded from server

File f = new File("absolutepath");
    if(f.exists()){
    f.length()
    }

Thanks Deepak


Hey there. You may use the code below or preferred in async task, where in onProgressUpdate method you may check the size from the input source variable.

URL u = new URL("http://www.java2s.com/binary.dat");
    URLConnection uc = u.openConnection();
    String contentType = uc.getContentType();
    int contentLength = uc.getContentLength();
    if (contentType.startsWith("text/") || contentLength == -1) {
      throw new IOException("This is not a binary file.");
    }
    InputStream raw = uc.getInputStream();
    InputStream in = new BufferedInputStream(raw);
    byte[] data = new byte[contentLength];
    int bytesRead = 0;
    int offset = 0;
    while (offset < contentLength) {
      bytesRead = in.read(data, offset, data.length - offset);
      if (bytesRead == -1)
        break;
      offset += bytesRead;
    }
    in.close();


Just go through the documentation below, which explains async task. In its progress update u can check size of input sorce file. If you want to show this to UI then use progress dialog.

ProgressDialog

0

精彩评论

暂无评论...
验证码 换一张
取 消