Skip to content

Commit

Permalink
Make loading data into WebView more correct.
Browse files Browse the repository at this point in the history
  • Loading branch information
rdoeffinger committed Dec 5, 2015
1 parent c77b092 commit f59c858
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions src/com/hughes/android/dictionary/HtmlDisplayActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,16 @@
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarActivity;
import android.util.Base64;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

import com.hughes.util.StringUtil;

import java.io.UnsupportedEncodingException;

public final class HtmlDisplayActivity extends ActionBarActivity {

static final String LOG = "QuickDic";
Expand Down Expand Up @@ -68,14 +71,21 @@ public void onCreate(final Bundle savedInstanceState) {
actionBar.setDisplayHomeAsUpEnabled(true);

final int htmlRes = getIntent().getIntExtra(HTML_RES, -1);
final String html;
String html;
if (htmlRes != -1) {
html = StringUtil.readToString(getResources().openRawResource(htmlRes));
} else {
html = getIntent().getStringExtra(HTML);
}
final MyWebView webView = (MyWebView) findViewById(R.id.webView);
webView.loadData(html, "text/html", "utf-8");
try {
// No way to get pure UTF-8 data into WebView
html = Base64.encodeToString(html.getBytes("UTF-8"), Base64.DEFAULT);
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e);
}
// Use loadURL to allow specifying a charset
webView.loadUrl("data:text/html;charset=utf-8;base64," + html);
webView.activity = this;

final String textToHighlight = getIntent().getStringExtra(TEXT_TO_HIGHLIGHT);
Expand Down

0 comments on commit f59c858

Please sign in to comment.