Share Coding

Tutorials, Problems, Stuffs …

Photobucket Album Downloader v2.0.1

To all who interested on my Photobucket Album Downloader Extension,

I am glad to introduce you the new Photobucket Album Downloader v2.0.1 extension.
Free version: http://goo.gl/2aGxLC
Unlimit version: http://goo.gl/1IFGwh

 

New features are listing below:
1. No longer rely on any server, no more SSL or speed issues.
2. No longer use popup dialog, it now embeds a “Download” button on photobucket website.
How to upgrade extension version:
1. Enter “chrome://extensions” on your chrome browser
2. Click “Update Extension” on the top right hand side
3. Done
Steps of downloading album: 
1. Go to any album, such as: http://s237.photobucket.com/user/bekadrk89/library/Photography
2. Find the Panel “Links to share this album” on the right and you can see a “Download” button
3. Click the “Download” button and you are now downloading photos

jQuery AJAX upload file

HTML form:

<form id="uploadForm">
	<input type="file" name="attachement" />
</form>

 

Javascript(jQuery):

$.ajax({
	type: "POST",
	url: "upload.php",
	data: new FormData($('#uploadForm')[0]),
	cache: false,
	contentType: false,
	processData: false,
	success: function (result) {
		console.log(result);
	}
});

[joda time] Convert timestamp into “Posted 3 minutes ago”

DateTimeZone.setDefault(DateTimeZone.UTC);
DateTime myBirthDate = new DateTime(comment.getTime()*1000);
DateTime now = new DateTime();
Period period = new Period(myBirthDate, now);
Log.d("period",period.toString());

PeriodFormatterBuilder builder = new PeriodFormatterBuilder();
if(period.getYears() != 0) {
	builder.appendYears().appendSuffix(" years ago\n");
} else if(period.getMonths() != 0) {
	builder.appendMonths().appendSuffix(" months ago\n");
} else if(period.getDays() != 0) {
	builder.appendDays().appendSuffix(" days ago\n");
} else if(period.getHours() != 0) {
	builder.appendHours().appendSuffix(" hours ago\n");
} else if(period.getMinutes() != 0) {
	builder.appendMinutes().appendSuffix(" minutes ago\n");
} else if(period.getSeconds() != 0) {
	builder.appendSeconds().appendSuffix(" seconds ago\n");
}
PeriodFormatter formatter = builder.printZeroNever().toFormatter();
String elapsed = formatter.print(period);

TextView txtComment_time = (TextView) comment_list_item.findViewById(R.id.txtComment_time);
txtComment_time.setText(elapsed);

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

AngularJS – Cross Domin Get data by $resource

For reference:

http://docs.angularjs.org/api/ngResource.$resource

Required:

http://code.angularjs.org/1.2.0rc1/angular-resource.js

Module Configuration:

var myApp = angular.module('MyApp', ['ngResource']);
myApp.config(['$routeProvider', ,'$httpProvider', function($routeProvider, $httpProvider) {
    $httpProvider.defaults.useXDomain = true;
    delete $httpProvider.defaults.headers.common['X-Requested-With'];
    ....
  }
]);

function: $resource(url[, paramDefaults][, actions]);

var User = $resource('http://localhost:port/sil/SilUserAuth.svc/Login', {port:':59217'}, { });
User.get({
       name: document.loginForm.email.value,
       password: document.loginForm.password.value
}, function (data) {
       alert(JSON.stringify(data));
});

Result on Firebug View:

GET http://localhost:59217/sil/SilUserAuth.svc/Login?name=abc&password=abc   200s OK 12ms

In ASP.NET MVC 4:

<script type="text/javascript" src="@Url.Content("~/Scripts/angular.js")"></script>
<script type="text/javascript" src="@Url.Content("~/Scripts/angular-resource.js")"></script>

Set httpProtocol on Web.config

    <httpProtocol>
      <customHeaders>
        <add name="Access-Control-Allow-Origin" value="*" />
        <add name="Access-Control-Allow-Headers" value="Content-Type" />
      </customHeaders>
    </httpProtocol>

Problem 1

OPTIONS xxxxxxxxx (405 Method Not Allowed)

Solution
Remove the header from all requests :

"delete $httpProvider.defaults.headers.common['X-Requested-With'];"

Problem 2

Angularjs Error: destination.push is not a function

Solution
Define action type get / query / update / delete and  add isArray: true:

    var Papers = $resource('http://localhost:port/Sil/SilUserPaper.svc/UserPapers/:id', { port: ':59217' }, {
        get: { method: 'GET', params: {}, isArray: true }
    });
    Papers.get({
        id: 2
    }, function (data) {
        $scope.papers = data;
    });
Follow

Get every new post delivered to your Inbox.