Share Coding

Tutorials, Problems, Stuffs …

Android WebView Upload Picture via Camera

When clicking an Input File button on webview,
we want to take a picture from camera and place the image to that input.

1. Declare instance variables

private final static int CAPTURE_RESULTCODE = 1;
private ValueCallback<Uri> mUploadMessage;
private String filePath;

2. Set custom webChromeClient to WebView

myWebView.setWebChromeClient(new WebChromeClient() {				
	public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) {

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

	public void openFileChooser(ValueCallback<Uri> uploadMsg) {
		this.mUploadMessage = uploadMsg;

		File imageStorageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "appName");
		this.filePath = imageStorageDir + File.separator + "IMG_" + String.valueOf(System.currentTimeMillis()) + ".jpg";
		File file = new File(this.filePath);

		Intent captureIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
		captureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));
		MainActivity.this.startActivityForResult(captureIntent,  CAPTURE_RESULTCODE); 

3. Apply onActivityResult to handle the picture after capture

protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
	if (requestCode == CAPTURE_RESULTCODE) {
		if (null == this.mUploadMessage || (resultCode != RESULT_OK && !new File(filePath).exists())) {
		} 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;

7 responses to “Android WebView Upload Picture via Camera

  1. Joe Dwyer 2013/12/29 at 12:32 pm

    New to android and just curious as to how or where to put the onActivityResult portion of the code?

    I tried to put it in a few different places and kept getting errors. Any help would be great as I try and get finish getting a mobile app up and running.

  2. Joe 2014/01/03 at 12:14 am

    Is there a way to add the feature of selecting from the gallery or the camera? and thank you for the help with the onActivity portion of the code as a newbie to android programming I really appreciate it.

  3. Kenneth Tse 2014/02/06 at 12:56 am

    System.currentTimeMillis() should not used with String.valueOf
    System can not get a correct path and the camera can not save come out by press ‘confirm’ because image output failed.
    You may see that the file path is correct but no effect and no error message when you press ‘tick’
    PS: Nexus 5

  4. Martin Meixger 2014/02/20 at 3:43 am

    unfortunately it seems, that Android KitKat 4.4 broke openFileChooser – see this bug report:

Leave a Reply

Please log in using one of these methods to post your comment: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: