Android webview Input type=file 文件上传 解决方法
2017-05-13 14:35
627 查看
默认的情况下在HTML中 写
<input type=file />
的时候回弹出选择文件的窗口,但实际上在webview中默认的是不弹出窗口的
解决方法
在WebChromeClient中增加一些方法:
增加方法:
重写方法:
有些变量事先声明:
测试
成功!!
<input type=file />
的时候回弹出选择文件的窗口,但实际上在webview中默认的是不弹出窗口的
解决方法
在WebChromeClient中增加一些方法:
webView.setWebChromeClient(new WebChromeClient(){ //For Android 3.0+ public void openFileChooser(ValueCallback<Uri> uploadMsg){ mUM = uploadMsg; Intent i = new Intent(Intent.ACTION_GET_CONTENT); i.addCategory(Intent.CATEGORY_OPENABLE); i.setType("*/*"); MainActivity.this.startActivityForResult(Intent.createChooser(i,"File Chooser"), FCR); } // For Android 3.0+, above method not supported in some android 3+ versions, in such case we use this public void openFileChooser(ValueCallback uploadMsg, String acceptType){ mUM = uploadMsg; Intent i = new Intent(Intent.ACTION_GET_CONTENT); i.addCategory(Intent.CATEGORY_OPENABLE); i.setType("*/*"); MainActivity.this.startActivityForResult( Intent.createChooser(i, "File Browser"), FCR); } //For Android 4.1+ public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture){ mUM = uploadMsg; Intent i = new Intent(Intent.ACTION_GET_CONTENT); i.addCategory(Intent.CATEGORY_OPENABLE); i.setType("*/*"); MainActivity.this.startActivityForResult(Intent.createChooser(i, "File Chooser"), MainActivity.FCR); } //For Android 5.0+ public boolean onShowFileChooser( WebView webView, ValueCallback<Uri[]> filePathCallback, WebChromeClient.FileChooserParams fileChooserParams){ if(mUMA != null){ mUMA.onReceiveValue(null); } mUMA = filePathCallback; Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); if(takePictureIntent.resolveActivity(MainActivity.this.getPackageManager()) != null){ File photoFile = null; try{ photoFile = createImageFile(); takePictureIntent.putExtra("PhotoPath", mCM); }catch(IOException ex){ Log.e(TAG, "Image file creation failed", ex); } if(photoFile != null){ mCM = "file:" + photoFile.getAbsolutePath(); takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoFile)); }else{ takePictureIntent = null; } } Intent contentSelectionIntent = new Intent(Intent.ACTION_GET_CONTENT); contentSelectionIntent.addCategory(Intent.CATEGORY_OPENABLE); contentSelectionIntent.setType("*/*"); Intent[] intentArray; if(takePictureIntent != null){ intentArray = new Intent[]{takePictureIntent}; }else{ intentArray = new Intent[0]; } Intent chooserIntent = new Intent(Intent.ACTION_CHOOSER); chooserIntent.putExtra(Intent.EXTRA_INTENT, contentSelectionIntent); chooserIntent.putExtra(Intent.EXTRA_TITLE, "Image Chooser"); chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, intentArray); startActivityForResult(chooserIntent, FCR); return true; } });
增加方法:
// Create an image file private File createImageFile() throws IOException{ @SuppressLint("SimpleDateFormat") String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date()); String imageFileName = "img_"+timeStamp+"_"; File storageDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES); return File.createTempFile(imageFileName,".jpg",storageDir); }
重写方法:
@Override protected void onActivityResult(int requestCode, int resultCode, Intent intent){ super.onActivityResult(requestCode, resultCode, intent); if(Build.VERSION.SDK_INT >= 21){ Uri[] results = null; //Check if response is positive if(resultCode== Activity.RESULT_OK){ if(requestCode == FCR){ if(null == mUMA){ return; } if(intent == null){ //Capture Photo if no image available if(mCM != null){ results = new Uri[]{Uri.parse(mCM)}; } }else{ String dataString = intent.getDataString(); if(dataString != null){ results = new Uri[]{Uri.parse(dataString)}; } } } } mUMA.onReceiveValue(results); mUMA = null; }else{ if(requestCode == FCR){ if(null == mUM) return; Uri result = intent == null || resultCode != RESULT_OK ? null : intent.getData(); mUM.onReceiveValue(result); mUM = null; } } }
有些变量事先声明:
private static final String TAG = MainActivity.class.getSimpleName(); private String mCM; private ValueCallback<Uri> mUM; private ValueCallback<Uri[]> mUMA; private final static int FCR=1;
测试
成功!!
相关文章推荐
- Android-WebView-解决对选择文件<input type="file">无响应
- Android:让WebView支持<input type="file"…>元素,实现文件上传
- Android WebView H5页面 input type =“file”解决方法
- 【Android进阶】Android里webview不支持input file的解决方法(上传按钮点击失效的原因)
- Android WebView 不支持 H5 input type="file" 解决方法
- 【Android进阶】Android里webview不支持input file的解决方法(上传按钮点击失效的原因)
- Android WebView 不支持 H5 input type="file" 解决方法
- Android WebView 不支持 H5 input type="file" 解决方法
- Android WebView 不支持 H5 input type="file" 解决方法
- android webview使用html5<input id="input" type="file"/> 上传相册、拍照照片
- android webview type=file文件上传,安卓端代码
- Android WebView 支持H5图片上传<input type="file">
- Android WebView不支持H5输入type =“file”解决方法
- Android笔记:Webview 支持 input type=file选择上传图片
- Android webview上传文件失败,添加混淆解决办法
- Android:让WebView支持<input type=”file”…>元素
- Android学习之实现WebView中input="file"选择文件,处理选择图片无法返回类型问题
- android Webview支持input type=file
- Android 上的 Chrome 终于,即将,就要,支持文件上传(input type="file")了?
- IE input file隐藏不能上传文件解决方法(转载)