[UI 스레드에 문제가 해결] UI 스레드안드로이드는 - 방법은 내가 백그라운드 스레드에서 사진을 촬영 한 후 서버로 보낼
에서 호출을 한 후 나는 서버
에서 응답을 수신 할 수 있어야합니다문제는 사진 부분을 찍는 것이 좋으며 서버로 전송하는 것도 좋지만 응답을 수신하지 않는다는 것입니다. 어떤 아이디어?
[UI 스레드에 문제가 해결] UI 스레드안드로이드는 - 방법은 내가 백그라운드 스레드에서 사진을 촬영 한 후 서버로 보낼
에서 호출을 한 후 나는 서버
에서 응답을 수신 할 수 있어야합니다문제는 사진 부분을 찍는 것이 좋으며 서버로 전송하는 것도 좋지만 응답을 수신하지 않는다는 것입니다. 어떤 아이디어?
당신은 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 ?");
}
난이 도움이되기를 바랍니다.
당신은 백그라운드 스레드 (이 경우 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));
}
}
괜찮지 만,이 Bitmap bmp = ((BitmapDrawable) capturedImageHolder.getDrawable()). getBitmap(); 어떻게 생겼는지 보여 줄 수 있어요? – SimpleCoder
비트 맵 비트 맵 = BitmapFactory.decodeByteArray (데이터 [0], 0, data.length); ' –
이 또한 도움이되지 않습니다 – SimpleCoder
이 도움이되지 않습니다. – SimpleCoder
오류? – ishmaelMakitla
null을 반환하고, 어떤 반환 유형'onPostExecute' 및'doInBackground'가 있어야합니까? – SimpleCoder