0

I'm trying to get an image from a Url into an ImageView. I've searched for it all day and I've tried lots of different solutions but none of them work for me...the image doesn't load. This is my code:

  String imageUrl = "http://www.photo-flash-maker.com/img/file_formats/jpg_file.jpg";

   iv.setImageDrawable(getImage(imageUrl));

 //...

  public static Drawable getImage(String url) {
    try {
        InputStream is = (InputStream) new URL(url).getContent();
        Drawable d = Drawable.createFromStream(is, "src");
        return d;
    } catch (Exception e) {
        return null;
    }
}

I found it here: How to display image from URL on Android because this seems to be the simplest solution but the image doesn't load at all. Any solutions?

Community
  • 1
  • 1
JoeyCK
  • 1,384
  • 4
  • 19
  • 29
  • 2
    Don't do downloading on the UI thread. Your app will ANR. Use / write something like https://github.com/nostra13/Android-Universal-Image-Loader – zapl Aug 21 '12 at 13:36

5 Answers5

1

you can use BitmapFactory

Bitmap bitmap;
try {
    bitmap = BitmapFactory.decodeStream(new URL(imageUrl).openConnection().getInputStream());
    Log.i("image", "retrieved");
} catch (Exception ex) {
    ex.printStackTrace();
}
ColdFire
  • 6,764
  • 6
  • 35
  • 51
  • Tried but I get this error: Unhandled exception type MalformedURLException then eclipse gives me the option to surround with try/catch, I do it and the app force closes :S – JoeyCK Aug 21 '12 at 13:32
  • creating new Url should be surrounded by try/catch..I updated my answer – ColdFire Aug 21 '12 at 13:41
  • are you testing on emulator or on a real device? if you're on emulator, verify that it's connected to internet – ColdFire Aug 21 '12 at 13:45
  • I'm testing on a real device. Now it seems to work but I think zapi's solution was better. Thank you very much anyways :) – JoeyCK Aug 21 '12 at 13:49
1

Have you set the permission in the manifest.xml file to allow your application to load an image from the Internet?

<manifest xlmns:android...>  
    ...  
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
</manifest> 

Also try this link: http://android-er.blogspot.co.uk/2010/06/load-imageview-with-bitmap-from.html

Contains all the xml changes you will need for your imageview

RossC
  • 1,200
  • 2
  • 11
  • 24
  • I had this same issue and it was the internet permission, it ran the app fine and just kept the image view blank. No error generated, logcat seemed fine too. – RossC Aug 21 '12 at 13:27
  • Yes I had set the permissions...I'll try the link you gave me thanks :) – JoeyCK Aug 21 '12 at 13:33
0

I think you'll get exactly what you need here

Manoj Kumar
  • 1,510
  • 3
  • 20
  • 40
0

I think the best solution was the one zapi suggested in a comment:

Don't do downloading on the UI thread. Your app will ANR. Use / write something like github.com/nostra13/Android-Universal-Image-Loader

However, the other solutions by A.A and RossC may also work.

JoeyCK
  • 1,384
  • 4
  • 19
  • 29
0

have you tried adding permission write external storage android:name="android.permission.WRITE_EXTERNAL_STORAGE"

just try this one two i am not sure whether it will help or not

Athul Harikumar
  • 2,501
  • 18
  • 16