2016-08-10 2 views

답변

0

당신은 onPostExecute에서 UI 요소에 액세스하는 코드를 삽입 할 필요가 호출 할 수 없습니다 콜백 메소드. 그러나 Bitmap 개체에 대한 액세스가 필요하므로 AsyncTask 클래스 정의를 변경하여 Bitmap을 3 번째 매개 변수로 포함하는 것이 좋습니다. 그런 다음 후 실행 방법

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; 
     } 

:

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 (데이터 [0], 0, data.length); ' –

+0

이 또한 도움이되지 않습니다 – SimpleCoder

관련 문제