Created
January 3, 2016 14:14
-
-
Save ar-android/4c31812d3e06fcd06db0 to your computer and use it in GitHub Desktop.
Convert uri to Bitmap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public Bitmap getThumbnail(Uri uri) throws FileNotFoundException, IOException { | |
InputStream input = this.getContentResolver().openInputStream(uri); | |
BitmapFactory.Options onlyBoundsOptions = new BitmapFactory.Options(); | |
onlyBoundsOptions.inJustDecodeBounds = true; | |
onlyBoundsOptions.inDither=true;//optional | |
onlyBoundsOptions.inPreferredConfig=Bitmap.Config.ARGB_8888;//optional | |
BitmapFactory.decodeStream(input, null, onlyBoundsOptions); | |
input.close(); | |
if ((onlyBoundsOptions.outWidth == -1) || (onlyBoundsOptions.outHeight == -1)) | |
return null; | |
int originalSize = (onlyBoundsOptions.outHeight > onlyBoundsOptions.outWidth) ? onlyBoundsOptions.outHeight : onlyBoundsOptions.outWidth; | |
double ratio = (originalSize > THUMBNAIL_SIZE) ? (originalSize / THUMBNAIL_SIZE) : 1.0; | |
BitmapFactory.Options bitmapOptions = new BitmapFactory.Options(); | |
bitmapOptions.inSampleSize = getPowerOfTwoForSampleRatio(ratio); | |
bitmapOptions.inDither=true;//optional | |
bitmapOptions.inPreferredConfig=Bitmap.Config.ARGB_8888;//optional | |
input = this.getContentResolver().openInputStream(uri); | |
Bitmap bitmap = BitmapFactory.decodeStream(input, null, bitmapOptions); | |
input.close(); | |
return bitmap; | |
} | |
private static int getPowerOfTwoForSampleRatio(double ratio){ | |
int k = Integer.highestOneBit((int)Math.floor(ratio)); | |
if(k==0) return 1; | |
else return k; | |
} |
Please, what is the value of THUMBNAIL_SIZE ??
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
How much is the value of THUMBNAIL_SIZE?