public static Bitmap loadBitmapWithBounds(String imagePath) {
// Just check bitmap dimensions
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(imagePath, options);
// Load bitmap
options.inSampleSize = calculateInSampleSize(options);
options.inJustDecodeBounds = false;
return BitmapFactory.decodeFile(imagePath, options);
}
private static int calculateInSampleSize(BitmapFactory.Options options) {
// Raw height and width of image
int height = options.outHeight;
int width = options.outWidth;
int curSize = Math.max(width, height);
int inSampleSize = 1;
int maxTexSize = 1000;
while (maxTexSize < curSize) {
inSampleSize *= 2;
curSize /= 2;
}
return inSampleSize;
}
public static void savebitmap(Bitmap bitmap,String jpgFilePath)
{
try (BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(jpgFilePath))) {
if (bitmap.compress(Bitmap.CompressFormat.JPEG, 80, bos)) {
bos.flush();
}
} catch (IOException e) {
e.printStackTrace();
}
}