Currently I put my html file in assets, and I load it in WebView. Can I load it through chrome custom tab?
I want to load a local html file through chrome custom tab, is that workable?
Asked Answered
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 question –
Retina
No, it is not possible to open file:// URLs in customtabs.
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/qAk6x –
Kraska
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
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)
#44467787 –
Liaotung
No, it is not possible to open file:// URLs in customtabs.
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/qAk6x –
Kraska
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.