Android 大文件上傳時(shí)處理上傳進(jìn)度問題小結(jié)
進(jìn)行大文件上傳時(shí),顯示上傳進(jìn)度是很好的用戶體驗(yàn),可以有效的緩解用戶急躁的情緒。今天Android IT 分享一個(gè)好的顯示上傳進(jìn)度的解決方案。

我們用到以下兩個(gè)類就可實(shí)現(xiàn)帶進(jì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);
}
}
}
該類計(jì)算寫入的字節(jié)數(shù),我們需要在實(shí)現(xiàn)ProgressListener中的trasnfered()方法,更行進(jìn)度條
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()實(shí)現(xiàn)上傳進(jìn)度的更新操作
以上所述是小編給大家介紹的Android 大文件上傳時(shí)處理上傳進(jìn)度問題小結(jié),希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時(shí)回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
相關(guān)文章
Android App中的GridView網(wǎng)格布局使用指南
GridView布局所實(shí)現(xiàn)的就是類似于九宮格的矩陣界面效果,下面整理了Android App中的GridView網(wǎng)格布局使用指南,包括分割線的添加與自定義GridView的實(shí)現(xiàn)等技巧,需要的朋友可以參考下2016-06-06
PopupWindow?RecyclerView實(shí)現(xiàn)下拉選擇Spinner示例解析
這篇文章主要介紹了PopupWindow?RecyclerView實(shí)現(xiàn)下拉選擇Spinner示例解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-07-07
Kotlin FrameLayout與ViewPager2控件實(shí)現(xiàn)滾動廣告欄方法
這篇文章主要介紹了Kotlin FrameLayout與ViewPager2控件實(shí)現(xiàn)滾動廣告欄,F(xiàn)rameLayout與ViewPager2是Android開發(fā)中非常常見的布局組件,并且它不單單是一個(gè)幀布局組件,可以用它實(shí)現(xiàn)多種功能,感興趣的朋友一起來看看吧2022-12-12
Android 限制顯示小數(shù)點(diǎn)后兩位的實(shí)現(xiàn)方法
下面小編就為大家分享一篇Android 限制顯示小數(shù)點(diǎn)后兩位的實(shí)現(xiàn)方法,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-01-01
Android編程中避免內(nèi)存泄露的方法總結(jié)
這篇文章主要介紹了Android編程中避免內(nèi)存泄露的方法總結(jié),本文講解了最可能造成內(nèi)存泄露的幾個(gè)點(diǎn),并總結(jié)出如何應(yīng)對這些內(nèi)存泄露,需要的朋友可以參考下2014-08-08
Android中的Button自定義點(diǎn)擊效果實(shí)例代碼
Android中的Button自定義點(diǎn)擊效果實(shí)例代碼,需要的朋友可以參考一下2013-05-05
Android在fragment中編寫toobar的步驟詳解
這篇文章主要介紹了Android在fragment中編寫toobar,本文分步驟通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-01-01
Android 網(wǎng)絡(luò)圖片查看顯示的實(shí)現(xiàn)方法
本篇文章小編為大家介紹,Android 網(wǎng)絡(luò)圖片查看顯示的實(shí)現(xiàn)方法,需要的朋友參考下2013-04-04

