Reputation: 564
I am trying to set bitmap of higher size to imageview of fixed height and width,
Imageview in xml
<ImageView
android:id="@+id/imgDisplay"
android:layout_width="320dp"
android:layout_height="180dp"
android:layout_marginLeft="10dp"
android:layout_marginTop="5dp"
android:contentDescription="@string/app_name" />
When i am using the following code error is avoided but the image appeared is blurred because of BitmapFactory.Options options,
BitmapFactory.Options options = new BitmapFactory.Options();
options.inPurgeable = true;
options.inSampleSize = 4;
Bitmap myBitmap = BitmapFactory.decodeFile(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES)+"/"+photo, options);
imgMainItem.setImageBitmap(myBitmap);
What else are the option available of setting a image of higher size and fixed height and width please help
Upvotes: 2
Views: 2873
Reputation: 23269
Don't used a fixed sample size. Calculate the sample size you need first, like so:
public static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {
// Raw height and width of image
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;
if (height > reqHeight || width > reqWidth) {
if (width > height) {
inSampleSize = Math.round((float)height / (float)reqHeight);
} else {
inSampleSize = Math.round((float)width / (float)reqWidth);
}
}
return inSampleSize;
}
Then, use it like so:
String path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES)+"/"+photo;
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(path, options);
// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, width, height);
options.inJustDecodeBounds = false;
Bitmap myBitmap = BitmapFactory.decodeFile(path, options);
imgMainItem.setImageBitmap(myBitmap);
width
and height
is your required width and height in pixels.
If your bitmap is a lot smaller than the size you want, you can't really scale it up without it being blurry. Use a higher-quality bitmap.
Upvotes: 2