Android WebView 支持 文件上传(Html File Upload)

风流不在谈锋胜,袖手无言味最长。这篇文章主要讲述Android WebView 支持 文件上传(Html File Upload)相关的知识,希望能为你提供帮助。

背景:有个html页面,用html里面自带的< input type =file/> 上传文件(图片,word,Excel等)浏览器直接打开可以上传,
套壳在android app里面,点击文件上传没反应,修改Android代码,可以实现相应功能,亲测有效。

1、在oncreate 方法上面 加入以下代码:

1private static final int REQUEST_STORAGE = 1; 2private static final int REQUEST_LOCATION = 2; 3public ValueCallback< Uri> mUploadMessage; 4public static final int FILECHOOSER_RESULTCODE = 5173;

 
2、添加以下方法:
 
1webView.setWebChromeClient(new WebChromeClient() { 2 3// For Android 3.0+ 4public void openFileChooser(ValueCallback< Uri> uploadMsg) { 5 6mUploadMessage = uploadMsg; 7Intent i = new Intent(Intent.ACTION_GET_CONTENT); 8i.addCategory(Intent.CATEGORY_OPENABLE); 9i.setType("image/*"); 10MainActivity.this.startActivityForResult(Intent.createChooser(i,"File Chooser"), FILECHOOSER_RESULTCODE); 11 12} 13 14// For Android 3.0+ 15public void openFileChooser( ValueCallback uploadMsg, String acceptType ) { 16mUploadMessage = uploadMsg; 17Intent i = new Intent(Intent.ACTION_GET_CONTENT); 18i.addCategory(Intent.CATEGORY_OPENABLE); 19i.setType("*/*"); 20MainActivity.this.startActivityForResult( 21Intent.createChooser(i, "File Browser"), 22FILECHOOSER_RESULTCODE); 23} 24 25//For Android 4.1 26public void openFileChooser(ValueCallback< Uri> uploadMsg, String acceptType, String capture){ 27mUploadMessage = uploadMsg; 28Intent i = new Intent(Intent.ACTION_GET_CONTENT); 29i.addCategory(Intent.CATEGORY_OPENABLE); 30i.setType("image/*"); 31MainActivity.this.startActivityForResult( Intent.createChooser( i, "File Chooser" ), MainActivity.FILECHOOSER_RESULTCODE ); 32} 33 34public boolean onShowFileChooser( 35WebView webView, ValueCallback< Uri[]> filePathCallback, 36FileChooserParams fileChooserParams) { 37if (mUMA != null) { 38mUMA.onReceiveValue(null); 39} 40mUMA = filePathCallback; 41Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 42if (takePictureIntent.resolveActivity(MainActivity .this.getPackageManager()) != null) { 43File photoFile = null; 44try { 45photoFile = createImageFile(); 46takePictureIntent.putExtra("PhotoPath", mCM); 47} catch (IOException ex) { 48//Log.e(TAG, "Image file creation failed", ex); 49} 50if (photoFile != null) { 51mCM = "file:" + photoFile.getAbsolutePath(); 52takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoFile)); 53} else { 54takePictureIntent = null; 55} 56} 57 58Intent contentSelectionIntent = new Intent(Intent.ACTION_GET_CONTENT); 59contentSelectionIntent.addCategory(Intent.CATEGORY_OPENABLE); 60contentSelectionIntent.setType("*/*"); 61Intent[] intentArray; 62if (takePictureIntent != null) { 63intentArray = new Intent[]{takePictureIntent}; 64} else { 65intentArray = new Intent[0]; 66} 67Intent chooserIntent = new Intent(Intent.ACTION_CHOOSER); 68chooserIntent.putExtra(Intent.EXTRA_INTENT, contentSelectionIntent); 69chooserIntent.putExtra(Intent.EXTRA_TITLE, "Image Chooser"); 70chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, intentArray); 71startActivityForResult(chooserIntent, FCR); 72return true; 73} 74});

3、添加方法:
1// Create an image file 2private File createImageFile() throws IOException { 3@SuppressLint("SimpleDateFormat") String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new java.util.Date()); 4String imageFileName = "img_" + timeStamp + "_"; 5File storageDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES); 6return File.createTempFile(imageFileName, ".jpg", storageDir); 7}

4、onActivityResult
1@Override 2protected void onActivityResult(int requestCode, int resultCode, Intent data) { 3// TODO Auto-generated method stub 4super.onActivityResult(requestCode, resultCode, data); 5 6 if(requestCode==FILECHOOSER_RESULTCODE) { 7if (null == mUploadMessage) return; 8Uri result = data =https://www.songbingjia.com/android/= null || resultCode != RESULT_OK ? null 9: data.getData(); 10mUploadMessage.onReceiveValue(result); 11mUploadMessage = null; 12} 13 14if (Build.VERSION.SDK_INT > = 21) { 15Uri[] results = null; 16 //Check if response is positive 17if (resultCode == Activity.RESULT_OK) { 18if (requestCode == FCR) { 19if (null == mUMA) { 20return; 21} 22if (data == null) { 23 //Capture Photo if no image available 24if (mCM != null) { 25results = new Uri[]{Uri.parse(mCM)}; 26} 27} else { 28String dataString = data.getDataString(); 29if (dataString != null) { 30results = new Uri[]{Uri.parse(dataString)}; 31} 32} 33mUMA.onReceiveValue(results); 34mUMA = null; 35} 36} 37} else { 38if (requestCode == FCR) { 39if (null == mUM) return; 40Uri result = data == null || resultCode != RESULT_OK ? null : data.getData(); 41mUM.onReceiveValue(result); 42mUM = null; 43} 44} 45 }

【Android WebView 支持 文件上传(Html File Upload)】 
参考:https://www.zidsworld.com/android-development/make-android-webview-support-file-upload/
代码下载

    推荐阅读