2017-02-27 9 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も同様に使用できます。

stacktraceが発信元であるときにロードしようとしたイメージは、213828900 bytesで、213MBです。これはおそらくサイズが大きいほど、バイト数が多いほど解像度が非常に高い画像になります。

イメージが大きすぎると、あまりにも多くの品質を犠牲にしてスケーリングを行う方法が機能しないことがあります。大きな画像では、ピカソだけがそれをロードする唯一のものかもしれません。

+0

アスペクト比を保持していないため、元のビットマップのサイズはわかりません。 –

+0

これを反映するように編集されました – Zoe

0

例外を避けるには、画像のサイズを確認し、画像のサイズを小さくしてください。この記事をお読みください:Loading Large Bitmaps Efficiently

あなたはまた、ビットマップのサイズが大きすぎると、Bitmapオブジェクトがそれを扱うことができない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' 
関連する問題