Pregunta

Basically, I want to generate a report from some data that the user provides my app. My first thought was to generate a PDF file. That turned out to be infeasible because the only free library doesn't support features like text wrapping, while licenses for the paid libraries cost ~$1000.

So I have generated an html file with all the content, and if I open this file in desktop Chrome, it renders fine. When the user clicks on a share button, the app should launch an intent to get either the default browser or mobile Chrome to open the html file.

I know how to open a url with a browser via an intent, the problem is opening a file. The HTML file is being accessed via a FileProvider (so a uri eg: "content://[package].fileprovider/[filename].html"). If I run myIntent.setData(htmlFileUri); and launch the intent, no browsers show up in the dialog, only text editors. Same thing if I use setDataAndType.

Next I tried a Data Uri: data:text/html;base64,[html as base64]. Again, if I copy this string into desktop Chrome it works fine. However if I start an intent with this uri, I get

android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW dat=data:text/html;base64,[base64]== typ=text/html }.

Finally, if I append http:// to the beginning of the data uri, I get the option to open chrome or the default browser. If I do, nothing gets rendered, not even the page url.

Is there anything else I can try, or any suggestions in general? Thanks in advance

¿Fue útil?

Solución

AFAIK the built-in HtmlViewer app does not work reliably by setting the data part of your Intent to text/html. Instead, you could

  • Copy the HTML to external storage and then set the URI of your Intent to file:///mnt/sdcard/path/to/file.html
  • Create an Activity with a WebView and use the loadData() method to load HTML within your app.
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top