Created
May 15, 2012 03:41
-
-
Save jfsurban/2698907 to your computer and use it in GitHub Desktop.
get thumbnail by scaling bitmaps bit.ly/JZW0Gj #android
This file contains hidden or 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
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