1 回答
TA贡献1784条经验 获得超8个赞
我在下面提供了代码片段,您可以查看它。基本上,您必须计算字节数,然后通过深入到 100 得出某个百分比来进行计算。
InputStream inputStream = util.getInputStream();
// opens an output stream to save into file
FileOutputStream outputStream = new FileOutputStream(saveFilePath);
byte[] buffer = new byte[BUFFER_SIZE];
int bytesRead = -1;
long totalBytesRead = 0;
int percentCompleted = 0;
long fileSize = util.getContentLength();
while ((bytesRead = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, bytesRead);
totalBytesRead += bytesRead;
percentCompleted = (int) (totalBytesRead * 100 / fileSize);
setProgress(percentCompleted);
添加回答
举报