我正在尝试在将文件上传到服务器时显示进度对话框。问题是文件上传时对话框显示0%然后被解除。
public class Complete_Bucket extends AsyncTask<String, Integer, List<String>> {
@Override
protected void onPostExecute(final List<String> result) {
prgDialog.dismiss();
}
@Override
protected void onPreExecute() {
complete_item.setEnabled(false);
prgDialog.show();
}
@Override
protected List<String> doInBackground(String... params) {
String fileName = params[1];
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(complete_bucket);
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
length = 0;
Bitmap bitmapSelectedImage;
try {
if (!fileName.equals("")) {
bitmapSelectedImage = getSampleBitmapFromFile(fileName, 400, 400);
ExifInterface exifInterface = new ExifInterface(fileName);
int orientation = exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
int rotationInDegrees = exifToDegrees(orientation);
Matrix matrix = new Matrix();
if (rotationInDegrees != 0f) {
matrix.preRotate(rotationInDegrees);
bitmapSelectedImage = Bitmap.createBitmap(bitmapSelectedImage, 0, 0, bitmapSelectedImage.getWidth(), bitmapSelectedImage.getHeight(), matrix, true);
}
ByteArrayOutputStream bos = new ByteArrayOutputStream();
bitmapSelectedImage.compress(CompressFormat.JPEG, 100, bos);
ContentBody mimePart = new ByteArrayBody(bos.toByteArray(), fileName);
builder.addPart("file",mimePart);
byte[] imageInByte = bos.toByteArray();
length = imageInByte.length;
}
builder.addTextBody("userID", userid);
final HttpEntity yourEntity = builder.build();
class ProgressiveEntity implements HttpEntity {
@Override
public void consumeContent() throws IOException {
yourEntity.consumeContent();
}
@Override
public InputStream getContent() throws IOException,
IllegalStateException {
return yourEntity.getContent();
}
@Override
public Header getContentEncoding() {
return yourEntity.getContentEncoding();
}
@Override
public long getContentLength() {
return yourEntity.getContentLength();
}
@Override
public Header getContentType() {
return yourEntity.getContentType();
}
@Override
public boolean isChunked() {
return yourEntity.isChunked();
}
@Override
public boolean isRepeatable() {
return yourEntity.isRepeatable();
}
@Override
public boolean isStreaming() {
return yourEntity.isStreaming();
}
@Override
public void writeTo(OutputStream outstream) throws IOException {
class ProxyOutputStream extends FilterOutputStream {
public ProxyOutputStream(OutputStream proxy) {
super(proxy);
}
public void write(int idx) throws IOException {
out.write(idx);
}
public void write(byte[] bts) throws IOException {
out.write(bts);
}
public void write(byte[] bts, int st, int end) throws IOException {
out.write(bts, st, end);
}
public void flush() throws IOException {
out.flush();
}
public void close() throws IOException {
out.close();
}
}
class ProgressiveOutputStream extends ProxyOutputStream {
long totalSent;
long totalSize;
public ProgressiveOutputStream(OutputStream proxy, long total) {
super(proxy);
totalSent = 0;
totalSize = total;
}
public void write(byte[] bts, int st, int end) throws IOException {
totalSent += end;
publishProgress( (int) ((totalSent / (float) totalSize) * 100));
out.write(bts, st, end);
}
}
yourEntity.writeTo(new ProgressiveOutputStream(outstream, yourEntity.getContentLength()));
}
};
ProgressiveEntity myEntity = new ProgressiveEntity();
post.setEntity(myEntity);
HttpResponse response = client.execute(post);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onProgressUpdate(Integer... values) {
super.onProgressUpdate(values);
prgDialog.setProgress(values[0]);
Log.i("VALUES", String.valueOf(values[0]));
}
}