Skip to content

Instantly share code, notes, and snippets.

@jfsurban
Created May 15, 2012 03:41
Show Gist options
  • Save jfsurban/2698907 to your computer and use it in GitHub Desktop.
Save jfsurban/2698907 to your computer and use it in GitHub Desktop.
get thumbnail by scaling bitmaps bit.ly/JZW0Gj #android
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
public class BitmapUtil {
public static Bitmap getBitmap(Context context,String photoUriPath) throws Exception {
Uri photoUri = Uri.parse(photoUriPath);
InputStream photoStream = context.getContentResolver().openInputStream(photoUri);
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize=2;
Bitmap photoBitmap = BitmapFactory.decodeStream(photoStream,null,options);
int h = photoBitmap.getHeight();
int w = photoBitmap.getWidth();
if((w>h)&&(w>128)){
double ratio = 128d/w;
w=128;
h=(int)(ratio*h);
}
else if((h>w)&&(h>128)){
double ratio = 128d/h;
h=128;
w=(int)(ratio*w);
}
Bitmap scaled = Bitmap.createScaledBitmap(photoBitmap, w, h, true);
photoBitmap.recycle();
return scaled;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment