2017-02-27 1 views
-1

나는과 같이 화면하는 파일 시스템에서 이미지를 그리는 응용 프로그램이 있습니다 이미지가 내가 볼 매우 큰 경우캐치 "의 RuntimeException : 캔버스 ... 너무 큰 그리는 시도"

Bitmap image = BitmapFactory.decodeFile(file.getPath()); 
imageView.setImageBitmap(image); 

을 이 오류 :

java.lang.RuntimeException: Canvas: trying to draw too large(213828900bytes) bitmap. 
    at android.view.DisplayListCanvas.throwIfCannotDraw(DisplayListCanvas.java:260) 
    at android.graphics.Canvas.drawBitmap(Canvas.java:1415) 
    ... 

스택이 내 코드에 도달하지 않습니다. 이 오류를 어떻게 잡을 수 있습니까? 아니면이 오류를 피할 수있는 imageView에 이미지를 그리는 더 적절한 방법이 있습니까?

답변

0

사용할 수 있습니다. 따라서 ImageView는 동일한 문제를 가져야합니다. 해결책 : paint.net과 같은 프로그램에서 이미지의 크기를 조정하거나 비트 맵의 ​​고정 크기를 설정하고 크기를 조정합니다.

내가 더 가기 전에, 비트 맵의 ​​도면에 당신의 스택 트레이스 링크, 객체 생성하지 : 당신이 할 수있는 등으로

: mentioned here

Bitmap image = BitmapFactory.decodeFile(file.getPath());//loading the large bitmap is fine. 
int w = image.getWidth();//get width 
int h = image.getHeight();//get height 
int aspRat = w/h;//get aspect ratio 
int W = [handle width management here...];//do whatever you want with width. Fixed, screen size, anything 
int H = w * aspRat;//set the height based on width and aspect ratio 

Bitmap b = Bitmap.createScaledBitmap(image, W, H, false);//scale the bitmap 
imageView.setImageBitmap(b);//set the image view 
image = null;//save memory on the bitmap called 'image' 

또는를, Picasso도 사용할 수 있습니다.

스택 트레이스가 시작될 때로드하려고 시도한 이미지는 213828900 bytes이며 213MB입니다. 이것은 아마도 크기가 클수록 더 큰 바이트로 더 높은 해상도의 이미지 일 것입니다.

이미지가 너무 커지면 너무 많은 품질을 희생하므로 크기 조정이 적용되지 않을 수 있습니다. 이미지가 크면 피카소가 너무 큰 해상도 손실없이 이미지를로드하는 유일한 방법 일 수 있습니다.

+0

가로 세로 비율을 유지하지 않으므로 원본 비트 맵의 ​​크기를 알 수 없습니다. –

+0

수정하여 수정했습니다. – Zoe

0

예외를 피하기 위해 이미지의 크기를 확인하고 작은 크기의 이미지를로드해야합니다. 이 기사를 읽으십시오 : Loading Large Bitmaps Efficiently

또한 비트 맵의 ​​크기가 너무 커서 비트 맵 개체를 처리 할 수 ​​Picasso Library

0

나는 LunarWatcher의 코드에서 오류를 수정했습니다.

Bitmap image = BitmapFactory.decodeFile(file.getPath()); 
float w = image.getWidth();//get width 
float h = image.getHeight();//get height 
int W = [handle width management here...]; 
int H = (int) ((h*W)/w); 
Bitmap b = Bitmap.createScaledBitmap(image, W, H, false);//scale the bitmap 
imageView.setImageBitmap(b);//set the image view 
image = null;//save memory on the bitmap called 'image'