使用WebView调用在线网址--解决显示不出来(VSCode编写网页+Android Studio获取)

VSCode部分(测试样例):

代码如下:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <h3>使用WebView调用在线网址(VSCode编写网页+Android Studio获取)</h3>
</body>
</html>

Android Studio部分:

布局文件:(activity_main.xml) 

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <WebView
        android:id="@+id/show"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

 java文件:(Mainactivity)

 关键代码如下:

webView.loadUrl("http://(写自己本地ip):5500/基本使用.html");

若直接使用

http://127.0.0.1:5500/%E5%9F%BA%E6%9C%AC%E4%BD%BF%E7%94%A8.html

(该乱码是因为采用了中文直接复制,粘贴所得,不影响),会得到如下报错页面--WebView不显示。

 更改为自己的ip地址后便可正常显示,要注意需要AndroidManifest.xml配置相关权限:

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

 正常显示如下所示:

        以上便是对不能正确使用WebView显示在线网址的问题以及解决的方案,希望可以帮助到有需要的人,喜欢的可以点赞收藏噢~

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Android Studio使用 WebView 调用相机和相册,需要使用 Android 系统提供的 WebChromeClient 和 WebViewClient 类。具体步骤如下: 1. 首先在 AndroidManifest.xml 文件中添加以下权限: ```xml <uses-permission android:name="android.permission.CAMERA" /> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> ``` 2. 在 Activity 中创建 WebView,并设置 WebChromeClient 和 WebViewClient: ```java WebView webView = findViewById(R.id.webview); webView.setWebChromeClient(new WebChromeClient() { // 处理打开相机或相册的请求 @Override public boolean onShowFileChooser(WebView webView, ValueCallback<Uri[]> filePathCallback, FileChooserParams fileChooserParams) { // 创建打开相机或相册的 Intent Intent intent = fileChooserParams.createIntent(); try { // 启动 Intent startActivityForResult(intent, REQUEST_CODE_FILE_CHOOSER); } catch (ActivityNotFoundException e) { e.printStackTrace(); return false; } return true; } }); webView.setWebViewClient(new WebViewClient() { // 处理网页加载完成事件 @Override public void onPageFinished(WebView view, String url) { super.onPageFinished(view, url); // 在网页中注入 JavaScript 代码,用于调用相机或相册 view.loadUrl("javascript: function chooseImage() {window.android.chooseImage();}"); } }); ``` 3. 在 Activity 中重写 onActivityResult 方法,处理选择图片后的结果: ```java @Override protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) { if (requestCode == REQUEST_CODE_FILE_CHOOSER) { if (resultCode == RESULT_OK && data != null) { // 获取选择的图片 Uri Uri uri = data.getData(); if (uri != null) { // 把 Uri 返回给网页 ValueCallback<Uri[]> filePathCallback = mFilePathCallback; if (filePathCallback != null) { filePathCallback.onReceiveValue(new Uri[]{uri}); mFilePathCallback = null; } } } else { // 如果选择图片失败,也要返回结果给网页 ValueCallback<Uri[]> filePathCallback = mFilePathCallback; if (filePathCallback != null) { filePathCallback.onReceiveValue(null); mFilePathCallback = null; } } } } ``` 4. 在 JavaScript 中调用 window.android.chooseImage() 方法,用于打开相机或相册并选择图片: ```javascript function chooseImage() { // 创建 input 元素,用于触发选择图片的操作 var input = document.createElement('input'); input.type = 'file'; input.accept = 'image/*'; input.onchange = function () { // 把选择图片的结果返回给 Android var uri = input.files; if (uri) { window.android.chooseImage(uri); } else { window.android.chooseImage(null); } }; // 触发点击 input 元素的操作,打开相机或相册 input.click(); } ``` 以上就是在 Android Studio使用 WebView 调用相机和相册的基本步骤。如果您还有其他问题,请随时提出。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值