You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android Studio WebView无法显示MJPG IP摄像头真机白屏问题求助

Fixing Blank WebView on Real Devices for IP Camera Feeds

Hey there, let's work through this issue together—since your WebView works on the emulator and with public sites like Google, the problem is almost certainly tied to real-device-specific settings or network constraints. Here are the most likely fixes to try:

1. Check Internet Permissions & Cleartext Traffic (Critical for Android 9+)

First, make sure your app has the necessary network access, and that it's allowed to load non-HTTPS (cleartext) content if your IP camera uses HTTP:

  • Add the internet permission to your AndroidManifest.xml:
    <uses-permission android:name="android.permission.INTERNET" />
    
  • If your camera uses HTTP (not HTTPS), create a network_security_config.xml file in res/xml/ with:
    <?xml version="1.0" encoding="utf-8"?>
    <network-security-config>
        <domain-config cleartextTrafficPermitted="true">
            <domain includeSubdomains="true">YOUR_CAMERA_IP_OR_DOMAIN</domain>
        </domain-config>
    </network-security-config>
    
  • Reference this config in your AndroidManifest.xml's application tag:
    <application
        ...
        android:networkSecurityConfig="@xml/network_security_config">
    

This fixes the most common issue where Android blocks unencrypted traffic by default on real devices.

2. Verify Your Real Device Can Reach the IP Camera

Emulators share your computer's network, but your phone might be on a different Wi-Fi network or using mobile data. Do these checks:

  • Connect your phone to the same Wi-Fi network as the IP camera (most cameras are only accessible locally).
  • Open your phone's browser and navigate directly to the camera's URL. If it doesn't load here, the problem is network-related (e.g., wrong IP, camera firewall, or network isolation).
  • If the browser loads it but WebView doesn't, move to the next steps.

3. Configure WebView Settings for Camera Feeds

IP camera web interfaces often rely on JavaScript, DOM storage, or mixed content. Update your WebView setup:

WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true); // Camera feeds often need JS
webSettings.setDomStorageEnabled(true); // For storing session data
webSettings.setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW); // If camera uses mixed HTTP/HTTPS content

Note: Only use MIXED_CONTENT_ALWAYS_ALLOW if you trust the camera's network—preferably, configure your camera to use HTTPS for security.

4. Debug WebView Errors with Callbacks

Add error listeners to your WebView to see exactly what's failing:

webView.setWebViewClient(new WebViewClient() {
    @Override
    public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
        super.onReceivedError(view, errorCode, description, failingUrl);
        Log.e("CameraWebView", "Error loading feed: " + description + " (Code: " + errorCode + ")");
    }

    @Override
    public void onReceivedHttpError(WebView view, WebResourceRequest request, WebResourceResponse errorResponse) {
        super.onReceivedHttpError(view, request, errorResponse);
        Log.e("CameraWebView", "HTTP Error: " + errorResponse.getStatusCode());
    }
});

Check Logcat for these error messages—they'll tell you if it's a network error, HTTP 404, or something else.

5. Check for CORS or Camera Server Restrictions

Some IP cameras block requests from non-browser user agents. You can override the WebView's user agent to match your phone's browser:

webSettings.setUserAgentString("Mozilla/5.0 (Linux; Android 13; SM-G998B) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Mobile Safari/537.36");

Replace the string with your phone's actual user agent (find it in your browser's "About" section or via an online tool).


内容的提问来源于stack exchange,提问作者emir

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 09:19:09