2014-05-07 104 views
-1

我想將圖像上傳到我的網絡服務器。 我有服務器和所有的響應,所以這不是問題。 當我將以下代碼粘貼到代碼中時,它大多會崩潰。Android:處理圖像時AsyncTask崩潰

我在調試器中看到AsyncTask的RuntimeException,就這樣。

另一個問題:我可以將位圖解壓縮成一個字節數組,所以我可以用原始文件結尾上傳原始文件嗎?

這裏有問題行:

 // create the image for the server 
    Bitmap bm=BitmapFactory.decodeFile(Values.imageFilePath); 
     ByteArrayOutputStream stream=new ByteArrayOutputStream(); 
     bm.compress(Bitmap.CompressFormat.JPEG, 100, stream); 
     byte[] byte_array=stream.toByteArray(); 
     String byteString=Base64.encodeToString(byte_array, Base64.DEFAULT); 

這裏是整個的AsyncTask類,上面的代碼被註釋掉,它運行。 什麼問題? (ProgressDialog將在主要活動中創建) u 謝謝。

class UploadThread extends AsyncTask<String, Void, String> 
    { 
     private Context _context; 
     private ProgressDialog pd; 

     public UploadThread(Context context) 
     { 
      super(); 
      _context=context; 
     } 

     protected String doInBackground(String... urls) 
     { 
      String response="Android says \"Nothing happened.\"";  
      InputStream is = null; 
      StringBuilder sb=null; 

      //http post 
      try{ 
       HttpClient httpclient = new DefaultHttpClient(); 
       HttpPost httppost = new HttpPost(Values.hostname); 

       ArrayList<NameValuePair> nvp=new ArrayList<NameValuePair>(); 
       // This is the Post data. 
       nvp.add(new BasicNameValuePair("plog_android_upload","plogupload")); 
       nvp.add(new BasicNameValuePair("thecomment",Values.comment)); 

       // get the image and process it. 
//   Values.progressDialog.setMessage("Processing image.."); 
     // create the image for the server 
    // Bitmap bm=BitmapFactory.decodeFile(Values.imageFilePath); 
    // ByteArrayOutputStream stream=new ByteArrayOutputStream(); 
    // bm.compress(Bitmap.CompressFormat.JPEG, 100, stream); 
    // byte[] byte_array=stream.toByteArray(); 
    // String byteString=Base64.encodeToString(byte_array, Base64.DEFAULT); 

     //Values.progressDialog.setMessage("Uploading.."); 
     //nvp.add(new BasicNameValuePair("upload_file",byteString)); 

     httppost.setEntity(new UrlEncodedFormEntity(nvp)); 

     HttpResponse httpresponse = httpclient.execute(httppost); 
     HttpEntity entity = httpresponse.getEntity(); 
     is = entity.getContent(); 
     response="Connection established."; 
    }catch(UnknownHostException eu){ // gets thrown on unknown host. 
     return "No connection!\n(or wrong hostname)"; 
    }catch(HttpHostConnectException ex){ // gets thrown on unknown connection. 
     return "No connection!\n(or wrong hostname)"; 
    }catch(Exception e){ 
     return "Error in http connection:"+e.toString(); 
    } 

    //convert response to string 
    try{ 
     BufferedReader reader = new BufferedReader(new InputStreamReader(is,"utf-8"),8); 
     sb = new StringBuilder(); 
     sb.append(reader.readLine() + "\n"); 
     String line="0"; 
     while ((line = reader.readLine()) != null) { 
      sb.append(line + "\n"); 
     } 
     is.close(); 
     response="Server says: "+sb.toString(); 
    }catch(Exception e){ 
     response= "Error converting HTTP result: "+e.toString(); 
     return response; 
    } 

    return response; 
} 

@Override 
protected void onPostExecute(String result) 
{ 
    super.onPostExecute(result); 
    // dismiss the progress dialog. 
    Values.progressDialog.dismiss(); 
    // Show the user some feedback. 
    Toast.makeText(_context,result, Toast.LENGTH_LONG).show();  
} 

}

+3

。 –

+0

你的問題在這裏 Values.progressDialog.setMessage(「Uploading ..」); ,因爲這是UI,並且doInBackground無法觸及UI。 和YES發佈一些堆棧跟蹤 – Yazan

回答

0

我想你用

代碼
  HttpClient httpclient = new DefaultHttpClient(); 
      HttpPost httppost = new HttpPost(Values.hostname); 

      ArrayList<NameValuePair> nvp=new ArrayList<NameValuePair>(); 
      // This is the Post data. 
      nvp.add(new BasicNameValuePair("plog_android_upload","plogupload")); 
      nvp.add(new BasicNameValuePair("thecomment",Values.comment)); 

不應在doInBackground執行,我也面臨同樣的問題,當我在doInBackground使用ArrayList中,所以我將該部分代碼更改爲在UIThread中工作的onPreExecute(),並將精確的行號與精確的行號一起發送到UIThread

+0

它不能成爲問題夥伴。 –

+0

你確定嗎?因爲當我這樣做時我的問題解決了,雖然我不知道內心的戲劇...... – Fahad