有乎-价值、共享、信任

What you care about-value, sharing, trust

安卓解决Webview打包后无法上传图片的问题

| 阅读:357 发表时间:2021-11-13 09:16:06 技术专栏

主要是修改自定义webClient部分方法

private ValueCallback mUploadMessage;
private ValueCallback<Uri[]> uploadMessage;
private final static int FILECHOOSER_RESULTCODE = 1;
public static final int REQUEST_SELECT_FILE = 100;
//在onCreate方法中获取WebView对象,并设置WebViewClient
WebChromeClient chromeClient = new WebChromeClientImpl();
mWebView.setWebChromeClient(chromeClient);

//防止在浏览器打开
mWebView.setWebViewClient(new WebViewClient(){
    @Override    public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
        return super.shouldOverrideUrlLoading(view, request);
    }
});

主要类和接收方法

public class WebChromeClientImpl extends WebChromeClient {
    @Override    public boolean onJsAlert(WebView view, String url, String message, JsResult result) {
        AlertDialog.Builder builder = new AlertDialog.Builder(view.getContext());
        builder.setTitle("提示").setMessage(message).setPositiveButton("确定", null);
        builder.setCancelable(false);
        AlertDialog dialog = builder.create();
        dialog.show();
        result.confirm();
        return true;
    }


    public void openFileChooser(ValueCallback uploadMsg) {
        mUploadMessage = uploadMsg;
        Intent i = new Intent(Intent.ACTION_GET_CONTENT);
        i.addCategory(Intent.CATEGORY_OPENABLE);
        i.setType("image/*");
        startActivityForResult(Intent.createChooser(i, "File Chooser"), FILECHOOSER_RESULTCODE);
    }

    //扩展浏览器上传文件    //3.0++版本    public void openFileChooser( ValueCallback uploadMsg, String acceptType ) {
        mUploadMessage = uploadMsg;
        Intent i = new Intent(Intent.ACTION_GET_CONTENT);
        i.addCategory(Intent.CATEGORY_OPENABLE);
        i.setType("*/*");
        startActivityForResult(
                Intent.createChooser(i, "File Browser"),
                FILECHOOSER_RESULTCODE);
    }

    //For Android 4.1    public void openFileChooser(ValueCallback uploadMsg, String acceptType, String capture) {
        mUploadMessage = uploadMsg;
        Intent i = new Intent(Intent.ACTION_GET_CONTENT);
        i.addCategory(Intent.CATEGORY_OPENABLE);
        i.setType("image/*");
        startActivityForResult(Intent.createChooser(i, "File Chooser"), FILECHOOSER_RESULTCODE);
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public boolean onShowFileChooser(WebView mWebView, ValueCallback<Uri[]> filePathCallback, WebChromeClient.FileChooserParams fileChooserParams) {
        if (uploadMessage != null) {
            uploadMessage.onReceiveValue(null);
            uploadMessage = null;
        }

        uploadMessage = filePathCallback;

        Intent intent = fileChooserParams.createIntent();
        try {
            startActivityForResult(Intent.createChooser(intent, "选择图片"), REQUEST_SELECT_FILE);
        } catch (ActivityNotFoundException e) {
            uploadMessage = null;
            Toast.makeText(getApplicationContext(), "文件上传失败", Toast.LENGTH_LONG).show();
            return false;
        }
        return true;
    }

}


protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        if (requestCode == REQUEST_SELECT_FILE) {
            if (uploadMessage == null)
                return;
            Uri result = (data == null || resultCode != RESULT_OK) ? null : data.getData();
            if (result != null) {
                uploadMessage.onReceiveValue(new Uri[]{result});
            } else {
                uploadMessage.onReceiveValue(new Uri[]{});
            }
            uploadMessage = null; //每次置空是为了防止下次点击按钮选择图片无响应        }
    } else if (requestCode == FILECHOOSER_RESULTCODE) {
        if (null == mUploadMessage)
            return;
        Uri result = data == null || resultCode != RESULT_OK ? null : data.getData();
        mUploadMessage.onReceiveValue(result); //注意此处        mUploadMessage = null;
    } else        Toast.makeText(getApplicationContext(), "上传文件失败", Toast.LENGTH_LONG).show();

    return;
}
*文章为作者独立观点,不代表【uuuho有乎】的立场
本文由【uuuho有乎】发表并编辑,转载此文章须经作者同意,并请附上出处及本页链接。如有侵权,请联系本站删除。