gpt4 book ai didi

android - android中的文件下载进度条

转载 作者:太空宇宙 更新时间:2023-11-03 13:36:53 26 4
gpt4 key购买 nike

如何在android中的进度条中显示剩余KB的文件下载。例如剩余 12kb/120kb..然后 97kb/120kb...等等

我们能否拥有如图所示的进度对话框enter image description here

最佳答案

class DownloadFileAsync extends AsyncTask<String, String, String> {
private ProgressDialog mProgressDialog;
@Override
protected void onPreExecute() {
super.onPreExecute();
mProgressDialog = new ProgressDialog(UrlTestActivity.this);
mProgressDialog.setMessage("Downloading file..");
mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
mProgressDialog.setCancelable(false);
mProgressDialog.show();
}

@Override
protected String doInBackground(String... aurl) {
int count;
try {

for (int i = 0; i < 3; i++) {
URL url = new URL("http://nodeload.github.com/nexes/Android-File-Manager/zipball/master");
URLConnection conexion = url.openConnection();
conexion.connect();
int lenghtOfFile = conexion.getContentLength();
InputStream is = url.openStream();
File testDirectory = new File(Environment.getExternalStorageDirectory() + "/Folder");
if (!testDirectory.exists()) {
testDirectory.mkdir();
}
FileOutputStream fos = new FileOutputStream(testDirectory+ "/"+(i+100)+".zip");
byte data[] = new byte[1024];
long total = 0;
int progress = 0;
while ((count = is.read(data)) != -1) {
total += count;
int progress_temp = (int) total * 100 / lenghtOfFile;
publishProgress(""+ progress_temp);
if (progress_temp % 10 == 0 && progress != progress_temp) {
progress = progress_temp;
}
fos.write(data, 0, count);
}
is.close();
fos.close();
}
} catch (Exception e) {}
return null;

}
protected void onProgressUpdate(String... progress) {
Log.d("ANDRO_ASYNC",progress[0]);
mProgressDialog.setProgress(Integer.parseInt(progress[0]));
}

@Override
protected void onPostExecute(String unused) {
dismissDialog(DIALOG_DOWNLOAD_PROGRESS);
}
}

关于android - android中的文件下载进度条,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5938198/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com