Display an image using URLs and screen orientation tasks

I am trying to display an image from a URL that may be larger than the screen size. This works for me, but I would like it to scale to fit the screen, and I also have problems when the screen orientation changes. The image is small, and I would like it to scale its width on the screen. (In both cases, I would like the image to fill the width of the screen with scroll bars (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 that loads the image: (some error handling codes have been 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 tried different settings for android: scaleType. I apologize if this question was asked before. I went through a series of tutorials on this, but they don't seem to work for me. Not sure if it has anything to do with the way the image is uploaded. (from the Internet instead of a local resource)

Another problem is that sometimes the image does not even load. There are no runtime errors, I just don't get anything in ImageView.

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

+6
android screen-orientation imageview
source share
1 answer

the problem that โ€œsometimes the image doesnโ€™t even loadโ€ is related to the context, so I used these functions to solve this problem.

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

in order to fill the width of the screen with your image, you must have this code

  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 are uploading a large image, obviously you will have to wait longer and loading problems may be caused for UnknowHostException.

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

to avoid problems with rotation changes, set the configChanges = "keyboardHidden | orientation" property inside your Manifest.xml

 <activity android:name=".myActivity" ... android:configChanges="keyboardHidden|orientation" > ... /> 
+9
source share

All Articles