将图像上传到 Android 中的服务器时通知栏中的进度条

发布于 2024-10-26 02:49:27 字数 1905 浏览 2 评论 0原文

我通过从图库中选取图像或使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我是有多爱你 2024-11-02 02:49:27

这是有用的链接: 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");
 }

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");
 }
坐在坟头思考人生 2024-11-02 02:49:27

我不熟悉您用来进行 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 时,您才能使用此方法。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文