画像の縮小サイズのビットマップを作成するさまざまな方法を見てきましたが、どれも適切に機能しません。別の方法が必要です。
これを説明するのは少し難しいです:-)
私が必要とするのは、画像の比率を維持するが、特定のサイズよりも小さいビットマップです-たとえば、1mbまたはピクセル寸法で同等です(このビットマップは意図のために putExtra() として追加する必要があるため)。
私がこれまでに抱えている問題:
私が調べた方法のほとんどは、ビットマップのスケーリングされたバージョンを作成します。したがって、Image -> Bitmap1 (スケーリングなし) -> Bitmap2 (スケーリングあり)。しかし、画像の解像度が非常に高い場合、十分に縮小されません。解決策は、正確なサイズのビットマップを作成して、解像度を十分に下げることができると思います。
ただし、この方法の副作用として、既に必要なサイズよりも小さい画像がサイズ変更されます (または、サイズ変更が機能しませんか?)。そのため、サイズを変更せずに画像をビットマップに変換できるかどうかを確認する「if」が必要です。
これを行う方法がわからないので、どんな助けでも大歓迎です!:-)
これは私が現在使用しているものです(それは私がやりたいことではありません):
// This is called when an image is picked from the gallery
@Override
public void onActivityResult(int requestCode, int resultCode,
Intent imageReturnedIntent) {
super.onActivityResult(requestCode, resultCode, imageReturnedIntent);
switch (requestCode) {
case 0:
if (resultCode == Activity.RESULT_OK) {
selectedImage = imageReturnedIntent.getData();
viewImage = imageReturnedIntent.getData();
try {
decodeUri(selectedImage);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
iv_preview.setImageBitmap(mImageBitmap);
}
break; // The rest is unnecessary
これは、現在サイズをスケーリングしている部分です。
private Bitmap decodeUri(Uri selectedImage) throws FileNotFoundException {
// Decode image size
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true; //
BitmapFactory.decodeStream(getActivity().getContentResolver()
.openInputStream(selectedImage), null, o);
// The new size we want to scale to
final int REQUIRED_SIZE = 260; // Is this kilobites? 306
// Find the correct scale value. It should be the power of 2.
int width_tmp = o.outWidth, height_tmp = o.outHeight;
int scale = 1;
while (true) {
if (width_tmp / 2 < REQUIRED_SIZE || height_tmp / 2 < REQUIRED_SIZE) {
break;
}
width_tmp /= 2;
height_tmp /= 2;
scale *= 2;
}
// Decode with inSampleSize
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize = scale;
o2.inScaled = false; // Better quality?
mImageBitmap = BitmapFactory.decodeStream(getActivity()
.getContentResolver().openInputStream(selectedImage), null, o2);
return BitmapFactory.decodeStream(getActivity().getContentResolver()
.openInputStream(selectedImage), null, o2);
}
さらに説明する必要がある場合は、言ってください。
ありがとうございました