Android WebView中无法上传从相机拍摄的图片,但可以上传来自相册的图片。
创始人
2024-08-19 10:30:11
0

在Android WebView中,要实现从相机拍摄图片并上传,可以通过以下步骤实现:

  1. 创建一个自定义的WebChromeClient类,重写其onShowFileChooser方法。该方法在用户选择上传文件时被调用。
public class MyWebChromeClient extends WebChromeClient {
    private ValueCallback mUploadCallback;
    private final static int FILE_CHOOSER_RESULT_CODE = 1;

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

        Intent intent = fileChooserParams.createIntent();
        try {
            startActivityForResult(intent, FILE_CHOOSER_RESULT_CODE);
        } catch (ActivityNotFoundException e) {
            mUploadCallback = null;
            Toast.makeText(getContext(), "Cannot Open File Chooser", Toast.LENGTH_LONG).show();
            return false;
        }
        return true;
    }

    // For Android < 5.0
    public void openFileChooser(ValueCallback uploadMsg) {
        openFileChooser(uploadMsg, null);
    }

    // For Android 3.0+
    public void openFileChooser(ValueCallback uploadMsg, String acceptType) {
        openFileChooser(uploadMsg, acceptType, null);
    }

    // For Android 4.1+
    public void openFileChooser(ValueCallback uploadMsg, String acceptType, String capture) {
        mUploadCallback = uploadMsg;

        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);
    }

    // Handle the file chooser result
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == FILE_CHOOSER_RESULT_CODE) {
            if (resultCode == Activity.RESULT_OK && mUploadCallback != null) {
                Uri[] result = null;
                if (data != null) {
                    String dataString = data.getDataString();
                    if (dataString != null) {
                        result = new Uri[]{Uri.parse(dataString)};
                    }
                }
                mUploadCallback.onReceiveValue(result);
            } else {
                mUploadCallback.onReceiveValue(null);
            }
            mUploadCallback = null;
        }
    }
}
  1. 在你的WebView中设置自定义的WebChromeClient。
WebView webView = findViewById(R.id.webView);
webView.setWebChromeClient(new MyWebChromeClient());

现在,当用户点击上传按钮时,会弹出文件选择对话框,用户可以选择拍摄照片并上传。

注意:在AndroidManifest.xml文件中需要添加相机和存储权限。



相关内容

热门资讯

据文件显示!wepoker透视... 据文件显示!wepoker透视脚本苹果版,如何下载wpk透视版(透视)详细教程(有挂方法)-哔哩哔哩...
今天下午!智星德州插件2024... 今天下午!智星德州插件2024最新版,wepoker透视脚本网页(透视)推荐教程(新版有挂)-哔哩哔...
现有说明如下!aapoker辅... 现有说明如下!aapoker辅助工具安全吗,wepoker分析(透视)科普教程(新版有挂)-哔哩哔哩...
反观!wpk模拟器是什么,we... 反观!wpk模拟器是什么,wepoker辅助软件视频(透视)有挂教程(有挂详细)-哔哩哔哩1、wep...
事发当天!wepoker高级辅... 事发当天!wepoker高级辅助,cloudpoker辅助(透视)了解教程(有挂详细)-哔哩哔哩1、...
透视有挂!来玩app破解版,哈... 透视有挂!来玩app破解版,哈糖大菠萝能开挂吗(透视)曝光教程(有挂方法)-哔哩哔哩1、这是跨平台的...
据统计!wepoker怎么破解... 据统计!wepoker怎么破解游戏,wepoker正确养号方法(透视)必备教程(揭秘有挂)-哔哩哔哩...
据报道!wepoker插件程序... 据报道!wepoker插件程序激活码,aapoker透视怎么用(透视)曝光教程(有挂工具)-哔哩哔哩...
透视ai!wepoker私人局... 透视ai!wepoker私人局开挂视频,sohoo poker辅助(透视)了解教程(有挂方法)-哔哩...
辅助透视!wepokerplu... 辅助透视!wepokerplus到底是挂了吗,werplan免费挂下载(透视)推荐教程(发现有挂)-...