2016-08-10 61 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

相关问题