我们在使用原生的webView的时候经常会加载一些本地的网页,那么这篇短小的文章就来介绍一下GeckoView加载本地网页资源的情况:
1、assets文件夹下资源
原生webview:
webView.loadUrl("file:///android_asset/web/index.html");
GeckoView:
GeckoView view = findViewById(R.id.geckoview);
GeckoSession session = new GeckoSession();
GeckoRuntime runtime = GeckoRuntime.create(this);
session.open(runtime);
GeckoSessionSettings settings = session.getSettings();
settings.setAllowJavascript(true);
view.setSession(session);
session.loadUri("resource://android/assets/html/bg.html");
2、SD卡中的资源
原生webview
webView.loadUrl("file:///storage/emulated/0/Android/data/com.example.geckotest/files/Download/bg.html");
GeckoView:
session.loadUri("file:///storage/emulated/0/Android/data/com.example.geckotest/files/Download/bg.html");
总结:加载内存卡中的资源我们都是用"file://",但是我们加载apk文件中的assets中资源的时候,ckoView提供了一个resource://android/指向APK根目录的简写形式,那么我们要访问assets就使用"resource://android/assets/"