webview,文件输入字段filechooser不显示

时间:2017-02-22 07:21:37

标签: android android-studio webview

我一直在努力尝试从webview上传文件。 我尝试了很多解决方案,但都没有。

http://m0s-programming.blogspot.tw/2011/02/file-upload-in-through-webview-on.html

Android WebView File Upload

当我点击按钮时,filechooser在桌面浏览器(如chrome)中工作正常,浏览器会打开一个对话框,我可以在其中选择要上传的文件。

当我使用手机测试程序时,我点击选择文件,对话框没有反应

有人能帮助我吗?感谢

1 个答案:

答案 0 :(得分:1)

请参阅此文档:

  

https://infeeds.com/d/CODEmgks/20475/upload-image-file-gallery-or-camera-webv

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("image/*");
     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;
}

创建图像文件功能,如上面的代码所述,我们需要这个来创建要上传的新临时文件。

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);
}