次の解決策が役立つことを願っています。imageView を固定サイズにして、その imageView の幅と高さをcalculateInSampleSize
メソッドに渡すことができます。画像サイズに基づいて、画像をダウンサンプリングするかどうかを決定します。
public Bitmap getBitmap(Context context, final String imagePath)
{
AssetManager assetManager = context.getAssets();
InputStream inputStream = null;
Bitmap bitmap = null;
try
{
inputStream = assetManager.open(imagePath);
BitmapFactory.Options options = new BitmapFactory.Options();
options.inScaled = true;
options.inJustDecodeBounds = true;
// First decode with inJustDecodeBounds=true to check dimensions
bitmap = BitmapFactory.decodeStream(inputStream);
// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, requiredWidth, requiredHeight);
options.inJustDecodeBounds = false;
bitmap = BitmapFactory.decodeStream(inputStream);
}
catch(Exception exception)
{
exception.printStackTrace();
bitmap = null;
}
return bitmap;
}
public int calculateInSampleSize(BitmapFactory.Options options, final int requiredWidth, final int requiredHeight)
{
// Raw height and width of image
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;
if(height > requiredHeight || width > requiredWidth)
{
if(width > height)
{
inSampleSize = Math.round((float)height / (float)requiredHeight);
}
else
{
inSampleSize = Math.round((float)width / (float)requiredWidth);
}
}
return inSampleSize;
}