欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android 大文件上传时处理上传进度问题小结

程序员文章站 2023-11-13 13:01:28
进行大文件上传时,显示上传进度是很好的用户体验,可以有效的缓解用户急躁的情绪。今天android it 分享一个好的显示上传进度的解决方案。 我们用到以下两个类就可实...

进行大文件上传时,显示上传进度是很好的用户体验,可以有效的缓解用户急躁的情绪。今天android it 分享一个好的显示上传进度的解决方案。

Android 大文件上传时处理上传进度问题小结

我们用到以下两个类就可实现带进度条的文件上传:

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 大文件上传时处理上传进度问题小结,希望对大家有所帮助