Android 显示来自 URL 的图像 - 大小和屏幕方向问题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3601775/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 10:55:44  来源:igfitidea点击:

Display image from URL - sizing and screen orientation problems

androidimageviewscreen-orientation

提问by RMS2

I am trying to display an image from a URL, which may be larger than the screen dimensions. I have it kind of working, but I would like it to scale to fit the screen, and I also have problems when the screen orientation changes. The image is tiny, and I would like it to scale its width to the screen as well. (In both cases, I would like the image to fill the screen width with scrollbars (if necessary for height).

我正在尝试显示来自 URL 的图像,该图像可能大于屏幕尺寸。我有它的工作,但我希望它可以缩放以适应屏幕,并且当屏幕方向改变时我也会遇到问题。图像很小,我希望它也将其宽度缩放到屏幕。(在这两种情况下,我都希望图像用滚动条填充屏幕宽度(如果需要高度)。

Here is my ImageView:

这是我的 ImageView:

<ImageView android:id="@+id/ImageView01" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content"
    android:scaleType="fitCenter"
    android:adjustViewBounds="true">
</ImageView>

Here is the java code which loads the image: (some error handling code removed for simplicity)

这是加载图像的java代码:(为简单起见,删除了一些错误处理代码)

    Object content = null;
    try{
      URL url = new URL("http://farm1.static.flickr.com/150/399390737_7a3d508730_b.jpg");
      content = url.getContent();
    }
      catch(Exception ex)
    {
        ex.printStackTrace();
    }
    InputStream is = (InputStream)content;
    Drawable image = Drawable.createFromStream(is, "src");
    Image01.setImageDrawable(image);

I have tried different settings for android:scaleType. I'm sorry if this question has been asked before. I've gone through a number of tutorials on the subject, but they don't seem to work for me. Not sure if it has anything to do with the way the image is loaded. (from the web instead of a local resource)

我为 android:scaleType 尝试了不同的设置。如果之前有人问过这个问题,我很抱歉。我已经阅读了许多关于该主题的教程,但它们似乎对我不起作用。不确定它是否与图像加载方式有关。(来自网络而不是本地资源)

Another issue is that sometimes the image doesn't even load. There are no runtime errors, I just get nothing in the ImageView.

另一个问题是有时图像甚至无法加载。没有运行时错误,我只是在 ImageView 中什么也没有。

Please let me know if you need more information or clarification.

如果您需要更多信息或说明,请告诉我。

回答by Jorgesys

the issue about that "sometimes the image doesn't even load" is related to the context so I used this functions to solve that issue

关于“有时图像甚至无法加载”的问题与上下文有关,因此我使用此函数来解决该问题

public Object fetch(String address) throws MalformedURLException,
    IOException {
        URL url = new URL(address);
        Object content = url.getContent();
        return content;
    }  

    private Drawable ImageOperations(Context ctx, String url) {
        try {
            InputStream is = (InputStream) this.fetch(url);
            Drawable d = Drawable.createFromStream(is, "src");
            return d;
        } catch (MalformedURLException e) {
            return null;
        } catch (IOException e) {
            return null;
        }
    }

so to fill the screen width with your image you must have a code like this

所以要用你的图像填充屏幕宽度,你必须有这样的代码

    try{
        String url = "http://farm1.static.flickr.com/150/399390737_7a3d508730_b.jpg";           
        Drawable image =ImageOperations(this,url);
        Image01.setImageDrawable(image);
    }
    catch(Exception ex)
    {
        ex.printStackTrace();
    }


    Image01.setMinimumWidth(width);
    Image01.setMinimumHeight(height);

    Image01.setMaxWidth(width);
    Image01.setMaxHeight(height);

UPDATE:: if you load a big size image obviously you will have to wait more time, and download problems could be caused for UnknowHostException.

更新::如果你加载一个大尺寸的图像显然你将不得不等待更多的时间,下载问题可能会导致 UnknowHostException。

yes you are right you will save your image locally, the local access is faster than the download.

是的,您是对的,您将在本地保存图像,本地访问比下载快。

to avoid problems on rotation change set your configChanges="keyboardHidden|orientation" property inside your Manifest.xml

为避免旋转更改出现问题,请在 Manifest.xml 中设置 configChanges="keyboardHidden|orientation" 属性

<activity android:name=".myActivity"
...
         android:configChanges="keyboardHidden|orientation"   >
...
/>