画像をサイズ変更して(ファイルとして保存する)、あらかじめ定義されたファイルサイズ(私の場合は512 KB) )。このメソッドが実行されるとAndroid - 画像サイズを縮小しても同じ割合でサイズが縮小されない
private static final int maximumFileSizeInKBs = 512;
public static Uri resizeImage(Context context, Uri imageUri) {
// get image width and height
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
Bitmap imageBitmap = BitmapFactory.decodeFile(new File(imageUri.getPath()).getAbsolutePath(), options);
int imageWidth = options.outWidth;
int imageHeight = options.outHeight;
// get image file size (in KBs)
File file = new File(imageUri.getPath());
int fileSizeInKBs = Integer.parseInt(String.valueOf(file.length()/1024));
// get the scaling factor
// (square root because when we scale both the width and height by the
// square root of the scaling factor, then the size of the final image
// will be scaled by the scaling factor)
double scalingFactor = Math.sqrt(fileSizeInKBs/maximumFileSizeInKBs);
// no need to scale if file already within maximum file size limit
if(scalingFactor <= 1) {
return imageUri;
}
// get scaled dimensions
int scaledImageWidth = (int) Math.floor(imageWidth/scalingFactor);
int scaledImageHeight = (int) Math.floor(imageHeight/scalingFactor);
// load original bitmap (load a scaled copy to avoid OutOfMemory errors)
options = new BitmapFactory.Options();
options.inSampleSize = Math.max(imageHeight/scaledImageWidth, imageHeight/scaledImageHeight);
imageBitmap = BitmapFactory.decodeFile(new File(imageUri.getPath()).getAbsolutePath(), options);
// the scaled image above will be scaled by a value equal to the
// nearest power of 2, hence it wont be scaled to the exact value
// that we want. So, we need to calculate new scaling factors
// based on the scaled loaded bitmap
Matrix m = new Matrix();
RectF inRect = new RectF(0, 0, imageWidth, imageHeight);
RectF outRect = new RectF(0, 0, scaledImageWidth, scaledImageHeight);
m.setRectToRect(inRect, outRect, Matrix.ScaleToFit.CENTER);
float[] values = new float[9];
m.getValues(values);
// create a scaled bitmap with required file size
Bitmap scaledBitmap = Bitmap.createScaledBitmap(imageBitmap, (int) (imageWidth*values[0]), (int) (imageHeight*values[4]), true);
// delete original image
new File(imageUri.getPath()).delete();
// write bitmap to file
File newImageFile = BaseResourceClass.makeTempResourceFile(Slide.ResourceType.IMAGE, context);
try {
newImageFile.createNewFile();
FileOutputStream fos = new FileOutputStream(newImageFile);
scaledBitmap.compress(Bitmap.CompressFormat.PNG, 1, fos);
fos.close();
} catch (IOException e) {
e.printStackTrace();
Toast.makeText(context, "Something went wrong", Toast.LENGTH_SHORT).show();
}
// recycle bitmaps to reallocate memory
Storage.recycleBitmap(imageBitmap);
Storage.recycleBitmap(scaledBitmap);
// scaling done, return new Image
return Uri.fromFile(newImageFile);
}
、画像寸法は(私はそれがあることを期待するもののように)正確なサイズに縮小ますが、KBの中の画像のサイズが縮小されていません。この方法は、以下のようになります同じ要因で例えば、私は次の番号に
before resizing
imageWidth : 3120, imageHeight : 4160, fileSize : 2960 KB
after resizing
imageWidth : 1395, imageHeight : 1860, fileSize : 2491 KB
を取得しています。このメソッドを実行した後、私は画像から非常に多くのピクセルを削除した後、それを維持するようにとても多くのスペースを占有している何であることを理解していません画像ファイルのサイズはこれほどです。他のStackOverflowの質問やAndroidのBitmapFactory、ビットマップクラスのドキュメントを見ようとしましたが、この問題はどこにも言及されていません。どんな助けもありがとう!