Skip to content

Commit

Permalink
Merge pull request #9 from deinlandel/dev
Browse files Browse the repository at this point in the history
merge "dev"
  • Loading branch information
RonTt committed Oct 18, 2018
2 parents d8b3fd9 + 86fcce3 commit c733323
Showing 1 changed file with 22 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import java.io.File;
import java.io.FileDescriptor;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
Expand Down Expand Up @@ -728,43 +729,35 @@ protected void rotateBitmapByExif(ChosenImage image, int quality) {
}
}

protected void ensureRequiredQuality(ChosenImage image, int quality) {
protected void ensureRequiredQuality(ChosenImage image, int quality) throws PickerException {
FileOutputStream out = null;
BufferedInputStream scaledInputStream = null;
try {
BufferedInputStream scaledInputStream = null;
Bitmap bitmap;
ExifInterface originalExifInterface = new ExifInterface(image.getOriginalPath());
String originalRotation = originalExifInterface.getAttribute(ExifInterface.TAG_ORIENTATION);
try {
scaledInputStream = new BufferedInputStream(new FileInputStream(image.getOriginalPath()));
bitmap = BitmapFactory.decodeStream(scaledInputStream);
} finally {
if (scaledInputStream != null) scaledInputStream.close();
}
if (bitmap == null) return;
scaledInputStream = new BufferedInputStream(new FileInputStream(image.getOriginalPath()));
Bitmap bitmap = BitmapFactory.decodeStream(scaledInputStream);

File file = new File(image.getOriginalPath());
try {
out = new FileOutputStream(file);
Matrix matrix = new Matrix();
bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(),
bitmap.getHeight(), matrix, false);
bitmap.compress(Bitmap.CompressFormat.JPEG, quality, out);
ExifInterface compressedExifInterface = new ExifInterface(file.getAbsolutePath());
compressedExifInterface.setAttribute(ExifInterface.TAG_ORIENTATION, originalRotation);
compressedExifInterface.saveAttributes();
} catch (OutOfMemoryError error) {
return;
} finally {
if (out != null) try {
out.close();
} catch (IOException ignored) {
}

}
out = new FileOutputStream(file);
Matrix matrix = new Matrix();
bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(),
matrix, false);
bitmap.compress(Bitmap.CompressFormat.JPEG, quality, out);
ExifInterface compressedExifInterface = new ExifInterface(file.getAbsolutePath());
compressedExifInterface.setAttribute(ExifInterface.TAG_ORIENTATION, originalRotation);
compressedExifInterface.saveAttributes();
recalculateSize(image);
} catch (IOException e) {
e.printStackTrace(); //TODO proper exception handling
throw new PickerException(e);
} catch (OutOfMemoryError error) {
throw new PickerException("Out of memory while processing image: " + image);
} catch (Exception e) {
throw new PickerException("Error while processing image: " + image);
} finally {
close(scaledInputStream);
flush(out);
close(out);
}
}

Expand Down

0 comments on commit c733323

Please sign in to comment.