【问题标题】:Open browser with Data URI Scheme instead of a URL使用数据 URI 方案而不是 URL 打开浏览器
【发布时间】:2016-02-27 21:43:47
【问题描述】:

好吧,我正在用这个破坏我的脑细胞,但没有找到解决方案......

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

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

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

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

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

但是我如何在 Android 中以编程方式进行呢?

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

dataHTMLBase64 存储了我之前提到的 Data URI Scheme。

上面的代码不起作用。它甚至不会启动 chrome。

我能做什么?

PS:我英语不好。如果我没有正确表达自己,请警告我...

【问题讨论】:

  • 如果浏览器普遍支持Uri 方案,对于传入的Intents,我会感到惊讶。当他们从他们加载的网页中遇到类似的 URL 时,他们会在内部处理它。

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


【解决方案1】:

实际上,似乎可以很容易地在 Android 浏览器中启动 Data 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 .apk 文件的 AndroidManifest.xml
(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>

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-09-09
      • 2020-12-20
      • 2014-05-22
      • 1970-01-01
      • 1970-01-01
      • 2020-05-27
      • 1970-01-01
      相关资源
      最近更新 更多