国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統(tǒng) > Android > 正文

Android 大文件上傳時處理上傳進度問題小結(jié)

2019-10-23 18:30:39
字體:
供稿:網(wǎng)友

進行大文件上傳時,顯示上傳進度是很好的用戶體驗,可以有效的緩解用戶急躁的情緒。今天Android IT 分享一個好的顯示上傳進度的解決方案。

android文件上傳進度條,android,上傳進度

我們用到以下兩個類就可實現(xiàn)帶進度條的文件上傳:

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);     }   } } 

該類計算寫入的字節(jié)數(shù),我們需要在實現(xiàn)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()函數(shù)中調(diào)用publishProgress((int) ((num / (float) totalSize) * 100));

在onProgressUpdate()實現(xiàn)上傳進度的更新操作

以上所述是小編給大家介紹的Android 大文件上傳時處理上傳進度問題小結(jié),希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對VEVB武林網(wǎng)網(wǎng)站的支持!


注:相關(guān)教程知識閱讀請移步到Android開發(fā)頻道。
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 香格里拉县| 庆安县| 岐山县| 安塞县| 海口市| 阿尔山市| 修武县| 连南| 卢湾区| 绥滨县| 聂荣县| 沽源县| 乌海市| 定南县| 开鲁县| 万山特区| 泸州市| 治多县| 天峻县| 元江| 梅河口市| 蛟河市| 兴安盟| 江达县| 曲麻莱县| 安新县| 凌海市| 海城市| 大名县| 旬阳县| 商南县| 阳新县| 通城县| 平阳县| 黑河市| 新乡市| 九龙县| 道真| 磐石市| 兴国县| 东阳市|