将图像上传到 Android 中的服务器时通知栏中的进度条
我通过从图库中选取图像或使用 Android 相机捕获图像来将图像上传到网络服务器。
我需要在通知栏中显示进度。我看到 Facebook 应用程序执行此操作。有没有办法知道已经上传了多少内容?
我将图像上传到服务器的代码是:
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.getResponse();
}
catch (Exception exception)
{
exception.printStackTrace();
}
}
有没有办法知道已上传了多少图像?
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.getResponse();
}
catch (Exception exception)
{
exception.printStackTrace();
}
}
Is there a way to find out how much has been uploaded?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是有用的链接: http://developer.android.com /guide/topics/ui/notifiers/notifications.html#CustomExpandedView
现在,在通知的自定义布局中使用进度栏。
此代码用于跟踪下载。因此,您可以检查上传状态,在服务中启动此异步任务以相应地上传和更新进度栏
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
我不熟悉您用来进行 SOAP 调用的类,但使用 Apache Commons HttpClient 完成上传进度的常见方法是提供您自己的 FilterOutputStream 子类,该子类每次只对指定的侦听器进行回调流的内容被读出。您只需要重写 write() 方法:
当然,只有当您的 HTTP 库允许您提供 HTTP 请求的整个正文(或至少大部分)作为 OutputStream 时,您才能使用此方法。
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:
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.