Reputation: 18607
I'm trying to start an intent to load a local web page in a browser for Android Lollipop:
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse("file:///android_asset/my_page.html"));
startActivity(i);
Unfortunately, I get that the page can't be found. I later found that the default browser only supports HTTP and HTTPS requests.
Does this mean I have to start a local web server on the user's phone to load my_page.html via a web intent? :/ Such local servers exist at least, but seem too heavyweight for my usage.
I can't use a web view since web views don't let users download object URLs, which my app relies on (I tried writing a DownloadListener
that uses DownloadManager to no avail.).
I could simply host the static page on a remote server, but then my app would be unusable offline ... unless I use App Cache. However, using App Cache is suboptimal since the user's first usage of the app better be when he/she is online ... otherwise, the first load fails.
Any ideas on how to open a local asset page via a web intent?
Upvotes: 1
Views: 5056
Reputation: 12335
Give this a try, this forces the file provided to be launched in the Android web browser:
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.addCategory(Intent.CATEGORY_BROWSABLE);
intent.setDataAndType(Uri.parse("file:///android_asset/my_page.html"), "text/html");
startActivity(intent);
You can also specify the File
earlier in the code and then use this instead of the previous third line:
intent.setDataAndType(Uri.fromFile(file), "text/html");
Upvotes: 1
Reputation: 1006539
Unfortunately, I get that the page can't be found
Correct. You are telling a third-party app to load a page from it's own app's assets, and there is no such page. file:///android_asset/
only loads stuff from your app when your process is the one interpreting the Uri
, not when you hand such a Uri
to a third-party app.
I later found that the default browser only supports HTTP and HTTPS requests.
There are thousands of Android device models. There will be more than one "default browser" implementation across all of them. That being said, while it is possible that a browser implementation might support file
(for actual files on the filesystem that happen to be accessible by the browser) or content
(for serving material from your app via a ContentProvider
), not all will.
Does this mean I have to start a local web server on the user's phone to load my_page.html via a web intent?
If by "web intent", you mean an ACTION_VIEW
Intent
, then yes, if you want fairly universal solution. Or, have the page be available on an off-device Web server.
Note that securing a local Web server is tricky business.
I can't use a web view since web views don't let users download object URLs, which my app relies on
I am not certain what you mean by "download object URLs". You might consider asking a separate Stack Overflow question, where you go into this in more detail, unless there is already a question and answer that you are sure covers your scenario.
Or, consider whether delivering this app's UI via Web content is the correct solution in the first place.
Upvotes: 4