android项目从零开始 Android webview支持 文件上传、图片上传

来源:互联网 发布:t6软件是什么 编辑:程序博客网 时间:2024/05/17 05:56

项目中用webview包裹网站,出现上传文件无反应的情况,之前了解过文件上传。现在的前端页面需要处理的是

1.打开本地文件选择器

2.用户选择需要上传的文件

3.处理用户选择的文件通知webview

4.前端提交表单上传文件


首先需要webview 对 JS 的支持

webview.getSettings().setJavaScriptEnable(true)


前端页面针对不同版本的android系统 webview内核会有不同,调用方法不一致

webview.setWebChromeClient(new WebChromeClient() {        // For Android < 3.0        public void openFileChooser(ValueCallback<Uri> valueCallback) {            ***        }        // For Android  >= 3.0        public void openFileChooser(ValueCallback valueCallback, String acceptType) {            ***        }        //For Android  >= 4.1        public void openFileChooser(ValueCallback<Uri> valueCallback,                 String acceptType, String capture) {            ***        }        // For Android >= 5.0        @Override        public boolean onShowFileChooser(WebView webView,                 ValueCallback<Uri[]> filePathCallback,                 WebChromeClient.FileChooserParams fileChooserParams) {            ***            return true;        }    });



完整代码


public class MainActivity extends AppCompatActivity {    private ValueCallback<Uri> uploadMessage;    private ValueCallback<Uri[]> uploadMessageAboveL;    private final static int FILE_CHOOSER_RESULT_CODE = 10000;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        WebView webview = (WebView) findViewById(R.id.web_view);        assert webview != null;        WebSettings settings = webview.getSettings();        settings.setUseWideViewPort(true);        settings.setLoadWithOverviewMode(true);        settings.setJavaScriptEnabled(true);        webview.setWebChromeClient(new WebChromeClient() {            // For Android < 3.0            public void openFileChooser(ValueCallback<Uri> valueCallback) {                uploadMessage = valueCallback;                openImageChooserActivity();            }            // For Android  >= 3.0            public void openFileChooser(ValueCallback valueCallback, String acceptType) {                uploadMessage = valueCallback;                openImageChooserActivity();            }            //For Android  >= 4.1            public void openFileChooser(ValueCallback<Uri> valueCallback, String acceptType, String capture) {                uploadMessage = valueCallback;                openImageChooserActivity();            }            // For Android >= 5.0            @Override            public boolean onShowFileChooser(WebView webView, ValueCallback<Uri[]> filePathCallback, WebChromeClient.FileChooserParams fileChooserParams) {                uploadMessageAboveL = filePathCallback;                openImageChooserActivity();                return true;            }        });        String targetUrl = "file:///android_asset/up.html";        webview.loadUrl(targetUrl);    }    private void openImageChooserActivity() {        Intent i = new Intent(Intent.ACTION_GET_CONTENT);        i.addCategory(Intent.CATEGORY_OPENABLE);        i.setType("image/*");        startActivityForResult(Intent.createChooser(i, "Image Chooser"), FILE_CHOOSER_RESULT_CODE);    }    @Override    protected void onActivityResult(int requestCode, int resultCode, Intent data) {        super.onActivityResult(requestCode, resultCode, data);        if (requestCode == FILE_CHOOSER_RESULT_CODE) {            if (null == uploadMessage && null == uploadMessageAboveL) return;            Uri result = data == null || resultCode != RESULT_OK ? null : data.getData();            if (uploadMessageAboveL != null) {                onActivityResultAboveL(requestCode, resultCode, data);            } else if (uploadMessage != null) {                uploadMessage.onReceiveValue(result);                uploadMessage = null;            }        }    }    @TargetApi(Build.VERSION_CODES.LOLLIPOP)    private void onActivityResultAboveL(int requestCode, int resultCode, Intent intent) {        if (requestCode != FILE_CHOOSER_RESULT_CODE || uploadMessageAboveL == null)            return;        Uri[] results = null;        if (resultCode == Activity.RESULT_OK) {            if (intent != null) {                String dataString = intent.getDataString();                ClipData clipData = intent.getClipData();                if (clipData != null) {                    results = new Uri[clipData.getItemCount()];                    for (int i = 0; i < clipData.getItemCount(); i++) {                        ClipData.Item item = clipData.getItemAt(i);                        results[i] = item.getUri();                    }                }                if (dataString != null)                    results = new Uri[]{Uri.parse(dataString)};            }        }        uploadMessageAboveL.onReceiveValue(results);        uploadMessageAboveL = null;    }}


原创粉丝点击