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

Posted amz520

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android WebView 支持 文件上传(Html File Upload)相关的知识,希望对你有一定的参考价值。

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

1、在oncreate 方法上面 加入以下代码:
1     private static final int REQUEST_STORAGE = 1;
2     private static final int REQUEST_LOCATION = 2;
3     public ValueCallback<Uri> mUploadMessage;
4     public static final int FILECHOOSER_RESULTCODE = 5173;

 

2、添加以下方法:

 

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

3、添加方法:

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

4、onActivityResult

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

 

参考:https://www.zidsworld.com/android-development/make-android-webview-support-file-upload/

代码下载

以上是关于Android WebView 支持 文件上传(Html File Upload)的主要内容,如果未能解决你的问题,请参考以下文章

android使用webview上传文件(支持相册和拍照),支持最高6.0安卓系统(改进版)

Android WebView中如何实现类似于UC浏览器上传文件时的选择框?

webview触摸屏上传程序失败

Android webview上传图片(调起相册/相机上传)

Android Studio - 如何在 webview Lollipop 中上传文件 (Android 5.0)

WebView Android Studio 中的文件上传