Webview中的相机支持,用于HTML输入标记

时间:2017-08-20 17:08:27

标签: android html webview

我正在尝试使用webview构建一个简单的应用程序,该应用程序使用 打开我的html页面。使用手机上的浏览器加载时,html页面会打开相机,但不会在我的网页浏览中打开相机。我试图寻找答案,但我不确定实际问题。 有人可以帮我理解什么是错的。我正在粘贴下面的代码和logcat输出。

Mainactivity:

    package com.webview.ak.webviewapp;

import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.content.Context;
import android.os.Build;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.webkit.JavascriptInterface;
import android.webkit.PermissionRequest;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    final String TAG = "MainActivity";
    //final String LOCAL_FILE ="http://10.0.2.2/Beacon/newcam.php";
    final String LOCAL_FILE ="http://192.168.1.35/Beacon/newcam.php";

    @SuppressLint("JavascriptInterface")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        // Find the web view in our layout xml
        WebView myWebView = (WebView) findViewById(R.id.webview);


        // Settings
        WebSettings webSettings = myWebView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        webSettings.setDomStorageEnabled(true);
        webSettings.setAllowFileAccessFromFileURLs(true);
        webSettings.setAllowUniversalAccessFromFileURLs(true);
        webSettings.setAllowContentAccess(true);
        webSettings.setDisplayZoomControls(true);

        // Set a web view client and a chrome client
        myWebView.setWebViewClient(new WebViewClient());
        myWebView.setWebChromeClient(new WebChromeClient() {
            // Need to accept permissions to use the camera and audio
            @TargetApi(Build.VERSION_CODES.M)

            @Override
            public void onPermissionRequest(final PermissionRequest request) {
                Log.d(TAG, "onPermissionRequest");

                        // Make sure the request is coming from our file
                        // Warning: This check may fail for local files
                            request.grant(request.getResources());



            }
        });



        // Load the local HTML file into the webview
        myWebView.loadUrl(LOCAL_FILE);
    }



}

xml文件:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">


    <WebView
        android:id="@+id/webview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        />
</RelativeLayout>

清单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.webview.ak.webviewapp">
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.RECORD_AUDIO" />
    <uses-permission android:name="android.permission.CAMERA" />
    <uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />

    <uses-feature android:name="android.hardware.camera" android:required="true" />
    <uses-feature android:name="android.hardware.camera.front" android:required="true" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

HTML文件:

<html>
<head>

</head>

<body>
<input type="file" accept="image/*">

</body>
</html>

Logcat输出:

08-20 22:16:06.590 16088-16088/? I/art: Late-enabling -Xcheck:jni
08-20 22:16:06.599 16088-16088/? D/TidaProvider: TidaProvider()
08-20 22:16:06.606 16088-16088/? V/BoostFramework: mAcquireFunc method = public int com.qualcomm.qti.Performance.perfLockAcquire(int,int[])
08-20 22:16:06.606 16088-16088/? V/BoostFramework: mReleaseFunc method = public int com.qualcomm.qti.Performance.perfLockRelease()
08-20 22:16:06.606 16088-16088/? V/BoostFramework: mAcquireTouchFunc method = public int com.qualcomm.qti.Performance.perfLockAcquireTouch(android.view.MotionEvent,android.util.DisplayMetrics,int,int[])
08-20 22:16:06.607 16088-16088/? V/BoostFramework: mIOPStart method = public int com.qualcomm.qti.Performance.perfIOPrefetchStart(int,java.lang.String)
08-20 22:16:06.607 16088-16088/? V/BoostFramework: mIOPStop method = public int com.qualcomm.qti.Performance.perfIOPrefetchStop()
08-20 22:16:06.619 16088-16088/? V/BoostFramework: BoostFramework() : mPerf = com.qualcomm.qti.Performance@e7557a
08-20 22:16:06.653 16088-16088/com.webview.ak.webviewapp W/System: ClassLoader referenced unknown path: /data/app/com.webview.ak.webviewapp-2/lib/arm64
08-20 22:16:06.690 16088-16088/com.webview.ak.webviewapp W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
08-20 22:16:06.721 16088-16088/com.webview.ak.webviewapp V/BoostFramework: BoostFramework() : mPerf = com.qualcomm.qti.Performance@1a0cdff
08-20 22:16:06.722 16088-16088/com.webview.ak.webviewapp V/BoostFramework: BoostFramework() : mPerf = com.qualcomm.qti.Performance@27597cc
08-20 22:16:06.793 16088-16088/com.webview.ak.webviewapp I/WebViewFactory: Loading com.android.chrome version 59.0.3071.125 (code 307112552)
08-20 22:16:06.883 16088-16088/com.webview.ak.webviewapp I/cr_LibraryLoader: Time to load native libraries: 5 ms (timestamps 1900-1905)
08-20 22:16:06.898 16088-16088/com.webview.ak.webviewapp I/chromium: [INFO:library_loader_hooks.cc(144)] Chromium logging enabled: level = 0, default verbosity = 0
08-20 22:16:06.898 16088-16088/com.webview.ak.webviewapp I/cr_LibraryLoader: Expected native library version number "59.0.3071.125", actual native library version number "59.0.3071.125"
08-20 22:16:06.914 16088-16088/com.webview.ak.webviewapp I/cr_BrowserStartup: Initializing chromium process, singleProcess=true
08-20 22:16:06.933 16088-16088/com.webview.ak.webviewapp I/Adreno: QUALCOMM build                   : 01d2d27, I3d52eaf367
                                                                   Build Date                       : 12/10/16
                                                                   OpenGL ES Shader Compiler Version: XE031.09.00.03
                                                                   Local Branch                     : 
                                                                   Remote Branch                    : 
                                                                   Remote Branch                    : 
                                                                   Reconstruct Branch               : 
