title: Photo/Document Upload in Android Webview
body: <p>I'm working on a wrapper application for Android and need to connect to the camera/photo gallery (so the user can upload a picture of themselves for example). I've found a number of posts on the subject, including <a href="https://stackoverflow.com/questions/15725814/upload-an-image-from-camera-or-gallery-in-webview">Upload an Image from camera or gallery in WebView</a> and <a href="https://stackoverflow.com/questions/13284903/upload-camera-photo-and-filechooser-from-webview-input-field">Upload camera photo and filechooser from webview INPUT field</a>, but the code that I've modeled after these solutions isn't working. I put a print statement inside the openFileChooser function, and it isn't being triggered when the add photo/document Javascript link is hit in my webview. Any suggestions would be appreciated, I'm including my code below:</p>
  
  <pre><code> private Uri mCapturedImageURI = null;
  
  //make HTML upload button work in Webview
  private ValueCallback&lt;Uri&gt; mUploadMessage;
  private String filePath;
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_log_in_page);
      Intent currentIntent = getIntent();
      String action = currentIntent.getAction();
      String url;
  
      //check for permissions
      boolean camera = checkCameraPermissions();
      boolean location = checkLocationPermissions();
      boolean photos = checkPhotoPermissions();
  
  
      if (!location){
          ActivityCompat.requestPermissions(this,
                  new String[] {"android.permission.ACCESS_FINE_LOCATION"}, REQUEST_LOCATION);
          requested_location = true;
      }
  
  
      if (!camera ) {
          ActivityCompat.requestPermissions(this,
                  new String[] {"android.permission.CAMERA"}, REQUEST_CAMERA);
          requested_camera = true;
      }
  
  
      if (!photos) {
          ActivityCompat.requestPermissions(this,
                  new String[] {"android.permission.READ_EXTERNAL_STORAGE"}, REQUEST_PHOTOS);
          requested_photos = true;
      }
  
      url =  Constants.login_page;
  
      //start up the webview, initiate navigation client
      myWebView = (WebView) findViewById(R.id.webview);
      if (myWebView != null) {
          myWebView.getSettings().setJavaScriptEnabled(true);
          myWebView.getSettings().setDomStorageEnabled(true);
          myWebView.getSettings().setAllowFileAccess(true);
          myWebView.getSettings().setAllowFileAccessFromFileURLs(true);
          myWebView.getSettings().setAllowUniversalAccessFromFileURLs(true);
          myWebView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
          myWebView.getSettings().setSupportMultipleWindows(true);
          myWebView.getSettings().setGeolocationEnabled(true);
          myWebView.setWebChromeClient(new WebChromeClient() {
  
              public void onGeolocationPermissionsShowPrompt(String origin, GeolocationPermissions.Callback callback) {
                  callback.invoke(origin, true, false);
              }
  
              @SuppressWarnings("unused")
              public void openFileChooser(ValueCallback&lt;Uri&gt; uploadMsg, String AcceptType){
                  this.openFileChooser(uploadMsg);
              }
  
              @SuppressWarnings("unused")
              public void openFileChooser(ValueCallback&lt;Uri&gt; uploadMsg, String AcceptType, String capture) {
                  this.openFileChooser(uploadMsg);
              }
  
              private Uri imageUri;
  
              public void openFileChooser(ValueCallback&lt;Uri&gt; uploadMsg ) {
                  // Update message
                  mUploadMessage = uploadMsg;
  
                  System.out.println("we are here");
  
                  File imageStorageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "my_app");
                  filePath = imageStorageDir + File.separator + "IMG_" + String.valueOf(System.currentTimeMillis()) + ".jpg";
                  File file = new File(filePath);
  
                  Intent captureIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
                  captureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));
                  LogInPage.this.startActivityForResult(captureIntent, REQUEST_PHOTOS);
              }
  
              });
          //myWebView.addJavascriptInterface(new MyJavascriptInterface(this), "Android");
          myWebView.loadUrl(url);
          myWebView.setWebViewClient(new NavigationClient());
      }
  }
  
  
  
  
     @Override
  protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
  
      if ((requestCode == SELECT_PHOTO) ||(requestCode == REQUEST_CAMERA)) {
          if (null == this.mUploadMessage || (resultCode != RESULT_OK &amp;&amp; !new File(filePath).exists())) {
              this.mUploadMessage.onReceiveValue(null);
          } else {
              ContentValues values = new ContentValues();
              values.put(MediaStore.Images.Media.DATA, this.filePath);
              this.mUploadMessage.onReceiveValue(this.getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values));
          }
          this.mUploadMessage = null;
      }
  }
  </code></pre>