【问题标题】:Progress bar in notification bar while uploading image to a server in Android将图像上传到Android中的服务器时通知栏中的进度条
【发布时间】:2011-03-26 05:20:53
【问题描述】:

我正在通过从图库中选择图像或使用 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();

    }

    }

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

【问题讨论】:

  • 我想做同样的事情,从内置摄像头捕获图像并将其发送到服务器。请问您是如何在相机图像和上传部分之间建立联系的?您在 Web 服务器部分使用什么方法来接收图像?该方法是否有 byte[] 参数?我拥有的方法是一种没有参数的方法,它会生成一个随机数。那么如何才能在这两者之间建立联系呢?发送图片并返回一个随机数?请帮忙!!谢谢。
  • @jennifer:既然你已经解决了这个问题,我能知道你是怎么得到正在上传的字节数组的数量的吗?
  • Junaid 我在 Android 上使用了 Multipart 表单上传 17od.com/2010/02/18/multipart-form-upload-on-android
  • 以下答案仅供链接,可能会被审核队列删除。 It is here 以保存它。

标签: android ksoap2


【解决方案1】:

这是有用的链接:http://developer.android.com/guide/topics/ui/notifiers/notifications.html#CustomExpandedView

现在在您的通知自定义布局中使用进度条。

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

 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");
 }

【讨论】:

  • 如何追踪上传图片的百分比??
  • 此代码用于跟踪下载......因为您可以检查上传状态,在服务中启动此异步任务以相应地上传和更新进度条
  • 我需要跟踪通过网络服务上传了多少字节数组。通过soap服务传输字节数组的进度
【解决方案2】:

我不熟悉您用来进行 SOAP 调用的类,但使用 Apache Commons HttpClient 完成上传进度的一种常用方法是提供您自己的 FilterOutputStream 子类,该子类只是对指定的侦听器进行回调每次读出部分流时。你只需要重写 write() 方法:

    @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);
    }

当然,只有当您的 HTTP 库允许您将 HTTP 请求的整个(或至少大部分)作为 OutputStream 提供时,您才能使用此方法。

【讨论】:

    猜你喜欢
    • 2010-12-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-31
    • 2014-02-09
    • 2014-04-19
    相关资源
    最近更新 更多