1

I want to get an HTML from a web page, remove some tags from the code and display it using a TextView... But those HTMLs are too big to be temporaly stored into a String...

When I try this way:

String html = "myBigHTML";
myTextView.setText(fromHtml(html));

compiler says error: constant string too long

If I put the html into a .txt and try this way:

InputStream is = getAssets().open("html.txt");

tvTeste.setText(fromHtml(convertStreamToString(is)));

public static String convertStreamToString(InputStream is) throws Exception {
    BufferedReader reader = new BufferedReader(new InputStreamReader(is));
    StringBuilder sb = new StringBuilder();
    String line = null;
    while ((line = reader.readLine()) != null) {
        sb.append(line).append("\n");
    }
    reader.close();
    return sb.toString();
}

It works but the app gets soooo slow, almost freezes... And also, if I store it in a .txt I couldn't work with the tags...

.:: EDIT ::.

My onCreate() method as asked...

private TextView tvTeste;
private InputStream is;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.layout_frequencia);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    getSupportActionBar().setHomeButtonEnabled(true);

    tvTeste = (TextView)findViewById(R.id.tvTeste);

    try {
        is = getAssets().open("html.txt");
    } catch (IOException e) {
        e.printStackTrace();
    }

    BufferedReader br = new BufferedReader(new InputStreamReader(is));

    String strLine;

    List<String> stringList = new ArrayList<>();
    try {
        while ((strLine = br.readLine()) != null)   {
            stringList.add(strLine);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }

    tvTeste.setText(fromHtml(TextUtils.join("",stringList)));
}
  • In addition to the accepted answer you may find an alternative solution [here](http://stackoverflow.com/questions/31837840/paginating-text-in-android/32096884#32096884) – Onik Mar 07 '17 at 21:10

2 Answers2

0

Let's try this: each line of HTML text is a String. Each String is inside a List of String.
So, some pseudocode:

List<String> stringList = new ArrayList<>();

while (htmlHandler.next()) {
   stringList.add(fromHtml(htmlHandler.readLine()));
}

myTextView.setText(joinStringArray(stringList));

Where joinStringArray uses a StringBuilder to produce a single big String object.
Basically you shouldn't read the entire web page, but you should read it sequentially.

LppEdd
  • 20,274
  • 11
  • 84
  • 139
  • I've done it using `TextUtils.join("",list)`... It works but the application gets extremely slow also – Thales Lima Mar 07 '17 at 18:52
  • Mmmh but how do you load the list? – LppEdd Mar 07 '17 at 18:53
  • It's loaded at `onCreate()` method... Maybe I could put some "Loading dialog" on it? – Thales Lima Mar 07 '17 at 18:56
  • Well for sure a loading message or icon is perfectly fine. Can you post the onCreate() code? I'd like to see how you did it,if possible. – LppEdd Mar 07 '17 at 18:58
  • OK, as I thought you are still reading from the text file. How do you produce the text file? Instead of outputting to the. txt, output to the List. I'm at the gym so I'm using the phone. When I come home I'll give you an example. – LppEdd Mar 07 '17 at 19:06
  • Alright... Actually, I didn't make the code that creates the .txt yet... I can make a code to push the web page's HTML to the list, directly... But I will also need to get it from .txt files sometimes in my project – Thales Lima Mar 07 '17 at 19:08
  • If you are able to push it into the List, that's the best solution – LppEdd Mar 07 '17 at 19:11
  • Ok, I'm gonna do that then... But the "most viewed" pages will be locally stored in the app, using a .txt... So this method will also be used... Thank you! – Thales Lima Mar 07 '17 at 19:13
  • Remember that there are standard ways to cache pages. – LppEdd Mar 07 '17 at 19:15
  • What do you mean by that? – Thales Lima Mar 07 '17 at 19:16
0

Another point to mark. You should avoid any time consuming process that blocks the activity. try the same using, for example an AsyncTask. Please check https://developer.android.com/reference/android/os/AsyncTask.html

Walter Palladino
  • 479
  • 1
  • 3
  • 8