当前位置:   article > 正文

Android 使用webView加载html页面_android webview加载本地html

android webview加载本地html

1、首先在布局xml里面指定WebView根节点

  1. <WebView
  2. android:id="@+id/myWebView"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"/>

2、在.java的onCreate()里使用

  1. @Override
  2. protected void onCreate(@Nullable Bundle savedInstanceState) {
  3. super.onCreate(savedInstanceState);
  4. setContentView(R.layout.activity_three);
  5. //1. asset目录下的index.html文件
  6. String filePath = "file:///android_asset/html/index.html";
  7. //2.本地内存中的index.html文件
  8. // 获取文件夹路径
  9. String htmlPath = getExternalFilesDir("html").getPath();
  10. File htmlFile = new File(htmlPath);
  11. // 判断是否存在,不存在则创建
  12. if (htmlFile.exists()){
  13. htmlPath = htmlFile.getPath()+File.separator+"index.html";
  14. }else {
  15. htmlFile.mkdirs();
  16. htmlPath = htmlFile.getPath()+File.separator+"index.html";
  17. }
  18. // 地址
  19. String localFilePath = "file:///"+htmlPath;
  20. //3.指定的URL的html文件
  21. /**
  22. * 若是不显示,在AndroidManifest.xml中添加android:usesCleartextTraffic="true"
  23. * 并且设置网络权限
  24. */
  25. String urlPath = "https://www.baidu.com/";
  26. myWebView = findViewById(R.id.myWebView);
  27. WebSettings myWebSettings = myWebView.getSettings();
  28. // webView解决加载html页面空白问题
  29. myWebSettings.setJavaScriptEnabled(true);// 设置支持javascript
  30. myWebSettings.setUseWideViewPort(true);//将图片调整到适合webView大小
  31. myWebSettings.setLoadWithOverviewMode(true);//缩放至屏幕大小
  32. myWebSettings.setDomStorageEnabled(true);//设置DOM缓存,当H5网页使用localstorage时一定要设置
  33. myWebSettings.setCacheMode(android.webkit.WebSettings.LOAD_NO_CACHE);// 设置去缓存,防止加载的是上一次数据
  34. myWebSettings.setDatabaseEnabled(true);
  35. // 解决加载本地内存中报错 err_access_denied
  36. myWebSettings.setAllowFileAccess(true);
  37. myWebSettings.setAllowContentAccess(true);
  38. // 解决webView报错 Loading local files from file:// urls is not possible due browser security restrictions
  39. /**
  40. * 设置是否允许运行在一个file schema URL环境下的JavaScript访问来自其他任何来源的内容,
  41. * 包括其他file schema URLs。
  42. * 通过此API可以设置是否允许通过file url加载的Javascript可以访问其他的源,
  43. * 包括其他的文件和http,https等其他的源。与上面的类似,实现一个就可以。
  44. * webSetting.setAllowUniversalAccessFromFileURLs(true);
  45. * */
  46. myWebSettings.setAllowUniversalAccessFromFileURLs(true);
  47. /**
  48. * 设置是否允许运行在一个file schema URL环境下的JavaScript访问来自其他任何来源的内容,
  49. * 包括其他file schema URLs。
  50. * 通过此API可以设置是否允许通过file url加载的Javascript可以访问其他的源,
  51. * 包括其他的文件和http,https等其他的源。与上面的类似,实现一个就可以。
  52. */
  53. //myWebSettings.setAllowUniversalAccessFromFileURLs(true);
  54. //加载html
  55. if (filePath != null) {
  56. myWebView.loadUrl(urlPath);
  57. }
  58. }

3、创建assets目录(与res目录同一级别)

4、将要访问的*.html页面放置到assets目录即可 

5、使用X5内核 腾讯SDK 

地址:腾讯浏览服务

下载sdk:腾讯浏览服务-SDK下载

放置在libs文件夹,引用

AS高版本:

implementation(fileTree("libs"))

