Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 380
Wang Tavx Avatar asked Oct 22 '15 02:10

Wang Tavx


People also ask

How do I add local explorer to Chrome?

Adding Local Explorer to Chrome is a two-part process. You need the add-on in Chrome, and you need an integration module to run the default programs for the files. Open the Local Explorer extension page in the Chrome Web Store, click Add to Chrome in the upper-right corner. In the popup window, select Add extension.

How do I view HTML files on a local server?

How to start a local web server to view HTML files. open up the terminal or command prompt. navigate to the directory where the files live. run a local python server by running: python3 -m http.server 1234. Go to http://localhost:1234/ in your browser and navigate to or type into the address bar the ...

How do I open a file in chrome from a terminal?

Open your Terminal and paste in the file path to the Chrome executable you just copied If it's not recognizing the path, the spaces in the file path might need to be escaped with a forward-slash like Google\ Chrome.app Add the following flag to the end of your command: --allow-file-access-from-files

Can I browse folders and files on my local device?

Everyone knows that you can use Google Chrome to browse websites. But like any browser, you can also use it to browse folders and files on your local device, just like Windows Explorer in Windows and Finder in macOS.


2 Answers

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

like image 56
Egor Pasko Avatar answered Sep 21 '22 11:09

Egor Pasko


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)
like image 29
Skotos Avatar answered Sep 19 '22 11:09

Skotos