打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
Android带进度条文件上传

 Being able to display a progress bar during a time consuming upload to a web server is important when dealing with users and appeasing their impatience. Here is one approach of achieving this.

In this example we are going to use 2 classes – the first one is going to implement Android’s really handy threading function: Async Task and the other is going to extend MutlipartEntity – the basic object used for a multipart POST. Let’s take a look at extending a MultipartEntity object:


CustomMultiPartEntity.java

  1. import java.io.FilterOutputStream;  
  2. import java.io.IOException;  
  3. import java.io.OutputStream;  
  4. import java.nio.charset.Charset;  
  5. import org.apache.http.entity.mime.HttpMultipartMode;  
  6. import org.apache.http.entity.mime.MultipartEntity;  
  7.    
  8. public class CustomMultiPartEntity extends MultipartEntity  
  9. {  
  10.    
  11.     private final ProgressListener listener;  
  12.    
  13.     public CustomMultiPartEntity(final ProgressListener listener)  
  14.     {  
  15.         super();  
  16.         this.listener = listener;  
  17.     }  
  18.    
  19.     public CustomMultiPartEntity(final HttpMultipartMode mode, final ProgressListener listener)  
  20.     {  
  21.         super(mode);  
  22.         this.listener = listener;  
  23.     }  
  24.    
  25.     public CustomMultiPartEntity(HttpMultipartMode mode, final String boundary, final Charset charset, final ProgressListener listener)  
  26.     {  
  27.         super(mode, boundary, charset);  
  28.         this.listener = listener;  
  29.     }  
  30.    
  31.     @Override  
  32.     public void writeTo(final OutputStream outstream) throws IOException  
  33.     {  
  34.         super.writeTo(new CountingOutputStream(outstream, this.listener));  
  35.     }  
  36.    
  37.     public static interface ProgressListener  
  38.     {  
  39.         void transferred(long num);  
  40.     }  
  41.    
  42.     public static class CountingOutputStream extends FilterOutputStream  
  43.     {  
  44.    
  45.         private final ProgressListener listener;  
  46.         private long transferred;  
  47.    
  48.         public CountingOutputStream(final OutputStream out, final ProgressListener listener)  
  49.         {  
  50.             super(out);  
  51.             this.listener = listener;  
  52.             this.transferred = 0;  
  53.         }  
  54.    
  55.         public void write(byte[] b, int off, int len) throws IOException  
  56.         {  
  57.             out.write(b, off, len);  
  58.             this.transferred += len;  
  59.             this.listener.transferred(this.transferred);  
  60.         }  
  61.    
  62.         public void write(int b) throws IOException  
  63.         {  
  64.             out.write(b);  
  65.             this.transferred++;  
  66.             this.listener.transferred(this.transferred);  
  67.         }  
  68.     }  
  69. }  

By simply counting the amount of bytes that are written, we can implement an interface (here we called it trasnfered())which can be called in our main class to update our progress bar dialog box:

Main.java

  1. class HttpMultipartPost extends AsyncTask<HttpResponse, Integer, TypeUploadImage>  
  2.     {  
  3.         ProgressDialog pd;  
  4.         long totalSize;  
  5.    
  6.         @Override  
  7.         protected void onPreExecute()  
  8.         {  
  9.             pd = new ProgressDialog(this);  
  10.             pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);  
  11.             pd.setMessage("Uploading Picture...");  
  12.             pd.setCancelable(false);  
  13.             pd.show();  
  14.         }  
  15.    
  16.         @Override  
  17.         protected TypeUploadImage doInBackground(HttpResponse... arg0)  
  18.         {  
  19.             HttpClient httpClient = new DefaultHttpClient();  
  20.             HttpContext httpContext = new BasicHttpContext();  
  21.             HttpPost httpPost = new HttpPost("http://herpderp.com/UploadImage.php");  
  22.    
  23.             try  
  24.             {  
  25.                 CustomMultipartEntity multipartContent = new CustomMultipartEntity(new ProgressListener()  
  26.                 {  
  27.                     @Override  
  28.                     public void transferred(long num)  
  29.                     {  
  30.                         publishProgress((int) ((num / (float) totalSize) * 100));  
  31.                     }  
  32.                 });  
  33.    
  34.                 // We use FileBody to transfer an image  
  35.                 multipartContent.addPart("uploaded_file", new FileBody(new File(m_userSelectedImagePath)));  
  36.                 totalSize = multipartContent.getContentLength();  
  37.    
  38.                 // Send it  
  39.                 httpPost.setEntity(multipartContent);  
  40.                 HttpResponse response = httpClient.execute(httpPost, httpContext);  
  41.                 String serverResponse = EntityUtils.toString(response.getEntity());  
  42.    
  43.                 ResponseFactory rp = new ResponseFactory(serverResponse);  
  44.                 return (TypeImage) rp.getData();  
  45.             }  
  46.    
  47.             catch (Exception e)  
  48.             {  
  49.                 System.out.println(e);  
  50.             }  
  51.             return null;  
  52.         }  
  53.    
  54.         @Override  
  55.         protected void onProgressUpdate(Integer... progress)  
  56.         {  
  57.             pd.setProgress((int) (progress[0]));  
  58.         }  
  59.    
  60.         @Override  
  61.         protected void onPostExecute(TypeUploadImage ui)  
  62.         {  
  63.             pd.dismiss();  
  64.         }  

原文


本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
Servlet中的过滤器(拦截器)Filter与监听器Listener的作用和区别
java web 文件上传与下载
Android开发之监听软键盘状态(弹出收回)
Android 浅谈Sensor工作流程(一)
在Android上打开键盘时如何隐藏广告横幅
谈谈我对Java中CallBack的理解
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服