AS低版本:

  1. android{
  2. ...
  3. sourceSets {
  4. main {
  5. jniLibs.srcDirs = ['libs']
  6. }
  7. }
  8. }
  9. dependencies{
  10. ...
  11. compile files('libs/tbs_sdk_thirdapp_v4.3.0.386_44286_sharewithdownloadwithfile_withoutGame_obfs_20230210_114429.jar')
  12. }

AndroidManifest.xml配置权限

  1. <uses-permission android:name="android.permission.INTERNET" />
  2. <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
  3. <application
  4. android:name=".activity.app.MyAplication"
  5. ***
  6. /application>

Application.java设置初始化

  1. package com.example.yuanzhoulv.activity.app;;
  2. import android.app.Application;
  3. import com.tencent.smtt.sdk.QbSdk;
  4. public class MyAplication extends Application {
  5. @Override
  6. public void onCreate() {
  7. // TODO Auto-generated method stub
  8. super.onCreate();
  9. //搜集本地tbs内核信息并上报服务器,服务器返回结果决定使用哪个内核。
  10. QbSdk.PreInitCallback cb = new QbSdk.PreInitCallback() {
  11. @Override
  12. public void onViewInitFinished(boolean arg0) {
  13. // TODO Auto-generated method stub
  14. //x5內核初始化完成的回调,为true表示x5内核加载成功,否则表示x5内核加载失败,会自动切换到系统内核。
  15. }
  16. @Override
  17. public void onCoreInitFinished() {
  18. // TODO Auto-generated method stub
  19. }
  20. };
  21. //x5内核初始化接口
  22. QbSdk.initX5Environment(getApplicationContext(), cb);
  23. }
  24. }

使用:

*.xml

  1. <com.tencent.smtt.sdk.WebView
  2. android:id="@+id/webView"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"/>

*.java

  1. //1. asset目录下的index.html文件
  2. String filePath = "file:///android_asset/html/index.html";
  3. //2.本地内存中的index.html文件
  4. // 获取文件夹路径
  5. String htmlPath = getExternalFilesDir("html").getPath();
  6. File htmlFile = new File(htmlPath);
  7. // 判断是否存在,不存在则创建
  8. if (htmlFile.exists()){
  9. htmlPath = htmlFile.getPath()+File.separator+"index.html";
  10. }else {
  11. htmlFile.mkdirs();
  12. htmlPath = htmlFile.getPath()+File.separator+"index.html";
  13. }
  14. // 地址
  15. String localFilePath = "file:///"+htmlPath;
  16. //3.指定的URL的html文件
  17. /**
  18. * 若是不显示,在AndroidManifest.xml中添加android:usesCleartextTraffic="true"
  19. * 并且设置网络权限
  20. */
  21. String urlPath = "https://www.baidu.com/";
  22. webView = findViewById(R.id.webView);
  23. com.tencent.smtt.sdk.WebSettings webSettings = webView.getSettings();
  24. webSettings.setJavaScriptEnabled(true);// 设置支持javascript
  25. webSettings.setUseWideViewPort(true);//将图片调整到适合webView大小
  26. webSettings.setLoadWithOverviewMode(true);//缩放至屏幕大小
  27. webSettings.setDomStorageEnabled(true);//设置DOM缓存,当H5网页使用localstorage时一定要设置
  28. webSettings.setCacheMode(android.webkit.WebSettings.LOAD_NO_CACHE);// 设置去缓存,防止加载的是上一次数据
  29. webSettings.setDatabaseEnabled(true);
  30. // 解决加载本地内存中报错 err_access_denied
  31. webSettings.setAllowFileAccess(true);
  32. webSettings.setAllowContentAccess(true);
  33. webSettings.setAllowUniversalAccessFromFileURLs(true);
  34. //加载html
  35. if (filePath != null) {
  36. webView.loadUrl(localFilePath);
  37. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/酷酷是懒虫/article/detail/760519
推荐阅读
相关标签
  

闽ICP备14008679号