
进行大文件上传时,显示上传进度是很好的用户体验,可以有效的缓解用户急躁的情绪。今天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 大文件上传时处理上传进度问题小结,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对编程小技巧网站的支持!
总结以上是内存溢出为你收集整理的Android 大文件上传时处理上传进度问题小结全部内容,希望文章能够帮你解决Android 大文件上传时处理上传进度问题小结所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)