安卓WebView中的“上传按钮无法工作”
创始人
2024-11-09 14:00:22
0

要解决安卓WebView中上传按钮无法工作的问题,可以尝试以下方法:

  1. 添加文件选择器: 在你的WebViewClient中,重写onShowFileChooser()方法,并返回一个文件选择器。示例代码如下:
webView.setWebViewClient(new WebViewClient() {
    @Override
    public boolean onShowFileChooser(WebView webView, ValueCallback filePathCallback, WebChromeClient.FileChooserParams fileChooserParams) {
        // 创建一个选择文件的Intent
        Intent intent = fileChooserParams.createIntent();
        try {
            // 启动文件选择器
            startActivityForResult(intent, FILE_CHOOSER_RESULT_CODE);
        } catch (ActivityNotFoundException e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }
});
  1. 处理文件选择结果: 在你的Activity或Fragment中,重写onActivityResult()方法,并处理文件选择结果。示例代码如下:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == FILE_CHOOSER_RESULT_CODE) {
        if (uploadMessage != null) {
            Uri[] results = null;
            if (resultCode == Activity.RESULT_OK) {
                if (data != null) {
                    // 获取选择的文件Uri
                    results = new Uri[] { data.getData() };
                }
            }
            // 将选择的文件Uri返回给WebView
            uploadMessage.onReceiveValue(results);
            uploadMessage = null;
        }
    }
}
  1. 设置WebChromeClient: 在创建WebView时,为其设置WebChromeClient,并重写onShowFileChooser()方法和onActivityResult()方法。示例代码如下:
webView.setWebChromeClient(new WebChromeClient() {
    // For Android 3.0+
    public void openFileChooser(ValueCallback uploadMsg) {
        mUploadMessage = uploadMsg;
        // 创建一个选择文件的Intent
        Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
        intent.addCategory(Intent.CATEGORY_OPENABLE);
        intent.setType("*/*");
        // 启动文件选择器
        startActivityForResult(Intent.createChooser(intent, "File Chooser"), FILE_CHOOSER_RESULT_CODE);
    }

    // For Android 3.0+
    public void openFileChooser(ValueCallback uploadMsg, String acceptType) {
        mUploadMessage = uploadMsg;
        // 创建一个选择文件的Intent
        Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
        intent.addCategory(Intent.CATEGORY_OPENABLE);
        intent.setType(acceptType);
        // 启动文件选择器
        startActivityForResult(Intent.createChooser(intent, "File Chooser"), FILE_CHOOSER_RESULT_CODE);
    }

    // For Android < 3.0
    public void openFileChooser(ValueCallback uploadMsg, String acceptType, String capture) {
        mUploadMessage = uploadMsg;
        // 创建一个选择文件的Intent
        Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
        intent.addCategory(Intent.CATEGORY_OPENABLE);
        intent.setType(acceptType);
        // 启动文件选择器
        startActivityForResult(Intent.createChooser(intent, "File Chooser"), FILE_CHOOSER_RESULT_CODE);
    }

    // For Android 5.0+
    @Override
    public boolean onShowFileChooser(WebView webView, ValueCallback filePathCallback, FileChooserParams fileChooserParams) {
        if (mUploadMessageArray != null) {
            mUploadMessageArray.onReceiveValue(null);
            mUploadMessageArray = null;
        }

        mUploadMessageArray = filePathCallback;
        // 创建一个选择文件的Intent
        Intent intent = fileChooserParams.createIntent();
        try {
            startActivityForResult(intent, FILE_CHOOSER_RESULT_CODE);
        } catch (ActivityNotFoundException e) {
            e.printStackTrace();
            mUploadMessageArray = null;
            return false;
        }
        return true;
    }
});

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == FILE_CHOOSER_RESULT_CODE) {
        if (mUploadMessage != null) {
            // For Android 3.0+
            Uri result = data == null || resultCode != RESULT_OK ? null : data.getData();
            mUploadMessage.onReceiveValue(result);
            mUploadMessage = null;
        } else if (mUploadMessageArray != null) {
            // For Android 5.0+
            Uri[] results = null;
            if (resultCode == Activity.RESULT_OK && data != null) {
                String dataString = data.getDataString();
                ClipData clipData = data.getClipData();
                if

相关内容

热门资讯

黑科技辅助!wepoke透明挂... 黑科技辅助!wepoke透明挂是真的,(WePoKe试用)原来真的有挂,攻略教程(竟然有挂)-哔哩哔...
黑科技辅助!悟空黑桃a概率多少... 黑科技辅助!悟空黑桃a概率多少,(悟空黑桃a免费赛)原来真的有挂,大神讲解(真是)-哔哩哔哩;悟空黑...
黑科技辅助!Wepoke辅助器... 黑科技辅助!Wepoke辅助器,(Wepoke总结)原来真的有挂,辅助教程(有挂助手)-哔哩哔哩;W...
黑科技辅助!德扑之星软件辅牌器... 黑科技辅助!德扑之星软件辅牌器,(德扑ai代打)原来真的有挂,攻略教程(发现)-哔哩哔哩是一款可以让...
黑科技辅助!wepoke一定有... 黑科技辅助!wepoke一定有挂,(WePoKe真假)原来真的有挂,专业教程(有挂细节)-哔哩哔哩;...
黑科技辅助!wpk透视辅助,(... 黑科技辅助!wpk透视辅助,(WPK逻辑)原来真的有挂,教你攻略(技巧)-哔哩哔哩是一款可以让一直输...
黑科技辅助!德扑之星ai辅助神... 黑科技辅助!德扑之星ai辅助神器,(德扑ai助手)原来真的有挂,必备教程(工具)-哔哩哔哩;德扑ai...
黑科技辅助!wepoke辅助挂... 黑科技辅助!wepoke辅助挂,(Wepoke存在)原来真的有挂,2025新版(有挂细节)-哔哩哔哩...
黑科技辅助!微扑克数据采集,(... 您好:微扑克数据这款游戏可以开挂的,确实是有挂的,很多玩家在这款游戏中打牌都会发现很多用户的牌特别好...
黑科技辅助!wepoke确实有... 黑科技辅助!wepoke确实有挂,(WePoKe机器)原来真的有挂,插件教程(发现有挂)-哔哩哔哩;...