【问题标题】:Filechooser doesn't work in webview文件选择器在 webview 中不起作用
【发布时间】:2016-07-26 11:52:54
【问题描述】:

当我在使用 web 视图的地方打开我的应用程序时,我点击了文件选择器,然后什么也没发生。当我尝试在浏览器中的网站上时,它可以工作。

为什么文件选择器在 Android Webview 中不起作用?

【问题讨论】:

    标签: android webview filechooser


    【解决方案1】:

    您必须在代码中实现 javascript 接口。基本上您必须与 Javascript 和您的 Activity 进行通信。 考虑这个示例并根据您的需要进行更改。

    https://www.opengeeks.me/2015/08/filechooser-and-android-webview/

    【讨论】:

    • 我怎样才能为更多的图像制作这个示例。我还想上传其他文件。
    【解决方案2】:

    使用此代码在 webview 打开时从设备获取文件:

    web.setWebChromeClient(new WebChromeClient()  
        {  
               //The undocumented magic method override  
               //Eclipse will swear at you if you try to put @Override here  
            // For Android 3.0+
            public void openFileChooser(ValueCallback<Uri> uploadMsg) {  
    
                mUploadMessage = uploadMsg;  
                Intent i = new Intent(Intent.ACTION_GET_CONTENT);  
                i.addCategory(Intent.CATEGORY_OPENABLE);  
                i.setType("image/*");  
                MyWb.this.startActivityForResult(Intent.createChooser(i,"File Chooser"), FILECHOOSER_RESULTCODE);  
    
               }
    
            // For Android 3.0+
               public void openFileChooser( ValueCallback uploadMsg, String acceptType ) {
               mUploadMessage = uploadMsg;
               Intent i = new Intent(Intent.ACTION_GET_CONTENT);
               i.addCategory(Intent.CATEGORY_OPENABLE);
               i.setType("*/*");
               MyWb.this.startActivityForResult(
               Intent.createChooser(i, "File Browser"),
               FILECHOOSER_RESULTCODE);
               }
    
            //For Android 4.1
               public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture){
                   mUploadMessage = uploadMsg;  
                   Intent i = new Intent(Intent.ACTION_GET_CONTENT);  
                   i.addCategory(Intent.CATEGORY_OPENABLE);  
                   i.setType("image/*");  
                   MyWb.this.startActivityForResult( Intent.createChooser( i, "File Chooser" ), MyWb.FILECHOOSER_RESULTCODE );
    
               }
    
        });  
    
    
    
     @Override  
     protected void onActivityResult(int requestCode, int resultCode,  
                                        Intent intent) {  
      if(requestCode==FILECHOOSER_RESULTCODE)  
      {  
       if (null == mUploadMessage) return;  
                Uri result = intent == null || resultCode != RESULT_OK ? null  
                        : intent.getData();  
                mUploadMessage.onReceiveValue(result);  
                mUploadMessage = null;  
      }
      }  
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-03-11
      • 1970-01-01
      • 1970-01-01
      • 2015-03-22
      • 1970-01-01
      • 2017-06-21
      • 2016-02-25
      • 2016-07-03
      相关资源
      最近更新 更多