Display image from URL - sizing and screen orientation problems

独自空忆成欢 提交于 2019-11-30 09:45:33

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.

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

<activity android:name=".myActivity"
...
         android:configChanges="keyboardHidden|orientation"   >
...
/>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!