简体   繁体   English

从Android中的URL加载时,图像加载的大小很小

[英]Image loading small in size when loaded from URL in Android

I have a requirement of loading an Image from a URL in my Android application 我需要在Android应用程序中从URL加载图像

For this I created an ImageView in my layout. 为此我在我的布局中创建了一个ImageView。 Following is the Imageview 以下是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中加载图像

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 位于我的URL路径上的图像大小为320 X 50.但非常奇怪,当图像显示在ImageView中时,大小变得非常小,几乎是宽度和高度的一半

I have tried a lot but no solution. 我已经尝试了很多但没有解决方案。 Can anybody help please? 有人可以帮忙吗?

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);
 }

使用此图像加载库并确保在DisplayImageOptions imageScaleType(ImageScaleType.EXACTLY_STRETCHED)包含此配置

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM