カメラ画像の読み込み
Gallaryからカメラで撮影した画像を選択して読み込もうとしたら Out of memory exception が出ました。
使う側に画面に合わせた画像にしてもらうのが一番いいのですが(本体の壁紙設定のようなクリッピングとか)まずは読み込み可能な状態にしようというところで対応しました。
ApiDemo にも読み込み時にサイズ変更する例が入ってますが、今回の ContentResolver 経由で読み込むときに挙動が違ったので載せておきます。
BitmapFactory.Options bo = new BitmapFactory.Options(); InputStream is = getContext().getContentResolver().openInputStream (uri); bo.inJustDecodeBounds = true; Bitmap bitmap = BitmapFactory.decodeStream(is, null, bo); is.close(); is = getContext().getContentResolver().openInputStream (uri); if ( bo.outWidth > mWidth ) { bo.inSampleSize = 倍率 } bo.inJustDecodeBounds = false; bitmap = BitmapFactory.decodeStream(is, null, bo); is.close();
inJustDecodeBounds を true にすることで縦横サイズを取得して、読み込めるサイズより大きいようならその画像を加工するために inSampleSize で分母を指定。2なら半分、4なら1/4に縮小されるのでファイルサイズも小さくなります。
そして2度 openInputStream しているのは…多分 FileStream じゃないので、一度終端まで読んでしまったら戻れないからかだと思うんですが…reset では例外が発生しました。