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
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);
}
}
}
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
class HttpMultipartPost extends AsyncTask<HttpResponse, Integer, TypeUploadImage>
{
ProgressDialog pd;
long totalSize;
@Override
protected void onPreExecute()
{
pd = new ProgressDialog(this);
pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
pd.setMessage("Uploading Picture...");
pd.setCancelable(false);
pd.show();
}
@Override
protected TypeUploadImage doInBackground(HttpResponse... arg0)
{
HttpClient httpClient = new DefaultHttpClient();
HttpContext httpContext = new BasicHttpContext();
HttpPost httpPost = new HttpPost("http://herpderp.com/UploadImage.php");
try
{
CustomMultipartEntity multipartContent = new CustomMultipartEntity(new ProgressListener()
{
@Override
public void transferred(long num)
{
publishProgress((int) ((num / (float) totalSize) * 100));
}
});
// We use FileBody to transfer an image
multipartContent.addPart("uploaded_file", new FileBody(new File(m_userSelectedImagePath)));
totalSize = multipartContent.getContentLength();
// Send it
httpPost.setEntity(multipartContent);
HttpResponse response = httpClient.execute(httpPost, httpContext);
String serverResponse = EntityUtils.toString(response.getEntity());
ResponseFactory rp = new ResponseFactory(serverResponse);
return (TypeImage) rp.getData();
}
catch (Exception e)
{
System.out.println(e);
}
return null;
}
@Override
protected void onProgressUpdate(Integer... progress)
{
pd.setProgress((int) (progress[0]));
}
@Override
protected void onPostExecute(TypeUploadImage ui)
{
pd.dismiss();
}
原文