Reputation: 1369
I have a requirement of loading an Image from a URL in my Android application
For this I created an ImageView in my layout. Following is the Imageview
<ImageView
android:id="@+id/MyImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:layout_gravity="center_horizontal">
</ImageView>
And then in my code I use the following to load the image in this ImageView
ImageView bmImage = (ImageView)this.findViewById(R.id.MyImage);
String urldisplay = urls[0];
Bitmap mIcon11 = null;
try {
InputStream in = new java.net.URL(urldisplay).openStream();
mIcon11 = BitmapFactory.decodeStream(in);
} catch (Exception e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
bmImage.setImageBitmap(mIcon11);
The image that is lying on my URL path is of size 320 X 50. But very strange, when the image is shown in the ImageView, the size becomes very small, almost half the width and height
I have tried a lot but no solution. Can anybody help please?
Upvotes: 3
Views: 2688
Reputation: 717
use this
image loading library
and make sure to include this configuration in the DisplayImageOptions imageScaleType(ImageScaleType.EXACTLY_STRETCHED)
Upvotes: 0
Reputation: 1907
I think the problem is that you are setting bitmap pixels and expecting device pixels
Try to get the density of the screen then set the width/height so it stretches accordingly, like this:
ImageView bmImage = (ImageView)this.findViewById(R.id.MyImage);
String urldisplay = urls[0];
Bitmap mIcon11 = null;
try {
InputStream in = new java.net.URL(urldisplay).openStream();
mIcon11 = BitmapFactory.decodeStream(in);
} catch (Exception e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
bmImage.setImageBitmap(mIcon11);
if(mIcon11 != null){
int width = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, mIcon11.getWidth(), this.getResources().getDisplayMetrics());
int height = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, mIcon11.getHeight(), this.getResources().getDisplayMetrics());
bmImage.setMinimumWidth(width);
bmImage.setMinimumHeight(height);
}
Upvotes: 1