i had a data stream that contains images and text from html, the text is showing fine but images are just small placeholders. How can i get the proper images to show up? I was wondering if its just because i used a TextView for the messages.
question is there a view for both image and text
Page 1 of 15 Replies - 489 Views - Last Post: 12 September 2012 - 06:12 AM
#1
question is there a view for both image and text
Posted 10 September 2012 - 07:20 AM
Replies To: question is there a view for both image and text
#2
Re: question is there a view for both image and text
Posted 11 September 2012 - 06:48 AM
kabuto178, on 10 September 2012 - 08:20 AM, said:
i had a data stream that contains images and text from html, the text is showing fine but images are just small placeholders. How can i get the proper images to show up? I was wondering if its just because i used a TextView for the messages.
I don't believe that there is. You are going to have to define your own view, this can be done in XML and then inflated in your java file as and when needed. You can then download the bitmap from URL (note: this should be threaded if done on the main thread or will throw NetworkingOnMainThreadException in later versions of Android.) using code such as:
public Bitmap getBitmap(String bitmapUrl) {
try {
URL url = new URL(bitmapUrl);
return BitmapFactory.decodeStream(url.openConnection().getInputStream());
}
catch(Exception ex) {return null;}
}
Also, you should handle the possible exceptions better than I have above. Once this is done, you can then add the Bitmap to an ImageView in your xml file, and add any text to a text view as appropriate.
Hope this helps.
Edit: Thought I'd just add that what I said about isn't 100% accurate. I believe buttons are capable of holding an icon to the left, right, top, or bottom of the text that it holds, so you might be able to utilise this to your advantage. I think its something like android:drawableTop="@drawable/myImage", obviously you could replace top with either of the other positions. This might be fit for purpose depending on what you are doing.
This post has been edited by Anthaas: 11 September 2012 - 06:59 AM
#3
Re: question is there a view for both image and text
Posted 11 September 2012 - 04:36 PM
thanks for your reply, the data im getting is html data and it has this layout
so all the above would be in 1 string variable, could that method be applied to this?
<div class='messages'>Anthaas<span class='date'> 06:48 AM</span> <br /> hello, <img src='emotes/CA.gif' alt='smiley' title='kool'/> </div> <div class='messages'>Anthaas<span class='date'> 06:48 AM</span> <br /> bye, <img src='emotes/CA.gif' alt='smiley' title='kool'/> </div> <div class='messages'>Anthaas<span class='date'> 06:48 AM</span> <br /> sad, <img src='emotes/CA.gif' alt='smiley' title='kool'/> </div>
so all the above would be in 1 string variable, could that method be applied to this?
#4
Re: question is there a view for both image and text
Posted 12 September 2012 - 01:09 AM
Hmmm, my example probably won't work as the URL for the image doesn't seem to be full. But with regards to the text, yes, you would just have to parse the String variable to get out what you want to display.
#5
Re: question is there a view for both image and text
Posted 12 September 2012 - 05:06 AM
if the image had full URL would your suggestion still work?
#6
Re: question is there a view for both image and text
Posted 12 September 2012 - 06:12 AM
kabuto178, on 12 September 2012 - 06:06 AM, said:
if the image had full URL would your suggestion still work?
Yeah, as long as you parse the text and image URL out of the string you get from the HTML feed. Get the TextView and ImageView from your inflated view that I described earlier, set the text to the text you want to display, get the bitmap using the method I gave above by passing the full url into the methods parameter, and then set the ImageView's contents to this Bitmap.
Don't forget that you can't do any networking on the main thread, so this will need to be threaded, but being as it shouldn't take a device very long to do this, it can be done in AsyncTask.
Again, worth noting, is that you really should handle the exceptions better than I have above. It is bad practise to catch a general exception when specific exceptions are thrown. It makes it easier to debug, and easier for a user to understand why the app isn't working!
Page 1 of 1
|
|

New Topic/Question
Reply



MultiQuote




|