1

I am retrieving image from url and display them in imageview. but my code is giving me null message.

ImageView imageview = (ImageView) findViewById(R.id.imageView1);
try {
    Uri uri=Uri.parse("http://10.0.2.2:8083/call/rahul.jpg");
    // uri.buildUpon().appendQueryParameter("key", "Android developer");
    URI u = new URI(uri.toString());
    System.out.println("path is "+u);
    HttpClient httpclient = new DefaultHttpClient();
    HttpGet httpget = new HttpGet();
    httpget.setURI(u);
    //httpget.setEntity(new UrlEncodedFormEntity(nameValuePairs));
    HttpResponse response = httpclient.execute(httpget);
    HttpEntity entity = response.getEntity();
    is = entity.getContent(); 

    Bitmap bitmap = BitmapFactory.decodeStream(is);
    imageview.setImageBitmap(bitmap); 
} catch(Exception e) {
    System.out.println("this is the error "+e.getMessage()); 
}

logcat:-

02-04 19:32:35.355: D/dalvikvm(679): threadid=1: still suspended after undo (sc=1 dc=1)
02-04 19:32:35.355: I/dalvikvm(679): Wrote stack traces to '/data/anr/traces.txt'
02-04 19:32:35.895: I/dalvikvm(679): threadid=3: reacting to signal 3
02-04 19:32:35.915: D/dalvikvm(679): threadid=1: still suspended after undo (sc=1 dc=1)
02-04 19:32:35.915: I/dalvikvm(679): Wrote stack traces to '/data/anr/traces.txt'
02-04 19:32:36.425: I/dalvikvm(679): threadid=3: reacting to signal 3
02-04 19:32:36.445: D/dalvikvm(679): threadid=1: still suspended after undo (sc=1 dc=1)
02-04 19:32:36.445: I/dalvikvm(679): Wrote stack traces to '/data/anr/traces.txt'
02-04 19:32:36.992: I/dalvikvm(679): threadid=3: reacting to signal 3
02-04 19:32:37.005: D/dalvikvm(679): threadid=1: still suspended after undo (sc=1 dc=1)
02-04 19:32:37.005: I/dalvikvm(679): Wrote stack traces to '/data/anr/traces.txt'
02-04 19:32:37.565: I/dalvikvm(679): threadid=3: reacting to signal 3
02-04 19:32:37.585: D/dalvikvm(679): threadid=1: still suspended after undo (sc=1 dc=1)
02-04 19:32:37.585: I/dalvikvm(679): Wrote stack traces to '/data/anr/traces.txt'
02-04 19:32:38.105: I/dalvikvm(679): threadid=3: reacting to signal 3
02-04 19:32:38.125: D/dalvikvm(679): threadid=1: still suspended after undo (sc=1 dc=1)
02-04 19:32:38.125: I/dalvikvm(679): Wrote stack traces to '/data/anr/traces.txt'
02-04 19:32:38.645: I/dalvikvm(679): threadid=3: reacting to signal 3
02-04 19:32:38.665: D/dalvikvm(679): threadid=1: still suspended after undo (sc=1 dc=1)
02-04 19:32:38.665: I/dalvikvm(679): Wrote stack traces to '/data/anr/traces.txt'
02-04 19:32:39.175: I/dalvikvm(679): threadid=3: reacting to signal 3
02-04 19:32:39.195: D/dalvikvm(679): threadid=1: still suspended after undo (sc=1 dc=1)
02-04 19:32:39.205: I/dalvikvm(679): Wrote stack traces to '/data/anr/traces.txt'
02-04 19:32:39.695: I/dalvikvm(679): threadid=3: reacting to signal 3
02-04 19:32:39.716: D/dalvikvm(679): threadid=1: still suspended after undo (sc=1 dc=1)
02-04 19:32:39.716: I/dalvikvm(679): Wrote stack traces to '/data/anr/traces.txt'
Rahul Rawat
  • 999
  • 2
  • 17
  • 40
  • Can you post the log please? – Milos Cuculovic Feb 04 '13 at 13:46
  • check this one http://stackoverflow.com/questions/8992964/android-load-from-url-to-bitmap – mihail Feb 04 '13 at 13:48
  • on which line are you recieving NUll pointer exception? – baboo Feb 04 '13 at 13:48
  • http://stackoverflow.com/questions/2471935/how-to-load-an-imageview-by-url-in-android – Nirav Ranpara Feb 04 '13 at 13:49
  • A few tips: 1) don't catch Exception, catch more specific types. 2) If you don't know what to do when an exception occurs, either don't catch it, or catch it then throw a new one that wraps it and represents the problem at a level of abstraction appropriate for this block. 3) Don't use System.out directly - use the appropriate logging system for your environment. In android that's the Log class. 3) When you catch an exception, don't lose the stack trace. In android you could log it with something like Log.e(LOG_TAG, MESSAGE, EXCEPTION). From the stack trace the problem would be obvious. – themightyjon Feb 04 '13 at 14:00
  • Yeah, so because all you're logging is the message of the exception, you can't see what the problem is. An exception doesn't have to have a message. It will have a stack trace though. While I'm at it, another tip for you: keep your try-catch blocks small. Something is being thrown somewhere in a huge block that is doing too much, and you have one point to catch whatever that might be, whatever line it may come from. You're doing too much in that block. – themightyjon Feb 04 '13 at 14:01
  • There is no exception in your log. As @themightyjon says, you don't have to put your exception in a message, you can leave it like that and this will be written in the log. – Milos Cuculovic Feb 04 '13 at 14:03
  • suspend exception is throw now. please check my updated logcat – Rahul Rawat Feb 04 '13 at 14:05
  • This updated logcat output now doesn't contain anything obviously related to your code above, let alone the stack trace. Unless you've just posted the wrong part of the output, I think we'd need to see the whole app to know what you're doing. Since you seem like you might be a long way off a working solution, and this problem has been solved many times before, might I suggest you use an existing library to do this? For example, [android-query](http://code.google.com/p/android-query/#Image_Loading) will give you this functionality and lots more useful stuff. – themightyjon Feb 04 '13 at 16:06
  • thank you all of you . themightyjon:- Thank you nice man . your given library "android-query " is very help full for me . is it free ? i used "AQuery aq = new AQuery(convertView); aq.id(imageview).image("http://www.vikispot.com/z/images/vikispot/android-w.png"); " in my list view for display images in a list . is it correct? or i need to change some thing such as recycle(). – Rahul Rawat Feb 05 '13 at 09:35

2 Answers2

0

Try this:

ImageView imageview = (ImageView) findViewById(R.id.imageView1);
mBitmap = BitmapFactory.decodeStream((InputStream) new URL(http://10.0.2.2:8083/call/rahul.jpg).getContent());
imageview .setImageBitmap(mBitmap);
Archie.bpgc
  • 23,812
  • 38
  • 150
  • 226
0

Well since you've said that what I suggested worked well for you, I guess I should submit it as an answer. I observed that you seem to be a long way off a working solution, and that since this problem has been solved many times before you might wish to use a library such as android-query. I have no affiliation with that library - I've just used it myself before. Also, since I didn't technically answer your question, although I solved your problem, I'd better do a little more for the green tick, eh?

Your usage of the library looks fine, and you don't need to call recycle (you can find that on Google easily btw). Note that you can do lots of other nice stuff with it too, such as fading in the images when they load, downsampling them as they load if they are very large, and more. You can find lots of examples on the android-query site.

As for your query about whether it is free, the license is declared on the main page I linked to, on the left hand side near the top. It's the Apache License 2.0. In short this license allows you to use the library freely, as long as you include a copy of the Apache License in any distribution of your app and clearly mention any libraries you use under the license. Or you can read the blurb from their FAQ. You would typically put in an about screen in your app for this kind of thing, that clearly says you are using the android-query library under the Apache 2.0 license, with a viewable copy of the license. Proper attribution: I think you'll agree it's a small price to pay for so many useful things you can find under this license :o)

Good luck with your app!

themightyjon
  • 1,241
  • 11
  • 12