从URL显示图像 - 调整大小和屏幕方向的问题 [英] Display image from URL - sizing and screen orientation problems

查看:218
本文介绍了从URL显示图像 - 调整大小和屏幕方向的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从一个URL,这可能比屏幕尺寸更大的显示图像。我有样的工作,但我想它扩展到适合屏幕,而且我也有问题,屏幕的方向改变时。该图像是微小的,而且我想它扩展它的宽度在屏幕上也是如此。 (在这两种情况下,我想的形象来填充屏幕宽度的滚动条(如果有必要的高度)。

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).

下面是我的ImageView的:

Here is my ImageView:

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

下面是java code,它加载图像:(一些错误处理code删除为简单起见)

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

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

我已经尝试了不同的设置为Android:scaleType。我很抱歉,如果这个问题已经问过。我已经通过了一些关于这个问题的教程走了,但他们似乎并没有为我工作。不知道是否有任何与加载图像的方式。 (从网站而不是本地资源)

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)

另一个问题是,有时候图像甚至不负载。有没有运行时错误,我就什么也得不到的ImageView的。

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

请让我知道如果你需要更多的信息或澄清。

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

推荐答案

有关有时候图像甚至不加载是关系到环境的问题,所以我用这个函数来解决这个问题。

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

这样充满整个屏幕宽度的图像,你必须有一个code这样

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

更新:: 如果你明明加载一个大尺寸的图像,你将不得不等待更多的时间和下载的问题可能引起的UnknowHostException。

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

是的,你是对的,你会在本地保存图像,本地接入比下载速度更快。

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

要避免在旋转变化问题设置configChanges =keyboardHidden |定位财产您的Manifest.xml

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

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

这篇关于从URL显示图像 - 调整大小和屏幕方向的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