I want to load a local html file through chrome custom tab, is that workable?
Asked Answered
T

2

15

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

Tansy answered 22/10, 2015 at 2:53 Comment(5)
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?Shiver
@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.Tansy
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.Shiver
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..Tansy
WOW, really a good questionRetina
S
5

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

Shiver answered 22/10, 2015 at 9:39 Comment(4)
We can use Nanohttpd for creating a local server and serve the file inside the assets folder.Kraska
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. bitbucket.org/snippets/pkumarad/qAk6xKraska
Could you explain how I use this class @praveena_kd?Really
@ArmandoMarquesSobrinho From your Android Application class try and create an object of this class. for ex try like this: " server = new LocalServerImplementation(getApplicationContext()); "Kraska
C
8

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)
Commencement answered 5/2, 2020 at 14:45 Comment(1)
#44467787Liaotung
S
5

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

Shiver answered 22/10, 2015 at 9:39 Comment(4)
We can use Nanohttpd for creating a local server and serve the file inside the assets folder.Kraska
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. bitbucket.org/snippets/pkumarad/qAk6xKraska
Could you explain how I use this class @praveena_kd?Really
@ArmandoMarquesSobrinho From your Android Application class try and create an object of this class. for ex try like this: " server = new LocalServerImplementation(getApplicationContext()); "Kraska

© 2022 - 2024 — McMap. All rights reserved.