08-20 22:16:07.011 16088-16203/com.webview.ak.webviewapp W/cr_BindingManager: Cannot setInForeground() - never saw a connection for the pid: 16088
08-20 22:16:07.028 16088-16088/com.webview.ak.webviewapp D/EgretLoader: EgretLoader(Context context)
08-20 22:16:07.030 16088-16088/com.webview.ak.webviewapp D/EgretLoader: The context is not activity
08-20 22:16:07.039 16088-16207/com.webview.ak.webviewapp W/cr_media: Requires BLUETOOTH permission
08-20 22:16:07.076 16088-16088/com.webview.ak.webviewapp E/HAL: PATH3 /odm/lib64/hw/gralloc.qcom.so
08-20 22:16:07.076 16088-16088/com.webview.ak.webviewapp E/HAL: PATH2 /vendor/lib64/hw/gralloc.qcom.so
08-20 22:16:07.076 16088-16088/com.webview.ak.webviewapp E/HAL: PATH1 /system/lib64/hw/gralloc.qcom.so
08-20 22:16:07.076 16088-16088/com.webview.ak.webviewapp E/HAL: PATH3 /odm/lib64/hw/gralloc.msm8953.so
08-20 22:16:07.076 16088-16088/com.webview.ak.webviewapp E/HAL: PATH2 /vendor/lib64/hw/gralloc.msm8953.so
08-20 22:16:07.076 16088-16088/com.webview.ak.webviewapp E/HAL: PATH1 /system/lib64/hw/gralloc.msm8953.so
08-20 22:16:07.076 16088-16223/com.webview.ak.webviewapp E/libEGL: validate_display:99 error 3008 (EGL_BAD_DISPLAY)
08-20 22:16:07.092 16088-16088/com.webview.ak.webviewapp D/ActivityThreadInjector: clearCachedDrawables.
08-20 22:16:07.123 16088-16223/com.webview.ak.webviewapp W/VideoCapabilities: Unrecognized profile 2130706433 for video/avc
08-20 22:16:07.123 16088-16223/com.webview.ak.webviewapp W/VideoCapabilities: Unrecognized profile 2130706434 for video/avc
08-20 22:16:07.128 16088-16223/com.webview.ak.webviewapp W/Utils: could not parse long range '175-174'
08-20 22:16:07.131 16088-16223/com.webview.ak.webviewapp W/VideoCapabilities: Unrecognized profile 2130706433 for video/avc
08-20 22:16:07.131 16088-16223/com.webview.ak.webviewapp W/VideoCapabilities: Unrecognized profile 2130706434 for video/avc
08-20 22:16:07.132 16088-16223/com.webview.ak.webviewapp W/VideoCapabilities: Unrecognized profile 2130706433 for video/avc
08-20 22:16:07.132 16088-16223/com.webview.ak.webviewapp W/VideoCapabilities: Unrecognized profile 2130706434 for video/avc
08-20 22:16:07.134 16088-16223/com.webview.ak.webviewapp W/VideoCapabilities: Unrecognized profile/level 0/3 for video/mpeg2
08-20 22:16:07.136 16088-16223/com.webview.ak.webviewapp W/VideoCapabilities: Unrecognized profile/level 0/3 for video/mpeg2
08-20 22:16:07.141 16088-16221/com.webview.ak.webviewapp I/OpenGLRenderer: Initialized EGL, version 1.4
08-20 22:16:07.141 16088-16221/com.webview.ak.webviewapp D/OpenGLRenderer: Swap behavior 1
08-20 22:16:07.151 16088-16223/com.webview.ak.webviewapp W/VideoCapabilities: Unsupported mime video/mp4v-esdp
08-20 22:16:07.164 16088-16205/com.webview.ak.webviewapp D/NetworkSecurityConfig: No Network Security Config specified, using platform default
08-20 22:16:07.171 16088-16223/com.webview.ak.webviewapp I/VideoCapabilities: Unsupported profile 4 for video/mp4v-es
08-20 22:16:07.377 16088-16088/com.webview.ak.webviewapp W/cr_BindingManager: Cannot call determinedVisibility() - never saw a connection for the pid: 16088
08-20 22:16:09.777 16088-16088/com.webview.ak.webviewapp V/BoostFramework: BoostFramework() : mPerf = com.qualcomm.qti.Performance@beb206
08-20 22:17:19.259 16088-16203/com.webview.ak.webviewapp W/cr_BindingManager: Cannot setInForeground() - never saw a connection for the pid: 16088

