を使用して適切なサイズでデコードした後、ビットマップ(以前にファイルから読み取ったもの)を保存しようとしていますBitmapFactory.Options.inSampleSize
。問題は、保存されたビットマップのファイル サイズが元のファイルのサイズの少なくとも 2 倍になることです。私は多くのことを検索しましたが、これに対処する方法を見つけることができませんでした。これは、メモリ効率のために発生させたくないためです (後で保存されたビットマップを再利用します)。これが私が説明することを行う私の方法です:
private Bitmap decodeFileToPreferredSize(File f) {
Bitmap b = null;
try {
// Decode image size
Log.i("Bitmap", "Imported image size: " + f.length() + " bytes");
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
BitmapFactory.decodeFile(f.getAbsolutePath(), o);
//Check if the user has defined custom image size
int scale = 1;
if(pref_height != -1 && pref_width != -1) {
if (o.outHeight > pref_height || o.outWidth > pref_width) {
scale = (int) Math.pow(
2,
(int) Math.round(Math.log(pref_width
/ (double) Math.max(o.outHeight, o.outWidth))
/ Math.log(0.5)));
}
}
// Decode with inSampleSize
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize = scale;
b = BitmapFactory.decodeFile(f.getAbsolutePath(), o2);
String name = "Image_" + System.currentTimeMillis() + ".jpg";
File file = new File(Environment.getExternalStorageDirectory(), name);
FileOutputStream out = new FileOutputStream(file);
b.compress(Bitmap.CompressFormat.JPEG, 100, out);
out.close();
Log.i("Bitmap", "Exported image size: " + file.length() + " bytes");
} catch (Exception e) {
b = null;
}
return b;
}
更新2 つの画像ファイルの密度を見ました。カメラの意図から来たものは、幅と高さの密度が 72 dpi です。上記の方法で作成された画像ファイルは、幅と高さの密度が 96 dpi です。これは、再スケール係数が (96/72) * 2 ~= 2.5 であるため、カメラから取得した 0.5 MByte の画像が上記の方法で約 2.5 MByte にサイズ変更される理由を説明しています。何らかの理由で、私が作成したビットマップは、カメラからの画像の密度を取りません。BitmapFactory.Options.inDensity のすべてのバリエーションで密度を設定しようとしましたが、うまくいきませんでした。また、ビットマップ密度を変更しようとしましbitmap.setDensity(int dpi);
たが、まだ効果がありません。したがって、私の新しい質問は、画像が保存されるときにビットマップの密度を定義する方法があるかどうかです。
前もって感謝します。