14

目前我将我的 html 文件放在资产中,并将其加载到 WebView 中。我可以通过 chrome 自定义选项卡加载它吗?

4

2 回答 2

6

其实是有办法的。在 AndroidManifest.xml 中

    <provider
        android:name="androidx.core.content.FileProvider"
        android:authorities="${applicationId}.provider"
        android:exported="false"
        android:grantUriPermissions="true">
        <meta-data
            android:name="android.support.FILE_PROVIDER_PATHS"
            android:resource="@xml/provider_paths" />
    </provider>

定义提供者路径

<?xml version="1.0" encoding="utf-8"?>
<paths>
<external-path
    name="external_files"
    path="." />
</paths>

然后只需将本地文件提取到外部目录

val file = File(activity.externalCacheDir, "hello.html")
        val bytes = resources.openRawResource(R.raw.hello).use { it.readBytes() }
        FileOutputStream(file).use { it.write(bytes) }
        val uri = FileProvider.getUriForFile(activity, "${activity.packageName}.provider", file)
        CustomTabsIntent.Builder()
            .build()
            .also { it.intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION) }
            .launchUrl(activity, uri)
于 2020-02-05T14:45:56.943 回答
5

不,无法在 customtabs 中打开 file:// URL。

于 2015-10-22T09:39:48.560 回答