Open browser with Data URI Scheme instead of a URL


Question


Well, I was breaking the hell of my brain cells with this and no solution came up...

Usually, in Android, to open the Web Browser in a specified Website, we do this:

Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com"));
startActivity(browserIntent);

So, I got a Data URI Scheme (dunno if it is written this way, i'm not an expert on this kind of stuff) like this:

data:text/html;charset=utf8;base64,<base64 html code>

If I copy and paste this in a web browser, it will handle it the way I want it.

But how can I do it programatically in Android?

 Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(dataHTMLBase64));
 startActivity(browserIntent);

dataHTMLBase64 stores the Data URI Scheme I mentioned before.

The code above won't work. It won't even launch chrome.

What can I do?

PS: I'm not good with English. Please warn me if I didn't express myself the right way...


Answer 1:


Actually, it appears that one can launch a Data URI in an Android browser quite easily.

String url = "data:text/html;charset=utf8,<b>Hee-haw!</b>";

startActivity(Intent.makeMainSelectorActivity(
        Intent.ACTION_MAIN, Intent.CATEGORY_APP_BROWSER)
        .setData(Uri.parse(url.toString())));

Using apktool I reviewed the AndroidManifest.xml of the Google Chrome .apk file.
(apktool is quite easy to install, and then the command is simply apktool d example.apk)

I found the relevant intent filters (listed below) so there are many possible ways to launch the browser. Of course other browsers may have different intent filters, but it seems that APP_BROWSER is a good choice.

<activity-alias android:exported="true" android:name="com.google.android.apps.chrome.Main" android:targetActivity="org.chromium.chrome.browser.document.ChromeLauncherActivity">
    <intent-filter>
        <action android:name="android.intent.action.MAIN"/>
        <category android:name="android.intent.category.DEFAULT"/>
        <category android:name="android.intent.category.LAUNCHER"/>
        <category android:name="android.intent.category.BROWSABLE"/>
        <category android:name="android.intent.category.APP_BROWSER"/>
        <category android:name="android.intent.category.NOTIFICATION_PREFERENCES"/>
    </intent-filter>
    <intent-filter>
        <action android:name="android.intent.action.VIEW"/>
        <category android:name="android.intent.category.DEFAULT"/>
        <category android:name="android.intent.category.BROWSABLE"/>
        <data android:scheme="googlechrome"/>
        <data android:scheme="http"/>
        <data android:scheme="https"/>
        <data android:scheme="about"/>
        <data android:scheme="javascript"/>
    </intent-filter>
    <intent-filter>
        <action android:name="android.intent.action.VIEW"/>
        <category android:name="android.intent.category.DEFAULT"/>
        <category android:name="android.intent.category.BROWSABLE"/>
        <data android:scheme="googlechrome"/>
        <data android:scheme="http"/>
        <data android:scheme="https"/>
        <data android:scheme="about"/>
        <data android:scheme="content"/>
        <data android:scheme="javascript"/>
        <data android:mimeType="text/html"/>
        <data android:mimeType="text/plain"/>
        <data android:mimeType="application/xhtml+xml"/>
    </intent-filter>
    <intent-filter>
        <action android:name="android.intent.action.VIEW"/>
        <category android:name="android.intent.category.DEFAULT"/>
        <data android:mimeType="multipart/related" android:scheme="file"/>
    </intent-filter>
    <intent-filter>
        <action android:name="android.intent.action.MEDIA_SEARCH"/>
        <category android:name="android.intent.category.DEFAULT"/>
    </intent-filter>
    <intent-filter>
        <action android:name="android.speech.action.VOICE_SEARCH_RESULTS"/>
        <category android:name="android.intent.category.DEFAULT"/>
    </intent-filter>
    <intent-filter>
        <action android:name="android.nfc.action.NDEF_DISCOVERED"/>
        <category android:name="android.intent.category.DEFAULT"/>
        <data android:scheme="http"/>
        <data android:scheme="https"/>
    </intent-filter>
    <intent-filter>
        <action android:name="android.intent.action.SEARCH"/>
    </intent-filter>
    <intent-filter>
        <action android:name="com.sec.android.airview.HOVER"/>
    </intent-filter>
    <meta-data android:name="android.app.searchable" android:resource="@xml/searchable"/>
</activity-alias>



Answer 2:


If you get these data URIs from somewhere you could do two things:

  1. Parse the data content out of it and use it in some WebViewand call loadData(...) on it to display the contents
  2. Save the data content of the URIs to some file, use FileProvider to make this file accessible outside of your app and use the URI returned by that to fire up a browser / view intent


来源:https://stackoverflow.com/questions/35675807/open-browser-with-data-uri-scheme-instead-of-a-url


码神部落- 版权声明 1、本主题所有言论和图片纯属会员个人意见,与码神部落立场无关。
2、本站所有主题由该帖子作者发表,该帖子作者对牛谈情码神部落享有帖子相关版权。
3、码神部落管理员和版主有权不事先通知发贴者而删除本文。
4、其他单位或个人使用、转载或引用本文时必须同时征得该帖子作者对牛谈情码神部落的同意。
5、帖子作者须承担一切因本文发表而直接或间接导致的民事或刑事法律责任。
6、本帖部分内容转载自其它媒体,但并不代表本站赞同其观点和对其真实性负责。
7、如本帖侵犯到任何版权问题,请立即告知本站,本站将及时予与删除并致以最深的歉意。

最新回复 (0)
    • 码神部落
      2
        立即登录 立即注册 GitHub登录
返回
发新帖
作者最近主题: