title: Cannot upload image taken from Camera in android WebView [Can upload image from gallery]
body: <p>I developed website and converted it into Android app using WebView. Saw few implementation and tried FILE upload, where I can upload image from gallery but I am not allowed to upload image which is taken from Camera. I was thrown net::ERR_ACCESS_DENIED error. Please help me to get this fixed.</p>
  <p>MAINACTIVITY.java</p>
  <pre><code>    public class MainActivity extends AppCompatActivity {
      private WebView web;
      String webUrl = &quot;https://mywebsite.com/&quot;;
  
  
      public Context context;
  
      private static final String TAG = MainActivity.class.getSimpleName();
  
      private static final int FILECHOOSER_RESULTCODE = 1;
      private ValueCallback&lt;uri&gt; mUploadMessage;
      private Uri mCapturedImageURI = null;
  
      private ValueCallback&lt;Uri[]&gt; mFilePathCallback;
      private String mCameraPhotoPath;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
  
          web = (WebView) findViewById(R.id.myweb);
          web.loadUrl(webUrl);
  
          WebSettings mywebsettings = web.getSettings();
          mywebsettings.setJavaScriptEnabled(true);
  
          web.setWebViewClient(new WebViewClient());
  
          web.getSettings().setLoadsImagesAutomatically(true);
          web.getSettings().setRenderPriority(WebSettings.RenderPriority.HIGH);
          web.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
          web.getSettings().setAppCacheEnabled(true);
          web.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
          mywebsettings.setDomStorageEnabled(true);
          mywebsettings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NARROW_COLUMNS);
          mywebsettings.setUseWideViewPort(true);
          mywebsettings.setSavePassword(true);
          mywebsettings.setSaveFormData(true);
          mywebsettings.setEnableSmoothTransition(true);
  
  
          web.setWebChromeClient(new WebChromeClient() {
              public boolean onShowFileChooser(
                      WebView webView, ValueCallback&lt;Uri[]&gt; filePathCallback,
                      WebChromeClient.FileChooserParams fileChooserParams) {
                  if (mFilePathCallback != null) {
                      mFilePathCallback.onReceiveValue(null);
                  }
                  mFilePathCallback = filePathCallback;
  
                  Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                  if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
  
                      File photoFile = null;
                      try {
                          photoFile = createImageFile();
                          takePictureIntent.putExtra(&quot;PhotoPath&quot;, mCameraPhotoPath);
                      } catch (IOException ex) {
                          Log.e(TAG, &quot;Unable to create Image File&quot;, ex);
                      }
  
                      if (photoFile != null) {
                          mCameraPhotoPath = &quot;file:&quot; + 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(&quot;image/*&quot;);
  
                  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, getString(R.string.image_chooser));
                  chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, intentArray);
  
                  startActivityForResult(chooserIntent, FILECHOOSER_RESULTCODE);
  
                  return true;
              }
  
              private File createImageFile() throws IOException {
  
                  File imageStorageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), &quot;DirectoryNameHere&quot;);
  
                  if (!imageStorageDir.exists()) {
                      imageStorageDir.mkdirs();
                  }
  
                  imageStorageDir = new File(imageStorageDir + File.separator + &quot;IMG_&quot; + String.valueOf(System.currentTimeMillis()) + &quot;.jpg&quot;);
                  return imageStorageDir;
              }
  
              public void openFileChooser(ValueCallback&lt;uri&gt; uploadMsg, String acceptType) {
                  mUploadMessage = uploadMsg;
  
                  try {
                      File imageStorageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), &quot;DirectoryNameHere&quot;);
  
                      if (!imageStorageDir.exists()) {
                          imageStorageDir.mkdirs();
                      }
  
                      File file = new File(imageStorageDir + File.separator + &quot;IMG_&quot; + String.valueOf(System.currentTimeMillis()) + &quot;.jpg&quot;);
  
                      mCapturedImageURI = Uri.fromFile(file); // save to the private variable
  
                      final Intent captureIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
                      captureIntent.putExtra(MediaStore.EXTRA_OUTPUT, mCapturedImageURI);
                      
  
                      Intent i = new Intent(Intent.ACTION_GET_CONTENT);
                      i.addCategory(Intent.CATEGORY_OPENABLE);
                      i.setType(&quot;image/*&quot;);
  
                      Intent chooserIntent = Intent.createChooser(i, getString(R.string.image_chooser));
                      chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Parcelable[]{captureIntent});
  
                      startActivityForResult(chooserIntent, FILECHOOSER_RESULTCODE);
                  } catch (Exception e) {
                      Toast.makeText(getBaseContext(), &quot;Camera Exception:&quot; + e, Toast.LENGTH_LONG).show();
                  }
  
              }
  
              public void openFileChooser(ValueCallback&lt;Uri&gt; uploadMsg) {
                  openFileChooser(uploadMsg, &quot;&quot;);
              }
  
              public void openFileChooser(ValueCallback&lt;uri&gt; uploadMsg, String acceptType, String capture) {
                  openFileChooser(uploadMsg, acceptType);
              }
  
          });
      }
  
      
  
      verride
      public void onActivityResult(int requestCode, int resultCode, Intent data) {
  
          if (Build.VERSION.SDK_INT &lt; Build.VERSION_CODES.LOLLIPOP) {
  
              if (requestCode == FILECHOOSER_RESULTCODE) {
                  if (null == this.mUploadMessage) {
                      return;
                  }
  
                  Uri result = null;
  
                  try {
                      if (resultCode != RESULT_OK) {
                          result = null;
                      } else {
                          // retrieve from the private variable if the intent is null
                          result = data == null ? mCapturedImageURI : data.getData();
                      }
                  } catch (Exception e) {
                      Toast.makeText(getApplicationContext(), &quot;activity :&quot; + e, Toast.LENGTH_LONG).show();
                  }
  
                  mUploadMessage.onReceiveValue(result);
                  mUploadMessage = null;
              }
  
          } 
  
          if (Build.VERSION.SDK_INT &gt;= Build.VERSION_CODES.LOLLIPOP) {
  
              if (requestCode != FILECHOOSER_RESULTCODE || mFilePathCallback == null) {
                  super.onActivityResult(requestCode, resultCode, data);
                  return;
              }
  
              Uri[] results = null;
  
              if (resultCode == Activity.RESULT_OK) {
                  if (data == null || data.getData() == null) {
                      // if there is not data, then we may have taken a photo
                      if (mCameraPhotoPath != null) {
                          results = new Uri[]{Uri.parse(mCameraPhotoPath)};
                      }
                  } else {
                      String dataString = data.getDataString();
                      if (dataString != null) {
                          results = new Uri[]{Uri.parse(dataString)};
                      }
                  }
              }
  
              mFilePathCallback.onReceiveValue(results);
              mFilePathCallback = null;
  
          } 
          
  
      }
  
      @Override
      public void onBackPressed() {
          if(web.canGoBack()){
  
              web.goBack();
          }
          else {
  
              super.onBackPressed();
          }
      }
  }
  </code></pre>
  <p>activity_main.xml</p>
  <pre><code>&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
  &lt;androidx.constraintlayout.widget.ConstraintLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
      xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
      xmlns:tools=&quot;http://schemas.android.com/tools&quot;
      android:layout_width=&quot;match_parent&quot;
      android:layout_height=&quot;match_parent&quot;
      tools:context=&quot;.MainActivity&quot;&gt;
  
      &lt;WebView
          android:id=&quot;@+id/web&quot;
          android:layout_width=&quot;match_parent&quot;
          android:layout_height=&quot;match_parent&quot; /&gt;
  
  &lt;/androidx.constraintlayout.widget.ConstraintLayout&gt;
  </code></pre>
  <p>using following app permissions:</p>
  <pre><code>&lt;uses-permission android:name=&quot;android.permission.INTERNET&quot; /&gt;
  &lt;uses-permission android:name=&quot;android.permission.ACCESS_NETWORK_STATE&quot; /&gt;
  &lt;uses-permission android:name=&quot;android.permission.WRITE_EXTERNAL_STORAGE&quot;/&gt;
  &lt;uses-permission android:name=&quot;android.permission.CAMERA&quot;/&gt;
  &lt;uses-permission android:name=&quot;android.permission.CAMERA2&quot;/&gt;
  </code></pre>
  <p>strings.xml</p>
  <pre><code>  &lt;string name=&quot;image_chooser&quot;&gt;File Chooser&lt;/string&gt;
  </code></pre>