最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android 大文件上傳時(shí)處理上傳進(jìn)度問(wèn)題小結(jié)

 更新時(shí)間:2017年04月23日 08:50:12   作者:瞎_扯D  
進(jìn)行大文件上傳時(shí),顯示上傳進(jìn)度給用戶(hù)帶來(lái)極好的用戶(hù)體驗(yàn)度,今天小編給大家分享Android 大文件上傳時(shí)處理上傳進(jìn)度問(wèn)題小結(jié),需要的的朋友參考下吧

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

我們用到以下兩個(gè)類(lèi)就可實(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); 
    } 
  } 
} 

該類(lèi)計(jì)算寫(xiě)入的字節(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)度問(wèn)題小結(jié),希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!

相關(guān)文章

最新評(píng)論

仪陇县| 揭西县| 永平县| 琼结县| 和龙市| 乐平市| 南阳市| 周口市| 老河口市| 三台县| 堆龙德庆县| 锡林浩特市| 舒城县| 乳山市| 高青县| 永兴县| 鲁甸县| 平果县| 嘉兴市| 泾阳县| 贵德县| 临泽县| 横山县| 濮阳市| 井陉县| 丰都县| 通化市| 尼木县| 利辛县| 延长县| 漠河县| 襄城县| 伊宁市| 锡林浩特市| 桃江县| 宜丰县| 防城港市| 商都县| 迁西县| 惠来县| 抚顺县|