首页 / JAVA / java-更新进度对话框
java-更新进度对话框
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了java-更新进度对话框,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2409字,纯文字阅读大概需要4分钟。
内容图文
我正在尝试制作一个可以帮助我评估从Web资源下载文件的时间的应用程序.我发现了2个样本:
Download a file with Android, and showing the progress in a ProgressDialog
和
http://www.helloandroid.com/tutorials/how-download-fileimage-url-your-device
第二个示例显示了较小的下载时间,但是我不明白如何使用它来更新进度对话框.我认为在第二种情况下应该使用“ while”表达来完成某些操作,但是我找不到.有人可以给我提些建议吗?
UPD:
第一个代码:
try {
time1 = System.currentTimeMillis();
URL url = new URL(path);
URLConnection conexion = url.openConnection();
conexion.connect();
// this will be useful so that you can show a tipical 0-100% progress bar
int lenghtOfFile = conexion.getContentLength();
// downlod the file
InputStream input = new BufferedInputStream(url.openStream());
OutputStream output = new FileOutputStream("/sdcard/analyzer/test.jpg");
byte data[] = new byte[1024];
long total = 0;
time11 = System.currentTimeMillis();
while ((count = input.read(data)) != -1) {
total += count;
// publishing the progress....
publishProgress((int)(total*100/lenghtOfFile));
output.write(data, 0, count);
}
time22= System.currentTimeMillis()-time11;
output.flush();
output.close();
input.close();
} catch (Exception e) {}
timetaken = System.currentTimeMillis() - time1;
第二个代码:
long time1 = System.currentTimeMillis();
DownloadFromUrl(path, "test.jpg");
long timetaken = System.currentTimeMillis() - time1;
哪里
public void DownloadFromUrl(String imageURL, String fileName) { //this is the downloader method
try {
URL url = new URL(imageURL); //you can write here any link
File file = new File(fileName);
/*Open a connection to that URL. */
URLConnection ucon = url.openConnection();
/*
* Define InputStreams to read from the URLConnection.
*/
InputStream is = ucon.getInputStream();
BufferedInputStream bis = new BufferedInputStream(is);
/*
* Read bytes to the Buffer until there is nothing more to read(-1).
*/
ByteArrayBuffer baf = new ByteArrayBuffer(50);
int current = 0;
while ((current = bis.read()) != -1) {
baf.append((byte) current);
}
/* Convert the Bytes read to a String. */
FileOutputStream fos = new FileOutputStream(PATH+file);
fos.write(baf.toByteArray());
fos.close();
} catch (IOException e) {
Log.d("ImageManager", "Error: " + e);
}
因此,问题在于第一种方法的速度似乎要慢30%.
解决方法:
second example可能运行得更快,但它垄断了GUI线程. first approach使用AsyncTask更好;它允许GUI在下载进行时保持响应.
我发现将AsyncTask与SwingWorker进行比较很有帮助,如该example所示.
内容总结
以上是互联网集市为您收集整理的java-更新进度对话框全部内容,希望文章能够帮你解决java-更新进度对话框所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。