Android 大文件上传时处理上传进度问题小结
程序员文章站
2023-11-13 13:01:28
进行大文件上传时,显示上传进度是很好的用户体验,可以有效的缓解用户急躁的情绪。今天android it 分享一个好的显示上传进度的解决方案。
我们用到以下两个类就可实...
进行大文件上传时,显示上传进度是很好的用户体验,可以有效的缓解用户急躁的情绪。今天android it 分享一个好的显示上传进度的解决方案。
我们用到以下两个类就可实现带进度条的文件上传:
1、custommultipartentity extends multipartentity,
2、httpmultipartpost extends asynctask
import java.io.filteroutputstream; import java.io.ioexception; import java.io.outputstream; import java.nio.charset.charset; import org.apache.http.entity.mime.httpmultipartmode; import org.apache.http.entity.mime.multipartentity; public class custommultipartentity extends multipartentity { private final progresslistener listener; public custommultipartentity(final progresslistener listener) { super(); this.listener = listener; } public custommultipartentity(final httpmultipartmode mode, final progresslistener listener) { super(mode); this.listener = listener; } public custommultipartentity(httpmultipartmode mode, final string boundary, final charset charset, final progresslistener listener) { super(mode, boundary, charset); this.listener = listener; } @override public void writeto(final outputstream outstream) throws ioexception { super.writeto(new countingoutputstream(outstream, this.listener)); } public static interface progresslistener { void transferred(long num); } public static class countingoutputstream extends filteroutputstream { private final progresslistener listener; private long transferred; public countingoutputstream(final outputstream out, final progresslistener listener) { super(out); this.listener = listener; this.transferred = 0; } public void write(byte[] b, int off, int len) throws ioexception { out.write(b, off, len); this.transferred += len; this.listener.transferred(this.transferred); } public void write(int b) throws ioexception { out.write(b); this.transferred++; this.listener.transferred(this.transferred); } } }
该类计算写入的字节数,我们需要在实现progresslistener中的trasnfered()方法,更行进度条
public class httpmultipartpost extends asynctask<httpresponse, integer, typeuploadimage> { progressdialogpd; longtotalsize; @override protectedvoidonpreexecute(){ pd= newprogressdialog(this); pd.setprogressstyle(progressdialog.style_horizontal); pd.setmessage("uploading picture..."); pd.setcancelable(false); pd.show(); } @override protectedtypeuploadimagedoinbackground(httpresponse... arg0) { httpclienthttpclient = newdefaulthttpclient(); httpcontexthttpcontext = newbasichttpcontext(); httpposthttppost = newhttppost("http://herpderp.com/uploadimage.php"); try{ custommultipartentitymultipartcontent = newcustommultipartentity( newprogresslistener() { @override public void transferred(longnum){ publishprogress((int) ((num / (float) totalsize) * 100)); } }); // we use filebody to transfer an image multipartcontent.addpart("uploaded_file", newfilebody( newfile(m_userselectedimagepath))); totalsize= multipartcontent.getcontentlength(); // send it httppost.setentity(multipartcontent); httpresponseresponse = httpclient.execute(httppost, httpcontext); string serverresponse = entityutils.tostring(response.getentity()); responsefactoryrp = newresponsefactory(serverresponse); return(typeimage) rp.getdata(); } catch(exception e) { system.out.println(e); } return null; } @override protectedvoidonprogressupdate(integer... progress){ pd.setprogress((int) (progress[0])); } @override protectedvoidonpostexecute(typeuploadimageui) { pd.dismiss(); } }
在 transferred()函数中调用publishprogress((int) ((num / (float) totalsize) * 100));
在onprogressupdate()实现上传进度的更新操作
以上所述是小编给大家介绍的android 大文件上传时处理上传进度问题小结,希望对大家有所帮助
上一篇: 深入PHP变量存储的详解
下一篇: 用Python代码来绘制彭罗斯点阵的教程
推荐阅读
-
Android 大文件上传时处理上传进度问题小结
-
PHP文件上传问题汇总(文件大小检测、大文件上传处理)
-
利用Plupload.js解决大文件上传问题, 带进度条和背景遮罩层
-
Android 大文件上传时处理上传进度问题小结
-
PHP文件上传问题汇总(文件大小检测、大文件上传处理),_PHP教程
-
PHP文件上传问题汇总(文件大小检测、大文件上传处理)
-
PHP文件上传问题汇总(文件大小检测、大文件上传处理)_PHP
-
PHP文件上传问题汇总(文件大小检测、大文件上传处理)_PHP
-
PHP文件上传问题汇总(文件大小检测、大文件上传处理)_php技巧
-
PHP文件上传问题汇总(文件大小检测、大文件上传处理)_php技巧