Inline images with Text in android

Go To StackoverFlow.com

1

I'm attempting to download webpages for a user to view later. So far, I've downloaded the html, and the images. I can get the HTML to show up nicely formatted, but I cannot get the images to inline.

The code I'm using so far:

This is my method to get the article.

MyImageGetter mig = new MyImageGetter(this, urlId);

Spanned span = Html.fromHtml(contents[1], mig, null);
contents[1] = span.toString();

titleView.setText(contents[0]);
content.setText(contents[1]);

contents[] is an array that contains two strings. contents[0] is a simple string, contents[1] is a string with HTML markup.

MyImageGetter:

public class MyImageGetter implements Html.ImageGetter{

String urlId = null;
Context c = null;

public MyImageGetter(ArticleViewer articleViewer, String urlId2) {
    c = articleViewer;
    urlId = urlId2;
}

public Drawable getDrawable(String source) {
    String[] brokenUrl = source.split("/");
    String imgName = brokenUrl[brokenUrl.length-1];

    File image = new File("/data/data/com.theHoloDev.Reader/Offline/" + urlId + "/" + imgName);
    Log.w("MyApp", image.getAbsolutePath());
    Bitmap bm = BitmapFactory.decodeFile(image.getAbsolutePath());

    Drawable d = new BitmapDrawable(c.getResources(), bm);

    return d;
}

}

When I have it log image.getAbsolutePath() it comes up with a file that exists in ddms. The Text content is there perfectly, but there are still little black boxes that say obj in each image. I had thought a textview would still be able to display images in that fashion, but either I'm doing something wrong, or there is another way to do this.

2012-04-04 01:30
by The Holo Dev


0

I figured it out. Instead of:

Spanned span = Html.fromHtml(contents[1], mig, null);
contents[1] = span.toString();

content.setText(contents[1]);

I need:

Spanned span = Html.fromHtml(contents[1], mig, null);
content.setText(span);

I was running into problems setting span to string.

2012-04-04 17:13
by The Holo Dev


0

What is your desired end result? It is confusing to see that you are displaying html but you want the user to see the page... without html or did you do that for debugging? I assume the users will not see the html but rather the the page itself. In this case, I would go for a webview. http://developer.android.com/reference/android/webkit/WebView.html

I don't think the text view is meant to be used like you are using it.

Good luck.

2012-04-04 01:49
by roboto1986
I'm having trouble understanding your confusion. The end goal is to let the end user see the page without the HTML tags, however those HTML tags are used for formatting. That's where the span comes in. As for no webview, I'm attemtping to keep performance at a maximum, and Textview is faster than WebView. I'm fairly certain that I'm not doing anything too far out of TextViews bounds. There are examples I've seen of people doing this if I'm not mistaken. There is just something wrong with my implementation - The Holo Dev 2012-04-04 02:06
Could you post that article you are looking at - roboto1986 2012-04-04 02:53
What do you mean? There is no specific article. The user sends it an article - The Holo Dev 2012-04-04 04:36
You mentioned "examples" . Which examples? links, ect. - roboto1986 2012-04-04 15:34
Ah, sorry. There have been some SO questions related to it such as this one: http://stackoverflow.com/questions/2865452/is-it-possible-to-display-inline-images-from-html-in-an-android-textview among others - The Holo Dev 2012-04-04 17:11
Ads