2012-08-09 5 views
0

비트 맵에로드 한 PNG 파일에서 캔버스를 만들려고하지만 오류가 발생합니다. 여기 코드는 다음과 같습니다Android에서로드 된 PNG에서 캔버스 만들기

public Bitmap CABINET_Bitmap; 

AssetManager assetManager = this.getAssets(); 
inputStream = assetManager.open("background.png"); 
CABINET_Bitmap = BitmapFactory.decodeStream(inputStream); 

// Next line gives error 
Canvas cv = new Canvas(CABINET_Bitmap); 

I을 수행하여, 비트 맵을 만드는 것이 아니라 그것을에서로드하는 경우 :

CABINET_Bitmap = Bitmap.createBitmap(480, 640, Config.RGB_565); 
Canvas cv = new Canvas(CABINET_Bitmap); 

그런 다음 캔버스 생성이 작동합니다. 내가 뭘 잘못하고 있는거야?

답변

0

문서 상태 : Bitmap.createBitmap()가 변경 가능한 하나를 반환하는 동안

Construct a canvas with the specified bitmap to draw into. The bitmap must be mutable.

The initial target density of the canvas is the same as the given bitmap's density.

그래서 내가 무엇을 가정하고있어 BitmapFactory.decodeStream()이다는 불변의 비트 맵을 반환합니다. 대신 BitmapFactory.Options을 사용하고 inMutable을 true로 설정하십시오.

BitmapFactory.Options o = new BitmapFactory.Options(); 
o.inMutable = true; 
CABINET_Bitmap = BitmapFactory.decodeStream(inputStream, o); 
Canvas cv = new Canvas(CABINET_Bitmap); 

작동하는지 확인하십시오.

+0

훌륭한 사운드! 불행히도 내 intellisense는 "inMutable"속성을 보여 주지만 "o.inMutable = true;"줄을 실행하면 오류가 발생했습니다. NoSuchFieldError ... – user1358999

+0

D' oh. 방금 API 11에 추가 된 것으로 나타났습니다. 글쎄, 나는 이것이 문제라는 것을 확신합니다. 따라서 비트 맵을 변경할 수없는 것으로 변환해야합니다. http://www.anddev.org/how_to_modify_the_image_file-t513.html 및 http://stackoverflow.com/questions/4349075/bitmapfactory-decoderesource-returns-a-mutable-bitmap-in-android-2-2-and- - 이뮤는 시작하기에 좋은 장소 인 것 같습니다. 도움이되기를 바랍니다. – DeeV

+0

훌륭하게, 나는 그것을 고칠 수 있어야합니다. 당신의 도움을 주셔서 감사합니다. 책상에서 내 머리를 몇 시간 동안 두드려서 구해 줬어. – user1358999