1 个答案:

答案 0 :(得分:0)

您可以使用Chrome自定义标签 阅读它here.

  

什么是Chrome自定义标签?

     

当用户点击URL启动浏览器或使用WebViews构建他们自己的应用程序内浏览器时,应用程序开发人员可以选择。

     

这两个选项都带来了挑战 - 启动浏览器是一个繁重的上下文切换,无法自定义,而WebView不会与浏览器共享状态并增加维护开销。

     

Chrome自定义标签可让应用程序更好地控制其网络体验,并使本机和网络内容之间的转换更加无缝,而无需借助WebView。

     

Chrome自定义标签允许应用自定义Chrome的外观和感觉。应用可以更改以下内容:

     

工具栏颜色

     

输入和退出动画

     

将自定义操作添加到Chrome工具栏,溢出菜单和底部工具栏

从支持中添加:

dependencies { ... compile 'com.android.support:customtabs:23.3.0' }

示例:

String url = ¨https://google.com/¨; CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder(); CustomTabsIntent customTabsIntent = builder.build(); customTabsIntent.launchUrl(this, Uri.parse(url));

<强>更新 您也可以通过启用以下设置在webview上转换html5:

 wv = (WebView) findViewById(R.id.webview);
 WebSettings ws = wv.getSettings();
 ws.setJavaScriptEnabled(true);
 ws.setAllowFileAccess(true); 
if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.ECLAIR) { 
try { 
Log.d(TAG, "Enabling HTML5-Features"); 
Method m1 = WebSettings.class.getMethod("setDomStorageEnabled", new Class[]{Boolean.TYPE});
  m1.invoke(ws, Boolean.TRUE); Method m2 = WebSettings.class.getMethod("setDatabaseEnabled", new Class[]{Boolean.TYPE}); 
m2.invoke(ws, Boolean.TRUE); 
Method m3 = WebSettings.class.getMethod("setDatabasePath", new Class[]{String.class}); 
m3.invoke(ws, "/data/data/" + getPackageName() + "/databases/");
  Method m4 = WebSettings.class.getMethod("setAppCacheMaxSize", new Class[]{Long.TYPE}); 
m4.invoke(ws, 1024*1024*8); 
Method m5 = WebSettings.class.getMethod("setAppCachePath", new Class[]{String.class}); 
m5.invoke(ws, "/data/data/" + getPackageName() + "/cache/"); 
Method m6 = WebSettings.class.getMethod("setAppCacheEnabled", new Class[]{Boolean.TYPE}); 
 m6.invoke(ws, Boolean.TRUE); Log.d(TAG, "Enabled HTML5-Features"); 
} catch (NoSuchMethodException e) {
  Log.e(TAG, "Reflection fail", e); 
} catch (InvocationTargetException e) {
 Log.e(TAG, "Reflection fail", e);
 } catch (IllegalAccessException e) {
 Log.e(TAG, "Reflection fail", e); 
}
 }