upload image from camera in webview does not work





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







0















I've been trying to do an upload of images in Workplace from facebook through webview from gallery and from camera.



From gallery it works fine but from camera the image doesn't appear on the upload.



I've seen similar posts with this question like this and this but i don't see what's wrong.



This is my class:



public class WorkplaceActivity extends BaseActivity implements WorkplaceContract.View {

private WorkplacePresenter workplacePresenter;
private Tracker trackerGA;
private MyApplicaton appGA;
private Toolbar toolbar;
private WebView ctWebView;
private ValueCallback<Uri> mUploadMessage;
private final static int FILECHOOSER_RESULTCODE = 1;
private Uri mCameraURI;

@Override
public void setupWorkplace() {
WebViewClient webclient = new WebViewClient();

ctWebView = (WebView) findViewById(R.id.ctWebView);
ctWebView.getSettings().setAppCacheEnabled(true);
ctWebView.getSettings().setJavaScriptEnabled(true);
ctWebView.getSettings().setLoadWithOverviewMode(true);
ctWebView.getSettings().setAllowFileAccess(true);
ctWebView.setWebViewClient(webclient);
ctWebView.loadUrl(ConstantsStrings.WORKPLACE_URL);
}

@Override
public void setupGA() {
// Google Analytics
appGA = (MyApplicaton) getApplication();
trackerGA = appGA.getDefaultTracker();
sendGA(ConstantsStrings.WORKPLACE_GA_IN);
}

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_workplace);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

workplacePresenter = new WorkplacePresenter();
workplacePresenter.attachView(this);
workplacePresenter.checkPermissions(WorkplaceActivity.this);

ActionBar actionBar = this.getSupportActionBar();
if (actionBar != null) {
actionBar.setDisplayHomeAsUpEnabled(false);
actionBar.setDisplayShowHomeEnabled(false);
actionBar.setHomeAsUpIndicator(R.drawable.ic_arrow_left);
actionBar.setTitle("");

// add back button
ImageButton back_button = (ImageButton) findViewById(R.id.back_button);
back_button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (ctWebView.canGoBack()) {
ctWebView.goBack();
}

Log.d("WORKPLACE", "click back");
}
});

// add close button
ImageButton close_button = (ImageButton) findViewById(R.id.close_button);
close_button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
finish();

Log.d("WORKPLACE", "click close");
}
});

}

setupGA();
setupWorkplace();
ctWebView.setWebChromeClient(new WebChromeClient() {
@Override
public boolean onShowFileChooser(WebView webView, ValueCallback<Uri> filePathCallback, FileChooserParams fileChooserParams) {
WorkplaceActivity.this.openFileChooser(filePathCallback);
return true;
}
});
}

@Override
public void onBackPressed() {
super.onBackPressed();
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
if (requestCode == FILECHOOSER_RESULTCODE) {
if (mUploadMessage != null) {
Uri temp = new Uri[1];
if (intent != null && intent.getData() != null) {
temp[0] = intent.getData();
} else if (mCameraURI != null) {
temp[0] = mCameraURI;
}
mUploadMessage.onReceiveValue(temp);
mUploadMessage = null;
}
}
}

public static Intent newInstance(Context context) {
return new Intent(context, WorkplaceActivity.class);
}

@Override
public void onAttachedToWindow() {
super.onAttachedToWindow();
workplacePresenter.attachView(this);
}

@Override
public void onDetachedFromWindow() {
super.onDetachedFromWindow();
workplacePresenter.detachView();
}

@Override
public void showLoading() {
}

@Override
public void hideLoading() {
}

@Override
public void sendGA(String msg) {
// Google Analytics
trackerGA = appGA.getDefaultTracker();
trackerGA.setScreenName(msg);
trackerGA.send(new HitBuilders.ScreenViewBuilder().build());
}

private void openFileChooser(ValueCallback<Uri> uploadMsg) {
mUploadMessage = uploadMsg;

try {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
ActivityCompat.requestPermissions(WorkplaceActivity.this, new String{Manifest.permission.CAMERA}, FILECHOOSER_RESULTCODE);
ActivityCompat.requestPermissions(WorkplaceActivity.this, new String{Manifest.permission_group.STORAGE}, 6);
ActivityCompat.requestPermissions(WorkplaceActivity.this, new String{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 5);
File storageDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
File cameraDir = new File(storageDir.getAbsolutePath() + File.separator + "browser-photos");
cameraDir.mkdirs();
String mCameraFilePath = cameraDir.getAbsolutePath() + File.separator + ".jpg";
mCameraURI = Uri.fromFile(new File(mCameraFilePath));
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, mCameraURI);


Intent contentSelectionIntent = new Intent(Intent.ACTION_GET_CONTENT);
contentSelectionIntent.addCategory(Intent.CATEGORY_OPENABLE);
contentSelectionIntent.setType("image/*");

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, new Parcelable{takePictureIntent});

startActivityForResult(chooserIntent, FILECHOOSER_RESULTCODE);
} catch (Exception e) {
Toast.makeText(getBaseContext(), "Camera Exception:" + e, Toast.LENGTH_LONG);
}
}


When i do try to upload an image from the camera, my phone saves the image but it does not appear on the upload screen.










