15

Currently I put my html file in assets, and I load it in WebView. Can I load it through chrome custom tab?

Wang Tavx
  • 183
  • 1
  • 9
  • Do you want to do it because you like the UI or you rather want to make sure the files are parsed securely by a separate chrome renderer process? – Egor Pasko Oct 22 '15 at 09:40
  • @EgorPasko No, my page will load a lot of js file, in order to reduce the cost of network resource and the loading time, we put the html file and js file in assets. Then it only need to make a few requests before rendering. While it really slow comparing with the same way in iOS. – Wang Tavx Oct 23 '15 at 02:21
  • This is a good usecase for WebView. In CustomTabs you won't have any access to the web contents area anyway, for security/privacy reasons, and I guess that's what you wanted. – Egor Pasko Oct 26 '15 at 16:54
  • Of course, but the WebView is slow comparing with iOS...Even Nexus 6 can not run as fast as iPhone 4s when loading a page with a lot of js file.. – Wang Tavx Nov 23 '15 at 08:54
  • WOW, really a good question – JasmineOT Jan 06 '16 at 06:16

2 Answers2

7

Actually there is a way. in 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>

define provider paths

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

And then just extract your local file to external dir

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)
Skotos
  • 99
  • 1
  • 1
5

No, it is not possible to open file:// URLs in customtabs.

Egor Pasko
  • 506
  • 3
  • 11
  • 1
    We can use Nanohttpd for creating a local server and serve the file inside the assets folder. – praveena_kd Mar 20 '17 at 08:33
  • 3
    Here is a Local Server implementation using Nano httpd. This will serve the files from assets folder and we can use this to render our offline pages in custom chrome tabs. https://bitbucket.org/snippets/pkumarad/qAk6x – praveena_kd Apr 03 '17 at 06:54
  • Could you explain how I use this class @praveena_kd? – Armando Marques da S Sobrinho Jul 08 '19 at 18:49
  • @ArmandoMarquesSobrinho From your Android Application class try and create an object of this class. for ex try like this: " server = new LocalServerImplementation(getApplicationContext()); " – praveena_kd Jul 09 '19 at 14:39