2011-03-26 54 views
2

我通過從圖庫中選擇圖像或使用安卓相機捕獲圖像將圖像上傳到網絡服務器。在上傳圖片到服務器的通知欄中的進度條在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(); 

    } 

    } 

是有辦法找出多少已經上傳???

+0

我想做同樣的事情從內置攝像頭捕獲圖像並將其發送到服務器。我可以問你是如何連接攝像頭圖像和上傳部分的?你使用什麼方法在Web服務器部分接收圖像? 該方法是否有byte []參數? 我有一個沒有參數的方法,它會生成一個隨機數。那麼我怎樣才能使這兩者之間的聯繫?發送圖像並返回一個隨機數字? 請幫忙!! 謝謝。 – Michiru 2011-06-02 18:03:53

+0

@jennifer:正如你已經解決了這個問題,我可以知道你是如何得到正在上傳的字節數組的數量? – Junaid 2011-11-15 08:37:10

+0

Junaid我在Android上使用多部分表格上傳 http://www.17od.com/2010/02/18/multipart-form-upload-on-android/ – jennifer 2011-11-21 10:53:41

回答

3

檢查這個舐...

3

嗨 這是有用的鏈接 http://developer.android.com/guide/topics/ui/notifiers/notifications.html#CustomExpandedView 現在在ur通知的自定義佈局中使用進度條。

該代碼可用於跟蹤下載....由於此,您可以檢查上傳狀態,在服務啓動這個異步任務的幫助Dialog for Loading Contacts上傳和更新進度條相應

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

我如何追蹤正在上傳的圖像的百分比? – jennifer 2011-03-26 05:29:53

+0

此代碼用於跟蹤下載....因此,您可以檢查上傳狀態,在服務中啓動此異步任務以上載並更新進度條 – om252345 2011-03-26 07:51:11

+0

@Jennifer如果您有答案,請務必接受。 – om252345 2011-03-26 09:57:47

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。