share|improve this question































    0















    I've been trying to do an upload of images in Workplace from facebook through webview from gallery and from camera.



    From gallery it works fine but from camera the image doesn't appear on the upload.



    I've seen similar posts with this question like this and this but i don't see what's wrong.



    This is my class:



    public class WorkplaceActivity extends BaseActivity implements WorkplaceContract.View {

    private WorkplacePresenter workplacePresenter;
    private Tracker trackerGA;
    private MyApplicaton appGA;
    private Toolbar toolbar;
    private WebView ctWebView;
    private ValueCallback<Uri> mUploadMessage;
    private final static int FILECHOOSER_RESULTCODE = 1;
    private Uri mCameraURI;

    @Override
    public void setupWorkplace() {
    WebViewClient webclient = new WebViewClient();

    ctWebView = (WebView) findViewById(R.id.ctWebView);
    ctWebView.getSettings().setAppCacheEnabled(true);
    ctWebView.getSettings().setJavaScriptEnabled(true);
    ctWebView.getSettings().setLoadWithOverviewMode(true);
    ctWebView.getSettings().setAllowFileAccess(true);
    ctWebView.setWebViewClient(webclient);
    ctWebView.loadUrl(ConstantsStrings.WORKPLACE_URL);
    }

    @Override
    public void setupGA() {
    // Google Analytics
    appGA = (MyApplicaton) getApplication();
    trackerGA = appGA.getDefaultTracker();
    sendGA(ConstantsStrings.WORKPLACE_GA_IN);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_workplace);
    toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    workplacePresenter = new WorkplacePresenter();
    workplacePresenter.attachView(this);
    workplacePresenter.checkPermissions(WorkplaceActivity.this);

    ActionBar actionBar = this.getSupportActionBar();
    if (actionBar != null) {
    actionBar.setDisplayHomeAsUpEnabled(false);
    actionBar.setDisplayShowHomeEnabled(false);
    actionBar.setHomeAsUpIndicator(R.drawable.ic_arrow_left);
    actionBar.setTitle("");

    // add back button
    ImageButton back_button = (ImageButton) findViewById(R.id.back_button);
    back_button.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
    if (ctWebView.canGoBack()) {
    ctWebView.goBack();
    }

    Log.d("WORKPLACE", "click back");
    }
    });

    // add close button
    ImageButton close_button = (ImageButton) findViewById(R.id.close_button);
    close_button.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
    finish();

    Log.d("WORKPLACE", "click close");
    }
    });

    }

    setupGA();
    setupWorkplace();
    ctWebView.setWebChromeClient(new WebChromeClient() {
    @Override
    public boolean onShowFileChooser(WebView webView, ValueCallback<Uri> filePathCallback, FileChooserParams fileChooserParams) {
    WorkplaceActivity.this.openFileChooser(filePathCallback);
    return true;
    }
    });
    }

    @Override
    public void onBackPressed() {
    super.onBackPressed();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
    if (requestCode == FILECHOOSER_RESULTCODE) {
    if (mUploadMessage != null) {
    Uri temp = new Uri[1];
    if (intent != null && intent.getData() != null) {
    temp[0] = intent.getData();
    } else if (mCameraURI != null) {
    temp[0] = mCameraURI;
    }
    mUploadMessage.onReceiveValue(temp);
    mUploadMessage = null;
    }
    }
    }

    public static Intent newInstance(Context context) {
    return new Intent(context, WorkplaceActivity.class);
    }

    @Override
    public void onAttachedToWindow() {
    super.onAttachedToWindow();
    workplacePresenter.attachView(this);
    }

    @Override
    public void onDetachedFromWindow() {
    super.onDetachedFromWindow();
    workplacePresenter.detachView();
    }

    @Override
    public void showLoading() {
    }

    @Override
    public void hideLoading() {
    }

    @Override
    public void sendGA(String msg) {
    // Google Analytics
    trackerGA = appGA.getDefaultTracker();
    trackerGA.setScreenName(msg);
    trackerGA.send(new HitBuilders.ScreenViewBuilder().build());
    }

    private void openFileChooser(ValueCallback<Uri> uploadMsg) {
    mUploadMessage = uploadMsg;

    try {
    Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    ActivityCompat.requestPermissions(WorkplaceActivity.this, new String{Manifest.permission.CAMERA}, FILECHOOSER_RESULTCODE);
    ActivityCompat.requestPermissions(WorkplaceActivity.this, new String{Manifest.permission_group.STORAGE}, 6);
    ActivityCompat.requestPermissions(WorkplaceActivity.this, new String{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 5);
    File storageDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
    File cameraDir = new File(storageDir.getAbsolutePath() + File.separator + "browser-photos");
    cameraDir.mkdirs();
    String mCameraFilePath = cameraDir.getAbsolutePath() + File.separator + ".jpg";
    mCameraURI = Uri.fromFile(new File(mCameraFilePath));
    takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, mCameraURI);


    Intent contentSelectionIntent = new Intent(Intent.ACTION_GET_CONTENT);
    contentSelectionIntent.addCategory(Intent.CATEGORY_OPENABLE);
    contentSelectionIntent.setType("image/*");

    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, new Parcelable{takePictureIntent});

    startActivityForResult(chooserIntent, FILECHOOSER_RESULTCODE);
    } catch (Exception e) {
    Toast.makeText(getBaseContext(), "Camera Exception:" + e, Toast.LENGTH_LONG);
    }
    }


    When i do try to upload an image from the camera, my phone saves the image but it does not appear on the upload screen.










    share|improve this question



























      0












      0








      0








      I've been trying to do an upload of images in Workplace from facebook through webview from gallery and from camera.



      From gallery it works fine but from camera the image doesn't appear on the upload.



      I've seen similar posts with this question like this and this but i don't see what's wrong.



      This is my class:



      public class WorkplaceActivity extends BaseActivity implements WorkplaceContract.View {

      private WorkplacePresenter workplacePresenter;
      private Tracker trackerGA;
      private MyApplicaton appGA;
      private Toolbar toolbar;
      private WebView ctWebView;
      private ValueCallback<Uri> mUploadMessage;
      private final static int FILECHOOSER_RESULTCODE = 1;
      private Uri mCameraURI;

      @Override
      public void setupWorkplace() {
      WebViewClient webclient = new WebViewClient();

      ctWebView = (WebView) findViewById(R.id.ctWebView);
      ctWebView.getSettings().setAppCacheEnabled(true);
      ctWebView.getSettings().setJavaScriptEnabled(true);
      ctWebView.getSettings().setLoadWithOverviewMode(true);
      ctWebView.getSettings().setAllowFileAccess(true);
      ctWebView.setWebViewClient(webclient);
      ctWebView.loadUrl(ConstantsStrings.WORKPLACE_URL);
      }

      @Override
      public void setupGA() {
      // Google Analytics
      appGA = (MyApplicaton) getApplication();
      trackerGA = appGA.getDefaultTracker();
      sendGA(ConstantsStrings.WORKPLACE_GA_IN);
      }

      @Override
      protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_workplace);
      toolbar = (Toolbar) findViewById(R.id.toolbar);
      setSupportActionBar(toolbar);

      workplacePresenter = new WorkplacePresenter();
      workplacePresenter.attachView(this);
      workplacePresenter.checkPermissions(WorkplaceActivity.this);

      ActionBar actionBar = this.getSupportActionBar();
      if (actionBar != null) {
      actionBar.setDisplayHomeAsUpEnabled(false);
      actionBar.setDisplayShowHomeEnabled(false);
      actionBar.setHomeAsUpIndicator(R.drawable.ic_arrow_left);
      actionBar.setTitle("");

      // add back button
      ImageButton back_button = (ImageButton) findViewById(R.id.back_button);
      back_button.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View view) {
      if (ctWebView.canGoBack()) {
      ctWebView.goBack();
      }

      Log.d("WORKPLACE", "click back");
      }
      });

      // add close button
      ImageButton close_button = (ImageButton) findViewById(R.id.close_button);
      close_button.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View view) {
      finish();

      Log.d("WORKPLACE", "click close");
      }
      });

      }

      setupGA();
      setupWorkplace();
      ctWebView.setWebChromeClient(new WebChromeClient() {
      @Override
      public boolean onShowFileChooser(WebView webView, ValueCallback<Uri> filePathCallback, FileChooserParams fileChooserParams) {
      WorkplaceActivity.this.openFileChooser(filePathCallback);
      return true;
      }
      });
      }

      @Override
      public void onBackPressed() {
      super.onBackPressed();
      }

      @Override
      protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
      if (requestCode == FILECHOOSER_RESULTCODE) {
      if (mUploadMessage != null) {
      Uri temp = new Uri[1];
      if (intent != null && intent.getData() != null) {
      temp[0] = intent.getData();
      } else if (mCameraURI != null) {
      temp[0] = mCameraURI;
      }
      mUploadMessage.onReceiveValue(temp);
      mUploadMessage = null;
      }
      }
      }

      public static Intent newInstance(Context context) {
      return new Intent(context, WorkplaceActivity.class);
      }

      @Override
      public void onAttachedToWindow() {
      super.onAttachedToWindow();
      workplacePresenter.attachView(this);
      }

      @Override
      public void onDetachedFromWindow() {
      super.onDetachedFromWindow();
      workplacePresenter.detachView();
      }

      @Override
      public void showLoading() {
      }

      @Override
      public void hideLoading() {
      }

      @Override
      public void sendGA(String msg) {
      // Google Analytics
      trackerGA = appGA.getDefaultTracker();
      trackerGA.setScreenName(msg);
      trackerGA.send(new HitBuilders.ScreenViewBuilder().build());
      }

      private void openFileChooser(ValueCallback<Uri> uploadMsg) {
      mUploadMessage = uploadMsg;

      try {
      Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
      ActivityCompat.requestPermissions(WorkplaceActivity.this, new String{Manifest.permission.CAMERA}, FILECHOOSER_RESULTCODE);
      ActivityCompat.requestPermissions(WorkplaceActivity.this, new String{Manifest.permission_group.STORAGE}, 6);
      ActivityCompat.requestPermissions(WorkplaceActivity.this, new String{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 5);
      File storageDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
      File cameraDir = new File(storageDir.getAbsolutePath() + File.separator + "browser-photos");
      cameraDir.mkdirs();
      String mCameraFilePath = cameraDir.getAbsolutePath() + File.separator + ".jpg";
      mCameraURI = Uri.fromFile(new File(mCameraFilePath));
      takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, mCameraURI);


      Intent contentSelectionIntent = new Intent(Intent.ACTION_GET_CONTENT);
      contentSelectionIntent.addCategory(Intent.CATEGORY_OPENABLE);
      contentSelectionIntent.setType("image/*");

      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, new Parcelable{takePictureIntent});

      startActivityForResult(chooserIntent, FILECHOOSER_RESULTCODE);
      } catch (Exception e) {
      Toast.makeText(getBaseContext(), "Camera Exception:" + e, Toast.LENGTH_LONG);
      }
      }


      When i do try to upload an image from the camera, my phone saves the image but it does not appear on the upload screen.










      share|improve this question
















      I've been trying to do an upload of images in Workplace from facebook through webview from gallery and from camera.



      From gallery it works fine but from camera the image doesn't appear on the upload.



      I've seen similar posts with this question like this and this but i don't see what's wrong.



      This is my class:



      public class WorkplaceActivity extends BaseActivity implements WorkplaceContract.View {

      private WorkplacePresenter workplacePresenter;
      private Tracker trackerGA;
      private MyApplicaton appGA;
      private Toolbar toolbar;
      private WebView ctWebView;
      private ValueCallback<Uri> mUploadMessage;
      private final static int FILECHOOSER_RESULTCODE = 1;
      private Uri mCameraURI;

      @Override
      public void setupWorkplace() {
      WebViewClient webclient = new WebViewClient();

      ctWebView = (WebView) findViewById(R.id.ctWebView);
      ctWebView.getSettings().setAppCacheEnabled(true);
      ctWebView.getSettings().setJavaScriptEnabled(true);
      ctWebView.getSettings().setLoadWithOverviewMode(true);
      ctWebView.getSettings().setAllowFileAccess(true);
      ctWebView.setWebViewClient(webclient);
      ctWebView.loadUrl(ConstantsStrings.WORKPLACE_URL);
      }

      @Override
      public void setupGA() {
      // Google Analytics
      appGA = (MyApplicaton) getApplication();
      trackerGA = appGA.getDefaultTracker();
      sendGA(ConstantsStrings.WORKPLACE_GA_IN);
      }

      @Override
      protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_workplace);
      toolbar = (Toolbar) findViewById(R.id.toolbar);
      setSupportActionBar(toolbar);

      workplacePresenter = new WorkplacePresenter();
      workplacePresenter.attachView(this);
      workplacePresenter.checkPermissions(WorkplaceActivity.this);

      ActionBar actionBar = this.getSupportActionBar();
      if (actionBar != null) {
      actionBar.setDisplayHomeAsUpEnabled(false);
      actionBar.setDisplayShowHomeEnabled(false);
      actionBar.setHomeAsUpIndicator(R.drawable.ic_arrow_left);
      actionBar.setTitle("");

      // add back button
      ImageButton back_button = (ImageButton) findViewById(R.id.back_button);
      back_button.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View view) {
      if (ctWebView.canGoBack()) {
      ctWebView.goBack();
      }

      Log.d("WORKPLACE", "click back");
      }
      });

      // add close button
      ImageButton close_button = (ImageButton) findViewById(R.id.close_button);
      close_button.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View view) {
      finish();

      Log.d("WORKPLACE", "click close");
      }
      });

      }

      setupGA();
      setupWorkplace();
      ctWebView.setWebChromeClient(new WebChromeClient() {
      @Override
      public boolean onShowFileChooser(WebView webView, ValueCallback<Uri> filePathCallback, FileChooserParams fileChooserParams) {
      WorkplaceActivity.this.openFileChooser(filePathCallback);
      return true;
      }
      });
      }

      @Override
      public void onBackPressed() {
      super.onBackPressed();
      }

      @Override
      protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
      if (requestCode == FILECHOOSER_RESULTCODE) {
      if (mUploadMessage != null) {
      Uri temp = new Uri[1];
      if (intent != null && intent.getData() != null) {
      temp[0] = intent.getData();
      } else if (mCameraURI != null) {
      temp[0] = mCameraURI;
      }
      mUploadMessage.onReceiveValue(temp);
      mUploadMessage = null;
      }
      }
      }

      public static Intent newInstance(Context context) {
      return new Intent(context, WorkplaceActivity.class);
      }

      @Override
      public void onAttachedToWindow() {
      super.onAttachedToWindow();
      workplacePresenter.attachView(this);
      }

      @Override
      public void onDetachedFromWindow() {
      super.onDetachedFromWindow();
      workplacePresenter.detachView();
      }

      @Override
      public void showLoading() {
      }

      @Override
      public void hideLoading() {
      }

      @Override
      public void sendGA(String msg) {
      // Google Analytics
      trackerGA = appGA.getDefaultTracker();
      trackerGA.setScreenName(msg);
      trackerGA.send(new HitBuilders.ScreenViewBuilder().build());
      }

      private void openFileChooser(ValueCallback<Uri> uploadMsg) {
      mUploadMessage = uploadMsg;

      try {
      Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
      ActivityCompat.requestPermissions(WorkplaceActivity.this, new String{Manifest.permission.CAMERA}, FILECHOOSER_RESULTCODE);
      ActivityCompat.requestPermissions(WorkplaceActivity.this, new String{Manifest.permission_group.STORAGE}, 6);
      ActivityCompat.requestPermissions(WorkplaceActivity.this, new String{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 5);
      File storageDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
      File cameraDir = new File(storageDir.getAbsolutePath() + File.separator + "browser-photos");
      cameraDir.mkdirs();
      String mCameraFilePath = cameraDir.getAbsolutePath() + File.separator + ".jpg";
      mCameraURI = Uri.fromFile(new File(mCameraFilePath));
      takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, mCameraURI);


      Intent contentSelectionIntent = new Intent(Intent.ACTION_GET_CONTENT);
      contentSelectionIntent.addCategory(Intent.CATEGORY_OPENABLE);
      contentSelectionIntent.setType("image/*");

      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, new Parcelable{takePictureIntent});

      startActivityForResult(chooserIntent, FILECHOOSER_RESULTCODE);
      } catch (Exception e) {
      Toast.makeText(getBaseContext(), "Camera Exception:" + e, Toast.LENGTH_LONG);
      }
      }


      When i do try to upload an image from the camera, my phone saves the image but it does not appear on the upload screen.







      android image webview






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Feb 28 '18 at 19:23







      Amadiu

















      asked Feb 28 '18 at 18:56









      AmadiuAmadiu

      12




      12
























          1 Answer
          1






          active

          oldest

          votes


















          0














          check this it's worked for me.



          public class MainActivity extends Activity {

          private WebView webView;
          private ValueCallback<Uri> mUploadMessage;
          private ValueCallback<Uri> mUploadMessages;
          private Uri mCapturedImageURI = null;
          private static final int MY_CAMERA_REQUEST_CODE = 100;
          private static final int FILECHOOSER_RESULTCODE = 1;

          @RequiresApi(api = Build.VERSION_CODES.M)
          @Override
          protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);

          webView = findViewById(R.id.webView);

          webView.setWebViewClient(new WebViewClient());

          WebSettings webSettings = webView.getSettings();
          webSettings.setJavaScriptEnabled(true);
          webSettings.setDomStorageEnabled(true);
          webView.getSettings().setJavaScriptEnabled(true);
          webView.getSettings().setDomStorageEnabled(true);
          webView.getSettings().setPluginState(WebSettings.PluginState.ON);
          webView.getSettings().setAppCacheEnabled(false);
          webView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
          webView.getSettings().setUseWideViewPort(true);
          webView.getSettings().setLoadWithOverviewMode(true);
          webView.getSettings().setBuiltInZoomControls(false);
          webView.getSettings().setSupportZoom(false);
          webView.getSettings().setDefaultZoom(WebSettings.ZoomDensity.FAR);
          webView.getSettings().setDomStorageEnabled(true);
          webView.getSettings().setDatabaseEnabled(true);
          webView.getSettings().setDatabasePath("/data/data/" + webView.getContext().getPackageName() + "/databases/");

          webView.loadUrl("url");

          webView.setWebChromeClient(new WebChromeClient() {

          // For api level bellow 24
          public boolean shouldOverrideUrlLoading(WebView view, String url) {

          if (url.startsWith("http")) {
          // Return false means, web view will handle the link
          return false;
          }

          return false;
          }


          // From api level 24
          @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
          public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
          /*Toast.makeText(mcontext, "New Method",Toast.LENGTH_SHORT).show();*/

          // Get the tel: url
          String url = request.getUrl().toString();

          if (url.startsWith("http")) {
          // Return false means, web view will handle the link
          return false;
          }

          return false;
          }

          // openFileChooser for Android 3.0+

          public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType) {
          mUploadMessage = uploadMsg;
          openImageChooser();
          }

          // For Lollipop 5.0+ Devices

          public boolean onShowFileChooser(WebView mWebView, ValueCallback<Uri> filePathCallback, WebChromeClient.FileChooserParams fileChooserParams) {
          mUploadMessages = filePathCallback;
          openImageChooser();
          return true;
          }

          // openFileChooser for Android < 3.0

          public void openFileChooser(ValueCallback<Uri> uploadMsg) {
          openFileChooser(uploadMsg, "");
          }

          //openFileChooser for other Android versions

          public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) {
          openFileChooser(uploadMsg, acceptType);
          }
          });

          }

          private void openImageChooser() {
          try {

          File imageStorageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "FolderName");

          if (!imageStorageDir.exists()) {
          imageStorageDir.mkdirs();
          }
          File file = new File(imageStorageDir + File.separator + "IMG_" + String.valueOf(System.currentTimeMillis()) + ".jpg");
          mCapturedImageURI = Uri.fromFile(file);

          final Intent captureIntent = new Intent(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("image/*");

          Intent chooserIntent = Intent.createChooser(i, "Image Chooser");
          chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Parcelable{captureIntent});

          startActivityForResult(chooserIntent, FILECHOOSER_RESULTCODE);
          } catch (Exception e) {
          e.printStackTrace();
          }
          }

          @Override
          protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
          super.onActivityResult(requestCode, resultCode, intent);

          if (requestCode == FILECHOOSER_RESULTCODE) {

          if (null == this.mUploadMessage && null == this.mUploadMessages) {
          return;
          }

          /* Uri result;
          if (requestCode != RESULT_OK){
          result = null;
          }else {
          result = intent == null ? this.mCapturedImageURI : intent.getData();
          }
          this.mUploadMessage.onReceiveValue(result);
          this.mUploadMessage = null;*/
          if (null != mUploadMessage) {
          handleUploadMessage(requestCode, resultCode, intent);

          } else if (mUploadMessages != null) {
          handleUploadMessages(requestCode, resultCode, intent);
          }
          }
          }

          private void handleUploadMessage(int requestCode, int resultCode, Intent intent) {
          Uri result = null;
          try {
          if (resultCode != RESULT_OK) {
          result = null;
          } else {
          // retrieve from the private variable if the intent is null

          result = intent == null ? mCapturedImageURI : intent.getData();
          }
          } catch (Exception e) {
          e.printStackTrace();
          }
          mUploadMessage.onReceiveValue(result);
          mUploadMessage = null;
          }

          @TargetApi(Build.VERSION_CODES.LOLLIPOP)
          private void handleUploadMessages(int requestCode, int resultCode, Intent intent) {
          Uri results = null;
          try {
          if (resultCode != RESULT_OK) {
          results = null;
          } else {
          if (intent != null) {
          String dataString = intent.getDataString();
          ClipData clipData = intent.getClipData();
          if (clipData != null) {
          results = new Uri[clipData.getItemCount()];
          for (int i = 0; i < clipData.getItemCount(); i++) {
          ClipData.Item item = clipData.getItemAt(i);
          results[i] = item.getUri();
          }
          }
          if (dataString != null) {
          results = new Uri{Uri.parse(dataString)};
          }
          } else {
          results = new Uri{mCapturedImageURI};
          }
          }
          } catch (Exception e) {
          e.printStackTrace();
          }
          mUploadMessages.onReceiveValue(results);
          mUploadMessages = null;
          }

          }





          share|improve this answer


























          • Don't forget to add storage and camera permission in manifest as well as programmatically.

            – A.Gun
            Nov 17 '18 at 7:50












          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f49036661%2fupload-image-from-camera-in-webview-does-not-work%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














          check this it's worked for me.



          public class MainActivity extends Activity {

          private WebView webView;
          private ValueCallback<Uri> mUploadMessage;
          private ValueCallback<Uri> mUploadMessages;
          private Uri mCapturedImageURI = null;
          private static final int MY_CAMERA_REQUEST_CODE = 100;
          private static final int FILECHOOSER_RESULTCODE = 1;

          @RequiresApi(api = Build.VERSION_CODES.M)
          @Override
          protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);

          webView = findViewById(R.id.webView);

          webView.setWebViewClient(new WebViewClient());

          WebSettings webSettings = webView.getSettings();
          webSettings.setJavaScriptEnabled(true);
          webSettings.setDomStorageEnabled(true);
          webView.getSettings().setJavaScriptEnabled(true);
          webView.getSettings().setDomStorageEnabled(true);
          webView.getSettings().setPluginState(WebSettings.PluginState.ON);
          webView.getSettings().setAppCacheEnabled(false);
          webView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
          webView.getSettings().setUseWideViewPort(true);
          webView.getSettings().setLoadWithOverviewMode(true);
          webView.getSettings().setBuiltInZoomControls(false);
          webView.getSettings().setSupportZoom(false);
          webView.getSettings().setDefaultZoom(WebSettings.ZoomDensity.FAR);
          webView.getSettings().setDomStorageEnabled(true);
          webView.getSettings().setDatabaseEnabled(true);
          webView.getSettings().setDatabasePath("/data/data/" + webView.getContext().getPackageName() + "/databases/");

          webView.loadUrl("url");

          webView.setWebChromeClient(new WebChromeClient() {

          // For api level bellow 24
          public boolean shouldOverrideUrlLoading(WebView view, String url) {

          if (url.startsWith("http")) {
          // Return false means, web view will handle the link
          return false;
          }

          return false;
          }


          // From api level 24
          @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
          public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
          /*Toast.makeText(mcontext, "New Method",Toast.LENGTH_SHORT).show();*/

          // Get the tel: url
          String url = request.getUrl().toString();

          if (url.startsWith("http")) {
          // Return false means, web view will handle the link
          return false;
          }

          return false;
          }

          // openFileChooser for Android 3.0+

          public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType) {
          mUploadMessage = uploadMsg;
          openImageChooser();
          }

          // For Lollipop 5.0+ Devices

          public boolean onShowFileChooser(WebView mWebView, ValueCallback<Uri> filePathCallback, WebChromeClient.FileChooserParams fileChooserParams) {
          mUploadMessages = filePathCallback;
          openImageChooser();
          return true;
          }

          // openFileChooser for Android < 3.0

          public void openFileChooser(ValueCallback<Uri> uploadMsg) {
          openFileChooser(uploadMsg, "");
          }

          //openFileChooser for other Android versions

          public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) {
          openFileChooser(uploadMsg, acceptType);
          }
          });

          }

          private void openImageChooser() {
          try {

          File imageStorageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "FolderName");

          if (!imageStorageDir.exists()) {
          imageStorageDir.mkdirs();
          }
          File file = new File(imageStorageDir + File.separator + "IMG_" + String.valueOf(System.currentTimeMillis()) + ".jpg");
          mCapturedImageURI = Uri.fromFile(file);

          final Intent captureIntent = new Intent(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("image/*");

          Intent chooserIntent = Intent.createChooser(i, "Image Chooser");
          chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Parcelable{captureIntent});

          startActivityForResult(chooserIntent, FILECHOOSER_RESULTCODE);
          } catch (Exception e) {
          e.printStackTrace();
          }
          }

          @Override
          protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
          super.onActivityResult(requestCode, resultCode, intent);

          if (requestCode == FILECHOOSER_RESULTCODE) {

          if (null == this.mUploadMessage && null == this.mUploadMessages) {
          return;
          }

          /* Uri result;
          if (requestCode != RESULT_OK){
          result = null;
          }else {
          result = intent == null ? this.mCapturedImageURI : intent.getData();
          }
          this.mUploadMessage.onReceiveValue(result);
          this.mUploadMessage = null;*/
          if (null != mUploadMessage) {
          handleUploadMessage(requestCode, resultCode, intent);

          } else if (mUploadMessages != null) {
          handleUploadMessages(requestCode, resultCode, intent);
          }
          }
          }

          private void handleUploadMessage(int requestCode, int resultCode, Intent intent) {
          Uri result = null;
          try {
          if (resultCode != RESULT_OK) {
          result = null;
          } else {
          // retrieve from the private variable if the intent is null

          result = intent == null ? mCapturedImageURI : intent.getData();
          }
          } catch (Exception e) {
          e.printStackTrace();
          }
          mUploadMessage.onReceiveValue(result);
          mUploadMessage = null;
          }

          @TargetApi(Build.VERSION_CODES.LOLLIPOP)
          private void handleUploadMessages(int requestCode, int resultCode, Intent intent) {
          Uri results = null;
          try {
          if (resultCode != RESULT_OK) {
          results = null;
          } else {
          if (intent != null) {
          String dataString = intent.getDataString();
          ClipData clipData = intent.getClipData();
          if (clipData != null) {
          results = new Uri[clipData.getItemCount()];
          for (int i = 0; i < clipData.getItemCount(); i++) {
          ClipData.Item item = clipData.getItemAt(i);
          results[i] = item.getUri();
          }
          }
          if (dataString != null) {
          results = new Uri{Uri.parse(dataString)};
          }
          } else {
          results = new Uri{mCapturedImageURI};
          }
          }
          } catch (Exception e) {
          e.printStackTrace();
          }
          mUploadMessages.onReceiveValue(results);
          mUploadMessages = null;
          }

          }





          share|improve this answer


























          • Don't forget to add storage and camera permission in manifest as well as programmatically.

            – A.Gun
            Nov 17 '18 at 7:50
















          0














          check this it's worked for me.



          public class MainActivity extends Activity {

          private WebView webView;
          private ValueCallback<Uri> mUploadMessage;
          private ValueCallback<Uri> mUploadMessages;
          private Uri mCapturedImageURI = null;
          private static final int MY_CAMERA_REQUEST_CODE = 100;
          private static final int FILECHOOSER_RESULTCODE = 1;

          @RequiresApi(api = Build.VERSION_CODES.M)
          @Override
          protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);

          webView = findViewById(R.id.webView);

          webView.setWebViewClient(new WebViewClient());

          WebSettings webSettings = webView.getSettings();
          webSettings.setJavaScriptEnabled(true);
          webSettings.setDomStorageEnabled(true);
          webView.getSettings().setJavaScriptEnabled(true);
          webView.getSettings().setDomStorageEnabled(true);
          webView.getSettings().setPluginState(WebSettings.PluginState.ON);
          webView.getSettings().setAppCacheEnabled(false);
          webView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
          webView.getSettings().setUseWideViewPort(true);
          webView.getSettings().setLoadWithOverviewMode(true);
          webView.getSettings().setBuiltInZoomControls(false);
          webView.getSettings().setSupportZoom(false);
          webView.getSettings().setDefaultZoom(WebSettings.ZoomDensity.FAR);
          webView.getSettings().setDomStorageEnabled(true);
          webView.getSettings().setDatabaseEnabled(true);
          webView.getSettings().setDatabasePath("/data/data/" + webView.getContext().getPackageName() + "/databases/");

          webView.loadUrl("url");

          webView.setWebChromeClient(new WebChromeClient() {

          // For api level bellow 24
          public boolean shouldOverrideUrlLoading(WebView view, String url) {

          if (url.startsWith("http")) {
          // Return false means, web view will handle the link
          return false;
          }

          return false;
          }


          // From api level 24
          @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
          public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
          /*Toast.makeText(mcontext, "New Method",Toast.LENGTH_SHORT).show();*/

          // Get the tel: url
          String url = request.getUrl().toString();

          if (url.startsWith("http")) {
          // Return false means, web view will handle the link
          return false;
          }

          return false;
          }

          // openFileChooser for Android 3.0+

          public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType) {
          mUploadMessage = uploadMsg;
          openImageChooser();
          }

          // For Lollipop 5.0+ Devices

          public boolean onShowFileChooser(WebView mWebView, ValueCallback<Uri> filePathCallback, WebChromeClient.FileChooserParams fileChooserParams) {
          mUploadMessages = filePathCallback;
          openImageChooser();
          return true;
          }

          // openFileChooser for Android < 3.0

          public void openFileChooser(ValueCallback<Uri> uploadMsg) {
          openFileChooser(uploadMsg, "");
          }

          //openFileChooser for other Android versions

          public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) {
          openFileChooser(uploadMsg, acceptType);
          }
          });

          }

          private void openImageChooser() {
          try {

          File imageStorageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "FolderName");

          if (!imageStorageDir.exists()) {
          imageStorageDir.mkdirs();
          }
          File file = new File(imageStorageDir + File.separator + "IMG_" + String.valueOf(System.currentTimeMillis()) + ".jpg");
          mCapturedImageURI = Uri.fromFile(file);

          final Intent captureIntent = new Intent(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("image/*");

          Intent chooserIntent = Intent.createChooser(i, "Image Chooser");
          chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Parcelable{captureIntent});

          startActivityForResult(chooserIntent, FILECHOOSER_RESULTCODE);
          } catch (Exception e) {
          e.printStackTrace();
          }
          }

          @Override
          protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
          super.onActivityResult(requestCode, resultCode, intent);

          if (requestCode == FILECHOOSER_RESULTCODE) {

          if (null == this.mUploadMessage && null == this.mUploadMessages) {
          return;
          }

          /* Uri result;
          if (requestCode != RESULT_OK){
          result = null;
          }else {
          result = intent == null ? this.mCapturedImageURI : intent.getData();
          }
          this.mUploadMessage.onReceiveValue(result);
          this.mUploadMessage = null;*/
          if (null != mUploadMessage) {
          handleUploadMessage(requestCode, resultCode, intent);

          } else if (mUploadMessages != null) {
          handleUploadMessages(requestCode, resultCode, intent);
          }
          }
          }

          private void handleUploadMessage(int requestCode, int resultCode, Intent intent) {
          Uri result = null;
          try {
          if (resultCode != RESULT_OK) {
          result = null;
          } else {
          // retrieve from the private variable if the intent is null

          result = intent == null ? mCapturedImageURI : intent.getData();
          }
          } catch (Exception e) {
          e.printStackTrace();
          }
          mUploadMessage.onReceiveValue(result);
          mUploadMessage = null;
          }

          @TargetApi(Build.VERSION_CODES.LOLLIPOP)
          private void handleUploadMessages(int requestCode, int resultCode, Intent intent) {
          Uri results = null;
          try {
          if (resultCode != RESULT_OK) {
          results = null;
          } else {
          if (intent != null) {
          String dataString = intent.getDataString();
          ClipData clipData = intent.getClipData();
          if (clipData != null) {
          results = new Uri[clipData.getItemCount()];
          for (int i = 0; i < clipData.getItemCount(); i++) {
          ClipData.Item item = clipData.getItemAt(i);
          results[i] = item.getUri();
          }
          }
          if (dataString != null) {
          results = new Uri{Uri.parse(dataString)};
          }
          } else {
          results = new Uri{mCapturedImageURI};
          }
          }
          } catch (Exception e) {
          e.printStackTrace();
          }
          mUploadMessages.onReceiveValue(results);
          mUploadMessages = null;
          }

          }





          share|improve this answer


























          • Don't forget to add storage and camera permission in manifest as well as programmatically.

            – A.Gun
            Nov 17 '18 at 7:50














          0












          0








          0







          check this it's worked for me.



          public class MainActivity extends Activity {

          private WebView webView;
          private ValueCallback<Uri> mUploadMessage;
          private ValueCallback<Uri> mUploadMessages;
          private Uri mCapturedImageURI = null;
          private static final int MY_CAMERA_REQUEST_CODE = 100;
          private static final int FILECHOOSER_RESULTCODE = 1;

          @RequiresApi(api = Build.VERSION_CODES.M)
          @Override
          protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);

          webView = findViewById(R.id.webView);

          webView.setWebViewClient(new WebViewClient());

          WebSettings webSettings = webView.getSettings();
          webSettings.setJavaScriptEnabled(true);
          webSettings.setDomStorageEnabled(true);
          webView.getSettings().setJavaScriptEnabled(true);
          webView.getSettings().setDomStorageEnabled(true);
          webView.getSettings().setPluginState(WebSettings.PluginState.ON);
          webView.getSettings().setAppCacheEnabled(false);
          webView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
          webView.getSettings().setUseWideViewPort(true);
          webView.getSettings().setLoadWithOverviewMode(true);
          webView.getSettings().setBuiltInZoomControls(false);
          webView.getSettings().setSupportZoom(false);
          webView.getSettings().setDefaultZoom(WebSettings.ZoomDensity.FAR);
          webView.getSettings().setDomStorageEnabled(true);
          webView.getSettings().setDatabaseEnabled(true);
          webView.getSettings().setDatabasePath("/data/data/" + webView.getContext().getPackageName() + "/databases/");

          webView.loadUrl("url");

          webView.setWebChromeClient(new WebChromeClient() {

          // For api level bellow 24
          public boolean shouldOverrideUrlLoading(WebView view, String url) {

          if (url.startsWith("http")) {
          // Return false means, web view will handle the link
          return false;
          }

          return false;
          }


          // From api level 24
          @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
          public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
          /*Toast.makeText(mcontext, "New Method",Toast.LENGTH_SHORT).show();*/

          // Get the tel: url
          String url = request.getUrl().toString();

          if (url.startsWith("http")) {
          // Return false means, web view will handle the link
          return false;
          }

          return false;
          }

          // openFileChooser for Android 3.0+

          public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType) {
          mUploadMessage = uploadMsg;
          openImageChooser();
          }

          // For Lollipop 5.0+ Devices

          public boolean onShowFileChooser(WebView mWebView, ValueCallback<Uri> filePathCallback, WebChromeClient.FileChooserParams fileChooserParams) {
          mUploadMessages = filePathCallback;
          openImageChooser();
          return true;
          }

          // openFileChooser for Android < 3.0

          public void openFileChooser(ValueCallback<Uri> uploadMsg) {
          openFileChooser(uploadMsg, "");
          }

          //openFileChooser for other Android versions

          public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) {
          openFileChooser(uploadMsg, acceptType);
          }
          });

          }

          private void openImageChooser() {
          try {

          File imageStorageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "FolderName");

          if (!imageStorageDir.exists()) {
          imageStorageDir.mkdirs();
          }
          File file = new File(imageStorageDir + File.separator + "IMG_" + String.valueOf(System.currentTimeMillis()) + ".jpg");
          mCapturedImageURI = Uri.fromFile(file);

          final Intent captureIntent = new Intent(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("image/*");

          Intent chooserIntent = Intent.createChooser(i, "Image Chooser");
          chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Parcelable{captureIntent});

          startActivityForResult(chooserIntent, FILECHOOSER_RESULTCODE);
          } catch (Exception e) {
          e.printStackTrace();
          }
          }

          @Override
          protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
          super.onActivityResult(requestCode, resultCode, intent);

          if (requestCode == FILECHOOSER_RESULTCODE) {

          if (null == this.mUploadMessage && null == this.mUploadMessages) {
          return;
          }

          /* Uri result;
          if (requestCode != RESULT_OK){
          result = null;
          }else {
          result = intent == null ? this.mCapturedImageURI : intent.getData();
          }
          this.mUploadMessage.onReceiveValue(result);
          this.mUploadMessage = null;*/
          if (null != mUploadMessage) {
          handleUploadMessage(requestCode, resultCode, intent);

          } else if (mUploadMessages != null) {
          handleUploadMessages(requestCode, resultCode, intent);
          }
          }
          }

          private void handleUploadMessage(int requestCode, int resultCode, Intent intent) {
          Uri result = null;
          try {
          if (resultCode != RESULT_OK) {
          result = null;
          } else {
          // retrieve from the private variable if the intent is null

          result = intent == null ? mCapturedImageURI : intent.getData();
          }
          } catch (Exception e) {
          e.printStackTrace();
          }
          mUploadMessage.onReceiveValue(result);
          mUploadMessage = null;
          }

          @TargetApi(Build.VERSION_CODES.LOLLIPOP)
          private void handleUploadMessages(int requestCode, int resultCode, Intent intent) {
          Uri results = null;
          try {
          if (resultCode != RESULT_OK) {
          results = null;
          } else {
          if (intent != null) {
          String dataString = intent.getDataString();
          ClipData clipData = intent.getClipData();
          if (clipData != null) {
          results = new Uri[clipData.getItemCount()];
          for (int i = 0; i < clipData.getItemCount(); i++) {
          ClipData.Item item = clipData.getItemAt(i);
          results[i] = item.getUri();
          }
          }
          if (dataString != null) {
          results = new Uri{Uri.parse(dataString)};
          }
          } else {
          results = new Uri{mCapturedImageURI};
          }
          }
          } catch (Exception e) {
          e.printStackTrace();
          }
          mUploadMessages.onReceiveValue(results);
          mUploadMessages = null;
          }

          }





          share|improve this answer















          check this it's worked for me.



          public class MainActivity extends Activity {

          private WebView webView;
          private ValueCallback<Uri> mUploadMessage;
          private ValueCallback<Uri> mUploadMessages;
          private Uri mCapturedImageURI = null;
          private static final int MY_CAMERA_REQUEST_CODE = 100;
          private static final int FILECHOOSER_RESULTCODE = 1;

          @RequiresApi(api = Build.VERSION_CODES.M)
          @Override
          protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);

          webView = findViewById(R.id.webView);

          webView.setWebViewClient(new WebViewClient());

          WebSettings webSettings = webView.getSettings();
          webSettings.setJavaScriptEnabled(true);
          webSettings.setDomStorageEnabled(true);
          webView.getSettings().setJavaScriptEnabled(true);
          webView.getSettings().setDomStorageEnabled(true);
          webView.getSettings().setPluginState(WebSettings.PluginState.ON);
          webView.getSettings().setAppCacheEnabled(false);
          webView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
          webView.getSettings().setUseWideViewPort(true);
          webView.getSettings().setLoadWithOverviewMode(true);
          webView.getSettings().setBuiltInZoomControls(false);
          webView.getSettings().setSupportZoom(false);
          webView.getSettings().setDefaultZoom(WebSettings.ZoomDensity.FAR);
          webView.getSettings().setDomStorageEnabled(true);
          webView.getSettings().setDatabaseEnabled(true);
          webView.getSettings().setDatabasePath("/data/data/" + webView.getContext().getPackageName() + "/databases/");

          webView.loadUrl("url");

          webView.setWebChromeClient(new WebChromeClient() {

          // For api level bellow 24
          public boolean shouldOverrideUrlLoading(WebView view, String url) {

          if (url.startsWith("http")) {
          // Return false means, web view will handle the link
          return false;
          }

          return false;
          }


          // From api level 24
          @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
          public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
          /*Toast.makeText(mcontext, "New Method",Toast.LENGTH_SHORT).show();*/

          // Get the tel: url
          String url = request.getUrl().toString();

          if (url.startsWith("http")) {
          // Return false means, web view will handle the link
          return false;
          }

          return false;
          }

          // openFileChooser for Android 3.0+

          public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType) {
          mUploadMessage = uploadMsg;
          openImageChooser();
          }

          // For Lollipop 5.0+ Devices

          public boolean onShowFileChooser(WebView mWebView, ValueCallback<Uri> filePathCallback, WebChromeClient.FileChooserParams fileChooserParams) {
          mUploadMessages = filePathCallback;
          openImageChooser();
          return true;
          }

          // openFileChooser for Android < 3.0

          public void openFileChooser(ValueCallback<Uri> uploadMsg) {
          openFileChooser(uploadMsg, "");
          }

          //openFileChooser for other Android versions

          public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) {
          openFileChooser(uploadMsg, acceptType);
          }
          });

          }

          private void openImageChooser() {
          try {

          File imageStorageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "FolderName");

          if (!imageStorageDir.exists()) {
          imageStorageDir.mkdirs();
          }
          File file = new File(imageStorageDir + File.separator + "IMG_" + String.valueOf(System.currentTimeMillis()) + ".jpg");
          mCapturedImageURI = Uri.fromFile(file);

          final Intent captureIntent = new Intent(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("image/*");

          Intent chooserIntent = Intent.createChooser(i, "Image Chooser");
          chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Parcelable{captureIntent});

          startActivityForResult(chooserIntent, FILECHOOSER_RESULTCODE);
          } catch (Exception e) {
          e.printStackTrace();
          }
          }

          @Override
          protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
          super.onActivityResult(requestCode, resultCode, intent);

          if (requestCode == FILECHOOSER_RESULTCODE) {

          if (null == this.mUploadMessage && null == this.mUploadMessages) {
          return;
          }

          /* Uri result;
          if (requestCode != RESULT_OK){
          result = null;
          }else {
          result = intent == null ? this.mCapturedImageURI : intent.getData();
          }
          this.mUploadMessage.onReceiveValue(result);
          this.mUploadMessage = null;*/
          if (null != mUploadMessage) {
          handleUploadMessage(requestCode, resultCode, intent);

          } else if (mUploadMessages != null) {
          handleUploadMessages(requestCode, resultCode, intent);
          }
          }
          }

          private void handleUploadMessage(int requestCode, int resultCode, Intent intent) {
          Uri result = null;
          try {
          if (resultCode != RESULT_OK) {
          result = null;
          } else {
          // retrieve from the private variable if the intent is null

          result = intent == null ? mCapturedImageURI : intent.getData();
          }
          } catch (Exception e) {
          e.printStackTrace();
          }
          mUploadMessage.onReceiveValue(result);
          mUploadMessage = null;
          }

          @TargetApi(Build.VERSION_CODES.LOLLIPOP)
          private void handleUploadMessages(int requestCode, int resultCode, Intent intent) {
          Uri results = null;
          try {
          if (resultCode != RESULT_OK) {
          results = null;
          } else {
          if (intent != null) {
          String dataString = intent.getDataString();
          ClipData clipData = intent.getClipData();
          if (clipData != null) {
          results = new Uri[clipData.getItemCount()];
          for (int i = 0; i < clipData.getItemCount(); i++) {
          ClipData.Item item = clipData.getItemAt(i);
          results[i] = item.getUri();
          }
          }
          if (dataString != null) {
          results = new Uri{Uri.parse(dataString)};
          }
          } else {
          results = new Uri{mCapturedImageURI};
          }
          }
          } catch (Exception e) {
          e.printStackTrace();
          }
          mUploadMessages.onReceiveValue(results);
          mUploadMessages = null;
          }

          }






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 16 '18 at 13:39









          Suraj Rao

          24k86074




          24k86074










          answered Nov 16 '18 at 11:31









          A.GunA.Gun

          796




          796













          • Don't forget to add storage and camera permission in manifest as well as programmatically.

            – A.Gun
            Nov 17 '18 at 7:50



















          • Don't forget to add storage and camera permission in manifest as well as programmatically.

            – A.Gun
            Nov 17 '18 at 7:50

















          Don't forget to add storage and camera permission in manifest as well as programmatically.

          – A.Gun
          Nov 17 '18 at 7:50





          Don't forget to add storage and camera permission in manifest as well as programmatically.

          – A.Gun
          Nov 17 '18 at 7:50




















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f49036661%2fupload-image-from-camera-in-webview-does-not-work%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Xamarin.iOS Cant Deploy on Iphone

          Glorious Revolution

          Dulmage-Mendelsohn matrix decomposition in Python