1

I'm pretty new to Android Development and spent all day looking into issues with my ListAdapter, but couldn't find any answers that helped me solve my problem.

I Have a JSON NewsString parsed from an URL and need to load the Thumbnail into the ListView. I tried to Load it via Imageloader (https://github.com/novoda/ImageLoader#directloader-utility) and then convert the Bitmap to a Drawable so I can use it in my ListView (Did that before).

I Have the permissions in the Manifest:

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

And the JsonObject is parsed in an AsyncTask where I try to load the image from Url.

try {
    String url = e.getString("imageThumb");
    Bitmap bmp = new DirectLoader().download(url);
    Drawable d = new BitmapDrawable(null,bmp);
    Log.d("getBitmap", d.toString());
    row.put("thumb", d);
}catch (Exception e5) {
    Log.e("IMG", "Failed to load Thumbnail From URL: "+e5.toString());
    Drawable d = MainActivity.hweb_icon;
    row.put("thumb", d);
}

but I always get the Same Error Message: enter image description here

I tried several tutorials and other stuff, but it never worked out for me. The URL is correct and does not need any encoding.

My previous approach looked like this :

try {

    String url = e.getString("imageThumb");
    InputStream in = new java.net.URL(url).openStream();
    Bitmap bmp = BitmapFactory.decodeStream(in);                                                                                
    Drawable d = new BitmapDrawable(null,bmp);
    row.put("thumb", d);
}catch (Exception e5) {
    // handle it
    Log.e("IMG", "Failed to load Thumbnail From URL : "+e5.toString());

}

Sorry if this question was already answered, but I found many solutions and tried them, but none of them helped me out.

Thx

pwittke

EDIT: I created a new Testproject and the code below worked fine, so I started to deactivate every part that uses drawables somehow and turned out, that the SimpleAdapter cant load images that way... It can only load images from the resurces and not from an url... thx for your script britzl, it works fine, now I need to find a way to change my adapter for the images...

jophab
  • 5,356
  • 14
  • 41
  • 60
pwittke
  • 17
  • 1
  • 9

1 Answers1

0

There are many ways to load a Bitmap from a URL. If you want to roll your own you can try something like:

public Bitmap loadImage(String url) {
    DefaultHttpClient client = new DefaultHttpClient();
    Bitmap bitmap = null;
    try {
        HttpResponse response = client.execute(new HttpGet(url));
        HttpEntity entity = response.getEntity();
        if(entity != null) {
            InputStream in = entity.getContent();
            bitmap = BitmapFactory.decodeStream(in);
        }
    }
    catch (Exception e) {
    }
    return bitmap;
}

There's some things to consider, for instance if you run into Skia decoder errors you may have to wrap the stream. Read more here.

I would however recommend that you take a look at existing libraries. The up and coming Picasso library from Square is probably your best bet. Example:

Picasso.with(context).load("http://i.imgur.com/DvpvklR.png").into(imageView);
Community
  • 1
  • 1
britzl
  • 10,132
  • 7
  • 41
  • 38
  • thanks for your Answer I gonna try it tmr when im back at the office! :) – pwittke May 22 '13 at 17:35
  • I tried your function, but im ending up in the same error message: BitmapFactory | Unable to decode stream: java.io.FileNotFoundException: /android.graphics.drawable.BitmapDrawable@414d5140: open failed ENOENT (No such file or directory) System.out | resolveUri failed on bad uri: android:graphics.drawable.BitmapDrawable@414d5140 – pwittke May 23 '13 at 09:12
  • its all .jpg images from our own Server and the url is clean :/ from what I understand it seems like the bitmap gets lost before I transform it into a drawable ? – pwittke May 23 '13 at 09:13
  • Can you download an image from your browser using the exact same URL? Have you tried downloading an image from another source? Pick a random image off the web and try to download it, for instance: http://i0.kym-cdn.com/photos/images/original/000/000/578/1234931504682.jpg – britzl May 23 '13 at 17:45
  • the Code you gave me is working... the only problem was the SimpleAdapter... that doesnt accept Drawables/Bitmaps from an external source... so I had to modify the simpleadapter to do that, but after trying for a few hours I figured that out and its working now. Thanks for your help mate! – pwittke May 28 '13 at 08:34
  • 1
    @ pwittke , what was the modification u did on simple adapter because i have the same problem , unable to decode image ... –  May 08 '14 at 05:54