public void InitBitmap(){ ... Bitmap bm = BitmapFactory.decodeFile(path,options); Bitmap newBM = Bitmap.careateSacleBitmap(bm, width, height, false); bm.recycle(); imgView.setImageBitmap(newBM); }
안녕하세요, 매번 비슷한 질문 올려서 죄송합니다. ㅠ 안드로이드에 대한 기초지식도 부족하구... 해서요..;
위의 코드에서 newBM변수는 recycle 안해줘도 되는건 알겠는데,
bm 변수는 recycle 해줘야 하는것 아닌가요? recycle해줬더니 아래와 같은 에러가 발생합니다.
java.lang.IllegalArgumentException: Cannot draw recycled bitmaps
구글링 해봤고 disposeBitmap(Bitmap bm) 함수 만들어서 setImageBitmap 아래에도 추가해보고 했는데,
계속 같은 에러가 발생하네요.
recycle을 안해주면 에러가 발생하지 않는데, 여기서 이거 recycle안해줘도 되는건지 의심스러워서 질문 드립니다.
조언 부탁드려요!
private void recycleIfOtherObject(Bitmap srcBitmap, Bitmap resultBitmap) {
if (srcBitmap.hashCode() != resultBitmap.hashCode()) {
try {
srcBitmap.recycle();
} catch (Exception e) {
e.getStackTrace();
}
}
}
======================================================================================
이 소스를 저도 응용해보고 싶은데요. 궁금한것은 저기 srcBitmap과 resultBitmap은 무엇을 받아와야하나요?
저는 지금 앱이 Destroy될 때 recycle 해주라고 되어있는데 다시 앱을 실행하면 Cannot draw recycled bitmaps라고 에러가 뜨면서 죽거든요.
제 코드입니다.
@Override
protected void onDestroy() {
Log.d(TAG, "onDestroy()");
recycleBitmap(background); // 배경이미지 바꾸는 imageView 없애기
super.onDestroy();
}
private static void recycleBitmap(ImageView iv) {
Drawable d = iv.getDrawable();
if(d instanceof BitmapDrawable){
Bitmap b = ((BitmapDrawable)d).getBitmap();
b.recycle();
b = null;
}
}
그런데 저의 경우에 칠리님의 srcBitmap과 resultBitmap을 이용한 recycleOtherObject를 사용하려면 어떻게 해야하나요?
쪽지로 드렸지만 다른분을 위해...
public static Bitmap createBitmap (Bitmap source, int x, int y, int width, int height, Matrix m, boolean filter)
Returns an immutable bitmap from subset of the source bitmap, transformed by the optional matrix. The new bitmap may be the same object as source, or a copy may have been made. It is initialized with the same density as the original bitmap. If the source bitmap is immutable and the requested subset is the same as the source bitmap itself, then the source bitmap is returned and no new bitmap is created.
private void recycleIfOtherObject(Bitmap srcBitmap, Bitmap resultBitmap) {
if (srcBitmap.hashCode() != resultBitmap.hashCode()) {
try {
srcBitmap.recycle();
} catch (Exception e) {
e.getStackTrace();
}
}
}
public static Bitmap createBitmap (Bitmap source, int x, int y, int width, int height, Matrix m, boolean filter)
public static Bitmap createBitmap (Bitmap source, int x, int y, int width, int height)
public static Bitmap createBitmap (Bitmap src)