Android WebView not fully decoding String - android

I have a very simple WebView that needs to display text as HTML. I retrieve data from a server that is in a byte array. I need to display that byte array as readable text.
var wv = FindViewById<WebView>(Resource.Id.webview);
var container = AutoFac.Container;
_routinesService = container.Resolve<IRoutinesService>();
byte[] documentHtml = _routinesService.GetFragment(documentId);
string mimeType = "text/html";
string encoding = "utf-8";
var html = System.Text.Encoding.UTF8.GetString(documentHtml);
wv.LoadDataWithBaseURL("", html, mimeType, encoding, "");
I try to convert the byte array to a String, and the variable html becomes \"U3RlcCAxOiBZb3UgYXJlIGEgd2lubmVyLg==\"
This looks like Base64 to me, and when I use an online converter (https://www.base64decode.org/), it does convert to the correct text. But the output on the screen in the WebView just looks like "U3RlcCAxOiBZb3UgYXJlIGEgd2lubmVyLg=="
Any ideas what I might be missing?
Just for more information, this is in Xamarin.Android so the code is C# instead of Java (but there is not much difference).

Have you tried javascript
atob()
method that decodes a string of data which has been encoded using base-64 encoding before posting it to webview.

Related

Uri.encode() encodes wrong

So, I need to encode my url.
For that I use Uri.encode():
private const val CHARS= "##&=*+-_.,:!?()/~'%"
if (query != null) {
query = Uri.encode(query, Chars)
}
But, it encodes weirdly... [ is %255B, when it should be and ] is %255D, when it should be %5D
Update: Turns out Uri.encode() works just fine. The problem is how I build the url. I do it by using HttpUrl and after I encode query I do HttpUrl.build() which encodes the url second time?
URLEncoder.encode(query, "UTF-8");
The URL encoding is can be Percent-encoding which encodes with %. Provide "UTF-8" in chars it will work. Hope this will work for you.
You seems to be calling android.net.Uri's:
public static String encode (String s, String allow)
From the documentation, this:
Encodes characters in the given string as '%'-escaped octets using the
UTF-8 scheme. Leaves letters ("A-Z", "a-z"), numbers ("0-9"), and
unreserved characters ("_-!.~'()*") intact. Encodes all other
characters with the exception of those specified in the allow
argument.
So you need pass the URL string as the s parameter. And it will return an encoded version of the url suitable for use as a URI component.

How to open blob link use webview of android?

I want to display video in android app.
The link of video is blob link so that i think i need use webview to display it.
How to implement it?
WebView support loading HTML data from string. So following code is possible.
String videoBlobLinkUrl = "data:video/mp4;base64,[video base64-encoded data ..]";
String videoBlobMime = "video/mp4";
String videoHtml = "<html><body><video><source type=\""+videoBlobMime+"\" src=\""+ videoBlobLinkUrl +"\"></video></body></html>";
webview.loadData(videoHtml, "text/html", null);

Sending an image through a socket from python to android

I currently have a python socket server that captures from a webcam using opencv. I have converted the captured image using image.tostring();
I am trying to send this image to an android application and have it open in a web view. I can get the text string to open, but cannot make it back into an image.
This is the only way that I can get anything to display in the web view :
webv.loadData(html, "text/html", "utf-8");
The data is transferring to the android device, so I know that the socket is working fine, I just cannot figure out the translation.
Any suggestions?
EDIT based on comments below:
Ok. So I now converted the string to base64 in python, before sending it.
I tried using bitmapfactory to add the image to an ImageView using:
byte[] decodedString = Base64.decode(result, Base64.DEFAULT);
Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);
but I get the error
SkImageDecoder:: Factory returned null
I also tried a webview with the following, which gives me a broken image icon
String html = "<img src=\"data:image/jpeg;base64,"+result+"\" />";
webv.loadData(html, "text/html", "utf-8");
These give me a blank webview
String html = "<img src=\"data:image/jpeg;base64,"+result+"\" />";
webv.loadData(html, "iamge/jpeg", "base64");
String html = "<img src=\"data:image/jpeg;base64,"+result+"\" />";
webv.loadData(html, "text/html", "base64");
webv.loadData(result, "image/jpeg","base64");
It seems that the problem is in my python code, as the base64 string exported to a text file cannot be reformed into an image, it "contains errors"
applicable code fragments:
img = cv.QueryFrame(cam)
imgstr = base64.b64encode(img.tostring())
conn = s.accept()
conn.send(imgstr)

The text from Android EditText becomes garbage characters in WebView

I am going to get the text in an EditText and then display the text in a WebView. The following code works for ASCII characters. For non-ASCII characters, the text in WebView becomes garbage characters.
String input = mEditText.getText().toString();
String html = makeHTML(input); // append HTML elements and headers including MIME and ENCODING header
mWebView.loadData(html, "text/html", "utf-8");
I thought that I was doing something wrong with my HTML, so I try to display the text directly in the WebView without modify the text. However, the result was the same.
String input = mEditText.getText().toString();
mWebView.loadData(input, "text/html", "utf-8");
The makeText() of Toast which displays non-ASCII text in EditText without any problem.
Does anyone know the answer?
WebView might not be able to load certain "unsafe" HTML characters. Try using:
String input = mEditText.getText().toString();
String html = makeHTML(input);
String encodedHtml = URLEncoder.encode(html,"UTF-8");
mWebView.loadData(encodedHtml, "text/html", "utf-8");
The URLEncoder.decode(encodedHtml,"UTF-8") method might also be useful.
Finally, I solve the problem by using loadDataWithBaseURL(baseUrl, data, mimeType, encoding, historyUrl) of WebView
mWebView.loadData(data, mimeType, encoding)
mWebView.loadDataWithBaseURL("", data, mimeType, encoding, "")
seems to be same but actually does not.
In my case, loadData() failed to encode the characters properly and failed to load images saved in the asset folder.

Load a div into a webview on Android

I have an Android app, which contains a WebView, and I would like to display in it not a webpage, but only a div from that webpage. I should mention that I do not have access to that page.
I would recommend Jsoup. It's larger than tagsoup but provides inbuilt functionality to pull the HTML from the URL and is super easy to use. For example if you want to pull a div with id example you would do the following:
Document doc = Jsoup.connect(url).get();
Elements ele = doc.select("div#example");
Then to load the HTML you've extracted into your web view you would do:
String html = ele.toString();
String mime = "text/html";
String encoding = "utf-8";
webView.loadData(html, mime, encoding);
You'll need to load the HTML of the page yourself, extract the div contents and pass it to the WebView as a string. You may find an HTML parser library (e.g. tagsoup) useful for this.

Categories

Resources