Progress bar in notification bar while uploading image to a server in Android
I am uploading image to a web server by picking the image from gallery or by capturing it using android camera.
I need to show a progress in the notification bar. I see the Facebook app does this. is there a way to find out how much has been uploaded?
My code for uploading the image to the server is :
public void uploadImage() {
final Toast toast = Toast.makeText(this, "Image uploaded successfully",
Toast.LENGTH_SHORT);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Upload");
builder.setMessage("Do you want to upload the captured picture?");
builder.setIcon(R.drawable.icon);
builder.setCancelable(false);
builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
testWebService(Bitmap bmp)
finish();
toast.show();
}
});
builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alert = builder.create();
alert.show();
}
public void testWebService(Bitmap bmp) {
MarshalBase64 marshal = new MarshalBase64();
ByteArrayOutputStream out = new ByteArrayOutputStream();
bmp.compress(CompressFormat.PNG, 100, out);
byte[] raw = out.toByteArray();
SoapObject request = new SoapObject(WSDL_TARGET_NAMESPACE,
OPERATION_NAME);
request.addProperty("image", raw);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
marshal.register(envelope);
HttpTransportSE httpTransport = new HttpTransportSE(SOAP_ADDRESS);
try
{
httpTransport.call(SOAP_ACTION, envelope);
Object response = envelope.g开发者_StackOverflow中文版etResponse();
}
catch (Exception exception)
{
exception.printStackTrace();
}
}
Is there a way to find out how much has been uploaded?
This is useful link: http://developer.android.com/guide/topics/ui/notifiers/notifications.html#CustomExpandedView
Now use Progress Bar in your custom layout for your Notification.
This code is for tracing download. As this you can check upload status, start this Async task in a service to upload and update progress bar accordingly
private class DownloadFilesTask extends AsyncTask<URL, Integer, Long> {
protected Long doInBackground(URL... urls) {
int count = urls.length;
long totalSize = 0;
for (int i = 0; i < count; i++) {
totalSize += Downloader.downloadFile(urls[i]);
publishProgress((int) ((i / (float) count) * 100));
}
return totalSize;
}
protected void onProgressUpdate(Integer... progress) {
setProgressPercent(progress[0]);
}
protected void onPostExecute(Long result) {
showDialog("Downloaded " + result + " bytes");
}
I'm not familiar with the classes you're using to make your SOAP calls, but a common way of accomplishing upload progress with the Apache Commons HttpClient is to provide your own FilterOutputStream subclass that simply makes a callback to a specified listener every time part of the stream is read out. You just need to override the write() methods:
@Override
public void write(byte[] b, int off, int len) throws IOException {
out.write(b, off, len);
transferred += len;
progressHandler.handleProgress(ProgressDirection.Send, len, transferred, expectedLength);
}
@Override
public void write(int b) throws IOException {
out.write(b);
progressHandler.handleProgress(ProgressDirection.Send, 1, ++transferred, expectedLength);
}
Of course, you can only use this method if your HTTP library allows you to provide the entire body (or at least a large portion) of your HTTP request as an OutputStream.
精彩评论