使用数据URI方案而不是URL打开浏览器

时间:2016-02-27 21:43:47

标签: android android-intent browser uri url-scheme

好吧,我用这个破坏了脑细胞的地狱,没有解决方案......

通常,在Android中,要在指定的网站中打开Web浏览器,我们会这样做:

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

所以,我得到了一个数据URI方案(dunno,如果是这样写的,我不是这类东西的专家),就像这样:

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

如果我将其复制并粘贴到网络浏览器中,它将按照我想要的方式处理它。

但是如何在Android中以编程方式执行此操作?

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

dataHTMLBase64存储我之前提到的数据URI方案。

上面的代码不起作用。它甚至不会推出镀铬。

我该怎么办?

PS:我对英语不太好。如果我没有以正确的方式表达自己,请警告我......

2 个答案:

答案 0 :(得分:1)

实际上,似乎可以很容易地在Android浏览器中启动数据URI。

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())));

使用apktool我查看了Google Chrome AndroidManifest.xml文件的.apk (apktool很容易安装,然后命令只是apktool d example.apk

我找到了相关的意图过滤器(如下所列),因此有很多种方法可以启动浏览器。当然,其他浏览器可能有不同的意图过滤器,但似乎APP_BROWSER是一个不错的选择。

<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>

答案 1 :(得分:0)

如果你从某个地方获得这些数据URI,你可以做两件事:

  1. 从中解析数据内容并在某些WebView中使用它并在其上调用loadData(...)以显示内容
  2. 将URI的数据内容保存到某个文件,使用FileProvider使该文件可以在您的应用程序外部访问,并使用其返回的URI启动浏览器/视图意图