Share Coding

Tutorials, Problems, Stuffs …

Tag Archives: picture

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;
	}
}

Script of getting all the pictures’ URL of Photobucket’s album in jQuery

Since the PHP script needs a PHP server for compiling, a jQuery version has been made for reference.
Javascript do not support cross-domain fetching source, but some libraries did helped on those issues.

Click here for Demo

jQuery Plugin: cross-domain-ajax
https://github.com/padolsey/jQuery-Plugins/ And it is depends on jQuery: http://jquery.com/

 
1. We need to include two libraries: jQuery and cross-domain-ajax(jQuery Plugin)

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js"></script>
<script type="text/javascript" src="https://raw.github.com/padolsey/jQuery-Plugins/master/cross-domain-ajax/jquery.xdomainajax.js"></script>

 
Read more of this post

Script of getting all the pictures’ URL of Photobucket’s album in PHP

http://s283.photobucket.com/albums/kk285/konnarak_1608/romantic/
Using above album for example.

Click here for Demo

<?php
$url = 'http://s283.photobucket.com/albums/kk285/konnarak_1608/romantic/';
$str = file_get_contents($url);
preg_match_all('/<img[^>]+>/i',$str, $result); 

foreach( $result as $img_tag) {
	foreach( $img_tag as $img) {
		if( !strpos($img, 'class="under off"') ) continue;
		preg_match('/< *img[^>]*src *= *["\']?([^"\']*)/i', $img, $imgURLs);
		$imgURL = str_replace("/th_", "/", $imgURLs[1]);
		echo $imgURL . "<br />";
	}
}
?>