【问题标题】:webview, file input field filechooser doesn't show upwebview,文件输入字段文件选择器不显示
【发布时间】:2017-07-12 02:57:09
【问题描述】:

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

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

Android WebView File Upload

当我单击按钮时,文件选择器在 chrome 等桌面浏览器中运行良好,浏览器会打开一个对话框,我可以在其中选择要上传的文件。

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

有人可以帮助我吗?谢谢

【问题讨论】:

    标签: android android-studio webview


    【解决方案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);
    }
    

    【讨论】:

    • 链接中有git项目
    • @Anymore : Upvote 答案供其他人轻松参考
    • 它说我的声誉低于 15 所以不要更改公开显示的帖子分数
    猜你喜欢
    • 1970-01-01
    • 2014-03-11
    • 2012-03-11
    • 2015-06-12
    • 2015-01-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多