2016-07-26 8 views
-1

Webviewを使用する場所でアプリケーションを開くと、何も起こらないようにfilechooserをクリックします。私が試している間は、ブラウザのWebサイト上で動作します。WebviewでFilechooserが動作しない

Android Webviewでファイルチューザーが動作しないのはなぜですか?

答えて

2

あなたはJavascriptとあなたの活動と通信する必要があなたのcode.BasicallyはJavaScriptインタフェースを実装する必要があります。 このサンプルを検討し、必要に応じて変更してください。

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

+0

ありがとうございます!これは動作します!スーパー! –

+0

それ以上の画像に対してこの例を作るにはどうすればよいですか?他のファイルもアップロードしたい。 –

0

使用の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; 
    } 
    } 
関連する問題