views:

55

answers:

1

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

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>

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

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)

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.

+1  A: 

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"   >
...
/>
Jorgesys
Thanks for the help with the Min and Max width. That did the trick, although I needed to put the ImageView inside a ScrollView if setting the width caused the image to exceed available height.
RMS2
Now, as for the image not downloading, (I used Joregsys's code) I'm running into all sorts of trouble. It seems that when the image is not displayed, what's going on is that I'm getting the error "SkImageDecoder::Factory returned null" when doing the .createFromStream method. Sometimes it works and sometimes I get that. I've tried closing the InputStream to flush the buffer, but no go. I've tried wrapping those lines of code in a while (d==null) loop, but then I just get an endless loop.
RMS2
I am also having memory issues when I rotate the screen. If the image loads the first time, I will get an OutOfMemoryError when orientation changes. The image is 1MB - small in terms of a digital photo, but maybe it's too big for Android? I think I'm getting memory leaks. I tried Image01.getDrawable().setCallback(null); in the activity's onDestroy(), but that doesn't help.
RMS2
These problems do seem to be related to image size. I've tried with smaller images (84k) and had no problems at all. A 680k image is hit or miss. It seems the bigger the filesize, the more trouble you run into. Is there something I can be doing differently to handle the larger images? Should I be saving the image locally and then opening it instead of just displaying the stream?
RMS2