ギャラリーやカメラの画像を操作する小さなアプリを作成しました。
それはすべて正常に動作しますが、. 小さな画面と小さなメモリ サイズのデバイス (HTC Desire) では、他の携帯電話からフル サイズでダウンロードした画像がいくつかありますが、それらははるかに大きくなっています (その電話の 8MP カメラ)。
それを読み込もうとすると、小さなカメラの巨大な画像の場合、すぐにクラッシュします。
では、ある種のチェックを実装してその画像を縮小する方法はありますが、適切にロードする方法はありますか?
イメージがロードされた後に縮小しますが、これはクラッシュが発生する前に実行する必要があります。
Tnx。
InputStream in = null;
try {
in = getContentResolver().openInputStream(data.getData());
} catch (FileNotFoundException e) {
e.printStackTrace();
}
// get picture size.
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeStream(in, null, options);
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
// resize the picture for memory.
int screenH = getResources().getDisplayMetrics().heightPixels; //800
int screenW = getResources().getDisplayMetrics().widthPixels; //480
int width = options.outWidth / screenW;
int height = options.outHeight / screenH;
Log.w("Screen Width", Integer.toString(width));
Log.w("Screen Height", Integer.toString(height));
int sampleSize = Math.max(width, height);
options.inSampleSize = sampleSize;
options.inJustDecodeBounds = false;
try {
in = getContentResolver().openInputStream(data.getData());
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// convert to bitmap with declared size.
Globals.INSTANCE.imageBmp = BitmapFactory.decodeStream(in, null, options);
try {
in.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}