Reputation: 1222
I want user to select a picture from gallery. Since size of most pictures are very large I want to scale it to the most appropriate width. Is there a way to do that when I only know the path of the picture?
Upvotes: 1
Views: 909
Reputation: 23962
For example, if your Bitmap is bmp, and ImageView is framePhoto:
int iWidth=bmp.getWidth();
int iHeight=bmp.getHeight();
Display display = getWindowManager().getDefaultDisplay();
DisplayMetrics dm = new DisplayMetrics();
display.getMetrics(dm);
int dWidth=dm.widthPixels;
int dHeight=dm.heightPixels;
float sWidth=((float) dWidth)/iWidth;
float sHeight=((float) dHeight)/iHeight;
if(sWidth>sHeight) sWidth=sHeight;
else sHeight=sWidth;
Matrix matrix=new Matrix();
matrix.postScale(sWidth,sHeight);
newImage=Bitmap.createBitmap(bmp, 0, 0, iWidth, iHeight, matrix, true);
framePhoto.setImageBitmap(newImage);
Upvotes: 2