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

Android Studio中WebView加载本地服务器网站无法上传图片求助

WebView无法上传图片的解决办法

你遇到的问题是WebView默认不会处理网页里的文件选择请求——手机浏览器自带这个处理逻辑,但WebView需要手动实现回调。下面是具体解决步骤:

1. 给WebView设置自定义WebChromeClient

修改你的WebView初始化代码,添加处理文件选择的核心逻辑:

// 先在Activity类里定义全局变量
private ValueCallback<Uri[]> mFilePathCallback;
private ValueCallback<Uri> mUploadMsg;
private static final int FILE_CHOOSER_REQUEST_CODE = 1000;

// 初始化WebView部分
mywebView=(WebView) findViewById(R.id.webview);
mywebView.setWebViewClient(new WebViewClient());

WebSettings webSettings=mywebView.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setAllowFileAccess(true);
webSettings.setAllowContentAccess(true);
webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
webSettings.setLoadsImagesAutomatically(true);

// 关键:添加WebChromeClient处理文件选择弹窗
mywebView.setWebChromeClient(new WebChromeClient() {
    // Android 5.0及以上版本处理逻辑
    @Override
    public boolean onShowFileChooser(WebView webView, ValueCallback<Uri[]> filePathCallback, FileChooserParams fileChooserParams) {
        mFilePathCallback = filePathCallback;
        Intent intent = fileChooserParams.createIntent();
        try {
            startActivityForResult(intent, FILE_CHOOSER_REQUEST_CODE);
        } catch (ActivityNotFoundException e) {
            mFilePathCallback = null;
            return false;
        }
        return true;
    }

    // 兼容Android 5.0以下版本(可选)
    public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType) {
        mUploadMsg = uploadMsg;
        Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
        intent.addCategory(Intent.CATEGORY_OPENABLE);
        intent.setType("image/*");
        startActivityForResult(Intent.createChooser(intent, "选择图片"), FILE_CHOOSER_REQUEST_CODE);
    }
});

mywebView.loadUrl("http://172.16.3.22/Server/index.html");

2. 处理文件选择后的返回结果

在Activity里重写onActivityResult方法,把选择的图片Uri返回给WebView:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
    super.onActivityResult(requestCode, resultCode, intent);
    if (requestCode == FILE_CHOOSER_REQUEST_CODE) {
        // 处理Android 5.0+的回调
        if (mFilePathCallback != null) {
            Uri[] results = null;
            if (resultCode == RESULT_OK && intent != null) {
                String dataString = intent.getDataString();
                if (dataString != null) {
                    results = new Uri[]{Uri.parse(dataString)};
                }
            }
            mFilePathCallback.onReceiveValue(results);
            mFilePathCallback = null;
        }
        // 处理Android 5.0以下的回调
        else if (mUploadMsg != null) {
            Uri result = (resultCode == RESULT_OK && intent != null) ? intent.getData() : null;
            mUploadMsg.onReceiveValue(result);
            mUploadMsg = null;
        }
    }
}

3. 动态申请权限(Android 6.0+)

虽然你已经在Manifest里添加了读写权限,但Android 6.0及以上需要在运行时主动申请:

// 在Activity的onCreate方法里添加权限检查
if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
    ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, 1);
}

补充注意

  • 若你的设备是Android 10及以上,遇到存储权限兼容问题时,可以在Manifest的<application>标签里添加android:requestLegacyExternalStorage="true"临时兼容旧存储逻辑
  • 你的网页上传逻辑没问题(手机浏览器可正常上传),无需修改网页代码

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.12 05:45:04