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() {				
	@SuppressWarnings("unused")
	public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) {
		openFileChooser(uploadMsg);
	}

	@SuppressWarnings("unused")
	public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType) {
		openFileChooser(uploadMsg);
	}

	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

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

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:

    https://code.google.com/p/android/issues/detail?id=62220

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s

%d bloggers like this: