这个问题是关于当从 Google Play 加载应用程序时,将资源文件夹中的 HTML 内容加载到 Android 动态功能模块内的 WebView 组件中。你怎么能这么做呢?现在,当我的代码使用 WebView.loadUrl
时,我收到“文件未找到”错误。
请注意,这个问题不是关于如何将 HTML 内容加载到主 Android“应用程序”模块内的 WebView 中。它也不是关于如何让它在模拟器上本地运行 - 当在动态功能模块中加载到模拟器上时,加载 HTML 内容可以正常工作。
我的项目具有以下结构:
Top
- app (parent module)
| - src
| - ...
- module 1 (child module)
| - src
| - main
| - assets (html is under this folder)
| - java
| - org.example.fragment
| - WebFragment
所以模块 1 是一个 Android 功能模块,带有一个 Fragment
,它加载一个 android.webkit.WebView
来加载资源。但是,当从 Google Play 加载应用程序时,无法加载 HTML 内容。
下面是模块 1 中 Fragment 的代码,它应该加载 HTML:
public class WebFragment extends Fragment {
protected static final String DEFAULT_LOCATION = "file:///android_asset/";
protected WebView gameView;
private static final String TAG = "MindGymWebFragment";
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
SplitCompat.install(getContext());
View v = inflater.inflate(R.layout.fragment_web_mindgym, container, false);
try {
String gameToOpen = this.getArguments().getString("gameToOpen");
gameView = v.findViewById(R.id.memory_game_view);
setupSettings();
boolean isMemory = "Memory".equals(gameToOpen);
boolean isDestress = "DeStress".equals(gameToOpen);
if (isMemory) {
gameView.loadUrl(DEFAULT_LOCATION + "memory/memory.html");
} else if (isDestress) {
gameView.loadUrl(DEFAULT_LOCATION + "breathe/breathing.html");
}
MainActivity mainActivity = (MainActivity) getActivity();
mainActivity.unselectHomeTab();
if (isMemory || isDestress) {
mainActivity.setDailyOnABar(getString(org.brahmakumaris.beezone.R.string.mind_gym));
}
} catch (Exception e) {
Log.e(TAG, "Cannot initializa game", e);
}
return v;
}
protected void setupSettings() {
WebSettings settings = gameView.getSettings();
settings.setLoadWithOverviewMode(true);
settings.setUseWideViewPort(true);
settings.setAllowFileAccess(true);
settings.setAllowContentAccess(true);
settings.setAllowFileAccessFromFileURLs(true);
settings.setAllowUniversalAccessFromFileURLs(true);
settings.setBlockNetworkImage(false);
settings.setBlockNetworkLoads(false);
settings.setLoadsImagesAutomatically(true);
settings.setDomStorageEnabled(true);
settings.setLoadWithOverviewMode(true);
settings.setJavaScriptEnabled(true);
settings.setMediaPlaybackRequiresUserGesture(false);
}
@Override
public void onDestroy() {
gameView.destroy();
gameView = null;
super.onDestroy();
}
}
那么:如何从 Google Play 提供的动态功能模块加载 HTML?
更新 1:如果将 html 资源复制到 app/src/main/assets
文件夹,上面的代码将起作用 - 但这显然不是我们想要的。当模块从 Google Play 下载时,模块 1 的 webview 应该能够访问模块 1 的资源文件夹。
最佳答案
经过大量研究后,我在互联网上发现了这个:
https://chromium.googlesource.com/chromium/src.git/+/master/docs/android_dynamic_feature_modules.md
Limitations DFMs have the following limitations:
WebView: We don't support DFMs for WebView. If your feature is used by WebView you cannot put it into a DFM.
因此您不能在动态功能模块中使用 WebView。
关于android - 从 Android 动态功能模块将资源加载到 WebView 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58338921/