Skip to content

Instantly share code, notes, and snippets.

@DHuckaby
Created February 12, 2013 23:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DHuckaby/4774437 to your computer and use it in GitHub Desktop.
Save DHuckaby/4774437 to your computer and use it in GitHub Desktop.
PhotupBitmapOptionsFactory.java
public class PhotupBitmapOptionsFactory implements BitmapOptionsFactory {
private static int MAX_DIMEN = 512;
@Override
public Options newOptionsFromStream(InputStream inputStream, int width, int height) {
BitmapFactory.Options bitmapFactoryOptions = new BitmapFactory.Options();
final int maxDim;
if (height > 0 && width > 0) {
maxDim = Math.max(height, width);
} else {
maxDim = MAX_DIMEN;
}
bitmapFactoryOptions.inJustDecodeBounds = true;
try {
BitmapFactory.decodeStream(inputStream, null, bitmapFactoryOptions);
} catch (SecurityException ignore) {}
final int origWidth = bitmapFactoryOptions.outWidth;
final int origHeight = bitmapFactoryOptions.outHeight;
bitmapFactoryOptions.inJustDecodeBounds = false;
bitmapFactoryOptions.inScaled = false;
bitmapFactoryOptions.inPurgeable = true;
bitmapFactoryOptions.inInputShareable = true;
bitmapFactoryOptions.inDither = true;
bitmapFactoryOptions.inPreferredConfig = Bitmap.Config.RGB_565;
if (origWidth > maxDim || origHeight > maxDim) {
int k = 1;
int tmpHeight = origHeight, tmpWidth = origWidth;
while ((tmpWidth / 2) >= maxDim || (tmpHeight / 2) >= maxDim) {
tmpWidth /= 2;
tmpHeight /= 2;
k *= 2;
}
bitmapFactoryOptions.inSampleSize = k;
}
return bitmapFactoryOptions;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment