2016-08-10 60 views
1

[與UI線程問題解決]Android的 - 方法必須從UI線程

我想在後臺線程圖片,然後將其發送到服務器被稱爲之後,我想接收來自服務器

響應

問題是拍照部分好,發送到服務器也好但是接收響應不起作用。任何想法爲什麼?

回答

0

你需要把訪問的onPostExecute UI元素的代碼回調方法。但是因爲您還需要訪問Bitmap對象 - 我建議將您的AsyncTask類定義更改爲包含Bitmap作爲第三個參數。

class TakePhotoTask extends AsyncTask<byte[], String, Bitmap> { 
    @Override 
    protected Bitmap doInBackground(byte[]... data) { 
     //since we want to decode the entire byte-array and not just 0th byte 
     Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length); 
     if (bitmap == null) { 
      return null; 
     } 
     //the rest of your code/logic goes here... 
     return bitmap; 
     } 

然後在你的後execute方法:

protected void onPostExecute(Bitmap bitmap) { 
    if(bitmap == null){return; } 
    capturedImageHolder.setImageBitmap(scaleDownBitmapImage(bitmap, 400, 400)); 
    try { 
      //I am not sure what the purpose of this section of the code is... 
      //so I just left it here 
      Bitmap bmp = ((BitmapDrawable) capturedImageHolder.getDrawable()).getBitmap(); 
      ByteArrayOutputStream bos = new ByteArrayOutputStream(); 
      bmp.compress(Bitmap.CompressFormat.PNG, 100, bos); 
      byte[] array = bos.toByteArray(); 
      final String tmp = Base64.encodeToString(array, Base64.NO_WRAP); 
     } catch (Exception e) { 
     e.printStackTrace(); 
    } 
    showDialog("Image Taken ?"); 
} 

我希望這有助於。

+0

這沒有幫助 – SimpleCoder

+0

你是否得到相同的錯誤? – ishmaelMakitla

+0

它返回null,'onPostExecute'和'doInBackground'應該有什麼返回類型? – SimpleCoder

0

您不能更新從後臺線程(doInBackground在這種情況下), 覆蓋onPostExecute UI(視圖),而且把這種capturedImageHolder.setImageBitmap(scaleDownBitmapImage(bitmap, 400, 400));

 class TakePhotoTask extends AsyncTask<byte[], String, Bitmap> { 

       @Override 
       protected Void doInBackground(byte[]... data) { 
        Bitmap bitmap = BitmapFactory.decodeByteArray(data[0], 0, data.length); 
        if (bitmap == null) { 
        //Toast.makeText(MainActivity.this, "Captured image is empty", Toast.LENGTH_LONG).show(); 
         return null; 
        } 

        //capturedImageHolder.setImageBitmap(scaleDownBitmapImage(bitmap, 400, 400)); 
        try { 
         Bitmap bmp = ((BitmapDrawable) capturedImageHolder.getDrawable()).getBitmap(); 
         ByteArrayOutputStream bos = new ByteArrayOutputStream(); 
         bmp.compress(Bitmap.CompressFormat.PNG, 100, bos); 
         byte[] array = bos.toByteArray(); 
         final String tmp = Base64.encodeToString(array, Base64.NO_WRAP); 
        } catch (Exception e) { 
         e.printStackTrace(); 
        } 
        return bitmap; 
       } 

      @Override 
      protected void onPostExecute(Bitmap bitmap) { 
       super.onPostExecute(bitmap); 
       capturedImageHolder.setImageBitmap(scaleDownBitmapImage(bitmap, 400, 400)); 
      } 
} 
+0

好的,但是用這個'Bitmap bmp =((BitmapDrawable)capturedImageHolder.getDrawable())。getBitmap();'?你能告訴我它應該是什麼樣子嗎? – SimpleCoder

+0

你已經在第一行有位圖參考 '位圖位圖= BitmapFactory.decodeByteArray(data [0],0,data.length);' –

+0

這也沒有幫助 – SimpleCoder

相關問題