在Android中将图像上载到服务器时,通知栏中的进度栏

在Android中将图像上载到服务器时,通知栏中的进度栏,android,ksoap2,Android,Ksoap2,我通过从gallery中拾取图像或使用android摄像头捕获图像,将图像上传到web服务器 我需要在通知栏中显示进度。我看到Facebook应用程序可以做到这一点。有没有办法知道上传了多少 我将图像上载到服务器的代码是: public void uploadImage() { final Toast toast = Toast.makeText(this, "Image uploaded successfully", Toast.LENGTH_SHORT); A

我通过从gallery中拾取图像或使用android摄像头捕获图像,将图像上传到web服务器

我需要在通知栏中显示进度。我看到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();

    }

    }
有没有办法知道上传了多少内容?

这是一个有用的链接:

现在在自定义布局中使用进度条进行通知


此代码用于跟踪下载。因为您可以检查上载状态,所以在服务中启动此异步任务以相应地上载和更新进度条

 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");
 }
私有类下载文件任务扩展异步任务{
受保护的长doInBackground(URL…URL){
int count=url.length;
长totalSize=0;
for(int i=0;i
我不熟悉您用来进行SOAP调用的类,但是使用Apache Commons HttpClient完成上载过程的一种常见方法是提供您自己的FilterOutputStream子类,该子类只需在每次读取流的一部分时对指定的侦听器进行回调。您只需重写写入()方法:


当然,只有在HTTP库允许您提供整个正文(或至少大部分)时,才能使用此方法您的HTTP请求的输出流。

我如何跟踪上载的图像的百分比?此代码用于跟踪下载…由于您可以检查上载状态,请在服务中启动此异步任务以上载和更新进度条。我需要跟踪通过Web服务上载的字节数。字节数的进度t通过soap服务传输我想做同样的事情从内置摄像头捕获图像并将其发送到服务器。我可以问一下您是如何在摄像头图像和上载部分之间建立连接的吗?您在web服务器部分使用了什么方法来接收图像?该方法是否有字节[]参数?我的方法是一个没有参数的方法,它会生成一个随机数。那么我如何在这两者之间建立连接?发送图像并返回一个随机数?请帮助!!谢谢。@jennifer:既然你已经解决了这个问题,我能知道你是如何得到上传的字节数组数量的吗?Junaid我使用了多部分表单uplAndroid上的oad下面的答案是链接,可能会被审查队列删除。为了保留它。
    @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);
    }