본문 바로가기

나의 플랫폼/안드로이드

[Android] !!! FAILED BINDER TRANSACTION !!!

336x280(권장), 300x250(권장), 250x250, 200x200 크기의 광고 코드만 넣을 수 있습니다.

안드로이드에서 Material Design을 계속 밀어줌으로써,

Activity 간의 이동을 할 때 Bitmap을 넘겨주고자 하는 일이 많아졌다.


Intent에 Bitmap을 put 시킬 때, 안드로이드에서는 이미지 크기가 40KB 로 제한되어 있다.

따라서 40KB 이상의 Bitmap을 넣을 경우!!! 아래와 같은 Log를 볼 수 있다.


!!! FAILED BINDER TRANSACTION !!!


그럼.. 이미지의 크기를 낮춰야 할까??? 그럼 이미지가 깨지게 되는데.. 어떻하지??

이벤트 마다 이미지를 다시 로딩 해야 하나??? 

화면을 넘어가서 로딩 할까?? 그럼 화면에서 정상적인 동작이 이뤄지지 않을 수도 있다.

(예를 들어 모션은 들어 갔는데 이미지는 아직 로딩이 안되는 상태에서 이미지를 구할려고 한다면?!)


아래와 같은 해결책이 있네요!


Bitmap 자체를 보내지 말고, byte 배열로 압축을 해서 보내는 겁니다.


아래는 보내기 전에 Bitmap을 byte 배열로 바꾸는 소스 입니다.


 ByteArrayOutputStream stream = new ByteArrayOutputStream();
    bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
    byte[] bytes = stream.toByteArray(); 
    setresult.putExtra("BMP",bytes);


받아온 byte 배열을 Bitmap으로 변환 하는 소스 입니다.

 byte[] bytes = data.getByteArrayExtra("BMP");
    Bitmap bmp = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
http://stackoverflow.com/a/31115344/3534559


엄청 큰 데이터는 당연 문제가 있겠지만, 어느 정도아온 byte 배열을 Bitmap으로 변환 하는 소스 입니다.

그럼 엄청 큰 데이터는 어떻게 할까요?? ㅎㄷㄷ.. 특히 카메라로 직촬 할 경우 더욱 그렇습니다.


public Bitmap readImageWithSampling(String imagePath, int targetWidth, int targetHeight) {
// Get the dimensions of the bitmap
BitmapFactory.Options bmOptions = new BitmapFactory.Options();
bmOptions.inJustDecodeBounds = true;
BitmapFactory.decodeFile(imagePath, bmOptions);

int photoWidth = bmOptions.outWidth;
int photoHeight = bmOptions.outHeight;

if (targetHeight <= 0) {
targetHeight = (targetWidth * photoHeight) / photoWidth;
}

// Determine how much to scale down the image
int scaleFactor = 1;
if (photoWidth > targetWidth) {
scaleFactor = Math.min(photoWidth / targetWidth, photoHeight / targetHeight);
}

// Decode the image file into a Bitmap sized to fill the View
bmOptions.inJustDecodeBounds = false;
bmOptions.inSampleSize = scaleFactor;

return BitmapFactory.decodeFile(imagePath, bmOptions);
}
http://lsit81.tistory.com/entry/Android-Bitmap-Load


위 소스는 고정하고자 하는 width와 height 사이즈가 있다면 그에 맞게 scale을 해주는 함수 입니다.


예를 들어 핸드폰 가로 사이즈에 딱 맞는 썸네일을 만들고 싶을 경우는 아래와 같이 넣으면 되겠죠?

Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);

readImageWithSampling(filePath,size.x,0)

참고하세요.