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" > ... />
Jorgesys
source share