2012-08-02 115 views
0

我一直在試圖開發一個Android應用程序,它可以讓用戶拍照並通過HTTP發送圖像。我正在使用本機相機。Android應用程序在等待HTTP響應時顯示黑屏

用戶拍攝照片並點擊保存按鈕後,當應用程序發送信息並等待響應時,屏幕出現黑屏。我寧願顯示一個進度對話框,但無論我嘗試了什麼,黑屏都停留在那裏,進度對話框只有在獲得響應並點擊後退按鈕後才能看到。我曾嘗試使用setContentView()無濟於事。這些線程用於HTTP請求。

下面是攝像頭的開始和結束的代碼:

protected void startCameraActivity() 
{ 

    File file = new File(_path); 
    Uri out = Uri.fromFile(file); 

    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
    intent.putExtra(MediaStore.EXTRA_OUTPUT, out); 

    startActivityForResult(intent, 0); 
} 

protected void onActivityResult(int requestCode, int resultCode, Intent data) 
{ 
    switch(resultCode) 
    { 
    case 0: 
     break; 

    case -1: 
     m_ProgressDialog = ProgressDialog.show(MainActivity.this, "Please wait...", "Uploading data ...", true, true); 
     onPhoto(); 
     break; 
    } 
} 

protected void onPhoto() 
{ 
    taken = true; 


    PipedOutputStream pos = new PipedOutputStream(); 
    PipedInputStream pis = null; 
    try { 
     pis = new PipedInputStream(pos); 
    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
    Sender sender = new Sender(pos); 
    Receiver receiver = new Receiver(pis); 
    sender.start(); 
    receiver.start(); 
    try { 
     sender.join(); 
     receiver.join(); 
     try { 
      field.setText(receiver.getIn().readUTF()); 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
    } catch (InterruptedException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 

    try { 
     pis.close(); 
     pos.close(); 
    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 

} 
+0

你或許應該做你的相機處理的'AsyncTask'。放在主線程意味着你的應用程序不會移動,直到該代碼行完成。 – 2012-08-02 18:56:21

回答

1

退房此鏈接:How can I use an Async Task to upload a file to the server?

onPreExecute()你可以定義你想顯示什麼,而圖像處理(進度酒吧,一個微調......)。

+0

我切換到使用AsyncTask代替HTTP線程後,應用程序不一致。我有時得到「E/WindowManager(14660):android.view.WindowLeaked:活動MainActivity已泄露窗口[email protected]原來在這裏添加」錯誤,有時它的工作完美無瑕。 – mori 2012-08-02 20:42:58

+0

將launchMode的清單更改爲「singleTop」做了訣竅,謝謝! – mori 2012-08-02 20:59:08

0

可以使用的AsyncTask,並顯示ProgressDialog任務的同時執行

private class YourTask extends AsyncTask { 
     private ProgressDialog dialog; 

     private GetNewsTask(Context context) { 
      this.dialog = new ProgressDialog(context); 
     } 

     @Override 
     protected void onPreExecute() { 
      this.dialog.setMessage(getResources().getString(R.string.loading)); 
      this.dialog.show(); 
     } 

     @Override 
     protected Object doInBackground(Object... objects) { 
      // your hard work 
      return something; 
     } 

     @Override 
     protected void onPostExecute(Object result) { 
      if (this.dialog.isShowing()) this.dialog.dismiss(); 
      // hanle results 

     } 

    } 
+0

謝謝你的回答。在onPostexecute()方法中更改爲新活動會導致任何問題嗎? – mori 2012-08-02 20:11:48

+0

不太明白你的意思。 onPostexecute在UI線程中執行,並且在任務完成後需要顯示一些更改。你可以使用這個類作爲你的活動的內部類。 – 2012-08-02 20:17:39