2

ギャラリーから画像を選択するか、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();

    }

    }

アップロードされた量を確認する方法はありますか?

4

2 に答える 2

3

これは便利なリンクです: 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");
 }
于 2011-03-26T05:24:03.290 に答える
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 として提供できる場合のみです。

于 2011-03-26T07:38:45.260 に答える